Skip to content

Commit 1eefe49

Browse files
authored
Added better error handling for the gRPC server. (#592)
- Consumers of the CLI have a better exit code when it fails at startup - Logged message to stdout, so gRPC clients know, it is time to connect Signed-off-by: Akos Kitta <[email protected]>
1 parent 11abbee commit 1eefe49

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

Diff for: cli/daemon/daemon.go

+26-1
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@
1616
package daemon
1717

1818
import (
19+
"errors"
1920
"fmt"
2021
"io"
2122
"io/ioutil"
2223
"net"
2324
"net/http"
2425
"os"
2526
"runtime"
27+
"syscall"
2628

29+
"github.com/arduino/arduino-cli/cli/errorcodes"
30+
"github.com/arduino/arduino-cli/cli/feedback"
2731
"github.com/arduino/arduino-cli/cli/globals"
2832
"github.com/arduino/arduino-cli/commands/daemon"
2933
srv_commands "github.com/arduino/arduino-cli/rpc/commands"
@@ -87,8 +91,29 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
8791
logrus.Infof("Starting daemon on TCP port %s", port)
8892
lis, err := net.Listen("tcp", fmt.Sprintf(":%s", port))
8993
if err != nil {
90-
logrus.Fatalf("failed to listen: %v", err)
94+
// Invalid port, such as "Foo"
95+
var dnsError *net.DNSError
96+
if errors.As(err, &dnsError) {
97+
feedback.Errorf("Failed to listen on TCP port: %s. %s is unknown name.", port, dnsError.Name)
98+
os.Exit(errorcodes.ErrCoreConfig)
99+
}
100+
// Invalid port number, such as -1
101+
var addrError *net.AddrError
102+
if errors.As(err, &addrError) {
103+
feedback.Errorf("Failed to listen on TCP port: %s. %s is an invalid port.", port, addrError.Addr)
104+
os.Exit(errorcodes.ErrCoreConfig)
105+
}
106+
// Port is already in use
107+
var syscallErr *os.SyscallError
108+
if errors.As(err, &syscallErr) && errors.Is(syscallErr.Err, syscall.EADDRINUSE) {
109+
feedback.Errorf("Failed to listen on TCP port: %s. Address already in use.", port)
110+
os.Exit(errorcodes.ErrNetwork)
111+
}
112+
feedback.Errorf("Failed to listen on TCP port: %s. Unexpected error: %v", port, err)
113+
os.Exit(errorcodes.ErrGeneric)
91114
}
115+
// This message will show up on the stdout of the daemon process so that gRPC clients know it is time to connect.
116+
logrus.Infof("Daemon is listening on TCP port %s...", port)
92117
if err := s.Serve(lis); err != nil {
93118
logrus.Fatalf("failed to serve: %v", err)
94119
}

0 commit comments

Comments
 (0)