Skip to content

Commit

Permalink
Added error code for daemon failing to listen to TCP port
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Nov 3, 2023
1 parent cc3396f commit 5a339c8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
12 changes: 6 additions & 6 deletions internal/cli/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,19 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
// Invalid port, such as "Foo"
var dnsError *net.DNSError
if errors.As(err, &dnsError) {
feedback.Fatal(tr("Failed to listen on TCP port: %[1]s. %[2]s is unknown name.", port, dnsError.Name), feedback.ErrCoreConfig)
feedback.Fatal(tr("Failed to listen on TCP port: %[1]s. %[2]s is unknown name.", port, dnsError.Name), feedback.ErrBadTCPPortArgument)
}
// Invalid port number, such as -1
var addrError *net.AddrError
if errors.As(err, &addrError) {
feedback.Fatal(tr("Failed to listen on TCP port: %[1]s. %[2]s is an invalid port.", port, addrError.Addr), feedback.ErrCoreConfig)
feedback.Fatal(tr("Failed to listen on TCP port: %[1]s. %[2]s is an invalid port.", port, addrError.Addr), feedback.ErrBadTCPPortArgument)
}
// Port is already in use
var syscallErr *os.SyscallError
if errors.As(err, &syscallErr) && errors.Is(syscallErr.Err, syscall.EADDRINUSE) {
feedback.Fatal(tr("Failed to listen on TCP port: %s. Address already in use.", port), feedback.ErrNetwork)
feedback.Fatal(tr("Failed to listen on TCP port: %s. Address already in use.", port), feedback.ErrFailedToListenToTCPPort)
}
feedback.Fatal(tr("Failed to listen on TCP port: %[1]s. Unexpected error: %[2]v", port, err), feedback.ErrGeneric)
feedback.Fatal(tr("Failed to listen on TCP port: %[1]s. Unexpected error: %[2]v", port, err), feedback.ErrFailedToListenToTCPPort)
}

// We need to parse the port used only if the user let
Expand All @@ -144,7 +144,7 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
split := strings.Split(address.String(), ":")

if len(split) == 0 {
feedback.Fatal(tr("Invalid TCP address: port is missing"), feedback.ErrBadArgument)
feedback.Fatal(tr("Invalid TCP address: port is missing"), feedback.ErrBadTCPPortArgument)
}

port = split[len(split)-1]
Expand All @@ -156,7 +156,7 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
})

if err := s.Serve(lis); err != nil {
logrus.Fatalf("Failed to serve: %v", err)
feedback.Fatal(fmt.Sprintf("Failed to serve: %v", err), feedback.ErrFailedToListenToTCPPort)
}
}

Expand Down
7 changes: 7 additions & 0 deletions internal/cli/feedback/errorcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,11 @@ const (

// ErrBadArgument is returned when the arguments are not valid (7)
ErrBadArgument

// ErrFailedToListenToTCPPort is returned if the CLI failed to open a TCP port
// to listen for incoming connections (8)
ErrFailedToListenToTCPPort

// ErrBadTCPPortArgument is returned if the TCP port argument is not valid (9)
ErrBadTCPPortArgument
)

0 comments on commit 5a339c8

Please sign in to comment.