From 5a339c8e24bc89d69fc2d1500f893035d1b5aa85 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 2 Nov 2023 22:21:12 +0100 Subject: [PATCH 1/2] Added error code for daemon failing to listen to TCP port --- internal/cli/daemon/daemon.go | 12 ++++++------ internal/cli/feedback/errorcodes.go | 7 +++++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/internal/cli/daemon/daemon.go b/internal/cli/daemon/daemon.go index 5024f4255d0..9829b6bbd61 100644 --- a/internal/cli/daemon/daemon.go +++ b/internal/cli/daemon/daemon.go @@ -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 @@ -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] @@ -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) } } diff --git a/internal/cli/feedback/errorcodes.go b/internal/cli/feedback/errorcodes.go index 1e1a0edf19e..75cae306279 100644 --- a/internal/cli/feedback/errorcodes.go +++ b/internal/cli/feedback/errorcodes.go @@ -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 ) From 5874decd738e7b6d9a2ac20b2b9975bf94bc6de7 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 3 Nov 2023 00:33:35 +0100 Subject: [PATCH 2/2] Better handling of daemon random port selection --- internal/cli/daemon/daemon.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/cli/daemon/daemon.go b/internal/cli/daemon/daemon.go index 9829b6bbd61..bed30fcead5 100644 --- a/internal/cli/daemon/daemon.go +++ b/internal/cli/daemon/daemon.go @@ -136,18 +136,18 @@ func runDaemonCommand(cmd *cobra.Command, args []string) { 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 - // us choose it randomly, in all other cases we already - // know which is used. + // We need to retrieve the port used only if the user did not specify it + // and let the OS choose it randomly, in all other cases we already know + // which port is used. if port == "0" { address := lis.Addr() split := strings.Split(address.String(), ":") - if len(split) == 0 { + if len(split) <= 1 { feedback.Fatal(tr("Invalid TCP address: port is missing"), feedback.ErrBadTCPPortArgument) } - port = split[len(split)-1] + port = split[1] } feedback.PrintResult(daemonResult{