diff --git a/cmd/helper.go b/cmd/helper.go index 6bd936bf0..bc0be8e5a 100644 --- a/cmd/helper.go +++ b/cmd/helper.go @@ -3,6 +3,7 @@ package cmd import ( "errors" "fmt" + "net" "os" "path/filepath" "regexp" @@ -99,3 +100,24 @@ func shutdownDoneC() <-chan struct{} { go shutdown.Cleanup(doneC) return doneC } + +func wrapErrors(err error) error { + if err != nil { + var opErr *net.OpError + var pathErr *os.PathError + + switch { + case errors.As(err, &opErr): + if opErr.Op == "listen" && strings.Contains(opErr.Error(), "address already in use") { + err = fmt.Errorf("could not open port- check that evcc is not already running (%w)", err) + } + + case errors.As(err, &pathErr): + if pathErr.Op == "remove" && strings.Contains(pathErr.Error(), "operation not permitted") { + err = fmt.Errorf("could not remove file- check that evcc is not already running (%w)", err) + } + } + } + + return err +} diff --git a/cmd/root.go b/cmd/root.go index 9fffa320a..372787ae8 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -268,6 +268,9 @@ func runRoot(cmd *cobra.Command, args []string) { once.Do(func() { close(stopC) }) // signal loop to end }) + // improve error message + err = wrapErrors(err) + publishErrorInfo(valueChan, cfgFile, err) log.FATAL.Println(err) @@ -282,5 +285,5 @@ func runRoot(cmd *cobra.Command, args []string) { // uds health check listener go server.HealthListener(site) - log.FATAL.Println(httpd.ListenAndServe()) + log.FATAL.Println(wrapErrors(httpd.ListenAndServe())) }