Improve error messages (#6014)

This commit is contained in:
andig 2023-02-03 17:30:50 +01:00 • committed by GitHub
parent 3324a0f1a7
commit 7a307c356a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View file

@ -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
}

View file

@ -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()))
}