Skip to content

Added better error handling for the gRPC server. #592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion cli/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@
package daemon

import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"runtime"
"syscall"

"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cli/cli/globals"
"github.com/arduino/arduino-cli/commands/daemon"
srv_commands "github.com/arduino/arduino-cli/rpc/commands"
Expand Down Expand Up @@ -87,8 +91,29 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
logrus.Infof("Starting daemon on TCP port %s", port)
lis, err := net.Listen("tcp", fmt.Sprintf(":%s", port))
if err != nil {
logrus.Fatalf("failed to listen: %v", err)
// Invalid port, such as "Foo"
var dnsError *net.DNSError
if errors.As(err, &dnsError) {
feedback.Errorf("Failed to listen on TCP port: %s. %s is unknown name.", port, dnsError.Name)
os.Exit(errorcodes.ErrCoreConfig)
}
// Invalid port number, such as -1
var addrError *net.AddrError
if errors.As(err, &addrError) {
feedback.Errorf("Failed to listen on TCP port: %s. %s is an invalid port.", port, addrError.Addr)
os.Exit(errorcodes.ErrCoreConfig)
}
// Port is already in use
var syscallErr *os.SyscallError
if errors.As(err, &syscallErr) && errors.Is(syscallErr.Err, syscall.EADDRINUSE) {
feedback.Errorf("Failed to listen on TCP port: %s. Address already in use.", port)
os.Exit(errorcodes.ErrNetwork)
}
feedback.Errorf("Failed to listen on TCP port: %s. Unexpected error: %v", port, err)
os.Exit(errorcodes.ErrGeneric)
}
// This message will show up on the stdout of the daemon process so that gRPC clients know it is time to connect.
logrus.Infof("Daemon is listening on TCP port %s...", port)
if err := s.Serve(lis); err != nil {
logrus.Fatalf("failed to serve: %v", err)
}
Expand Down