Skip to content

Commit 894406e

Browse files
author
Akos Kitta
committed
Added better error handling for the gRPC server.
- 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 bc8e073 commit 894406e

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

Diff for: cli/daemon/daemon.go

+25-1
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@
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"
2730
"github.com/arduino/arduino-cli/cli/globals"
2831
"github.com/arduino/arduino-cli/commands/daemon"
2932
srv_commands "github.com/arduino/arduino-cli/rpc/commands"
@@ -87,8 +90,29 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
8790
logrus.Infof("Starting daemon on TCP port %s", port)
8891
lis, err := net.Listen("tcp", fmt.Sprintf(":%s", port))
8992
if err != nil {
90-
logrus.Fatalf("failed to listen: %v", err)
93+
// Invalid port, such as "Foo"
94+
var dnsError *net.DNSError
95+
if errors.As(err, &dnsError) {
96+
logrus.Error("Failed to listen on TCP port: %s. %v is unknown name.", port, dnsError.Name)
97+
os.Exit(errorcodes.ErrCoreConfig)
98+
}
99+
// Invalid port number, such as -1
100+
var addrError *net.AddrError
101+
if errors.As(err, &addrError) {
102+
logrus.Error("Failed to listen on TCP port: %s. %v is an invalid port.", port, addrError.Addr)
103+
os.Exit(errorcodes.ErrCoreConfig)
104+
}
105+
// Port is already in use
106+
var syscallErr *os.SyscallError
107+
if errors.As(err, &syscallErr) && errors.Is(syscallErr.Err, syscall.EADDRINUSE) {
108+
logrus.Error("Failed to listen on TCP port: %s. Address already in use.", port)
109+
os.Exit(errorcodes.ErrNetwork)
110+
}
111+
logrus.Error("Failed to listen on TCP port: %s. Unexpected error: %v", port, err)
112+
os.Exit(errorcodes.ErrGeneric)
91113
}
114+
// This message will show up on the stdout of the daemon process so that gRPC clients know it is time to connect.
115+
logrus.Infof("Daemon is listening on TCP port %s...", port)
92116
if err := s.Serve(lis); err != nil {
93117
logrus.Fatalf("failed to serve: %v", err)
94118
}

0 commit comments

Comments
 (0)