Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dfafb49

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 <kittaakos@typefox.io>
1 parent bc8e073 commit dfafb49

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

‎cli/daemon/daemon.go‎

Lines changed: 26 additions & 1 deletion
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)

AltStyle によって変換されたページ (->オリジナル) /