Maintenance: improve error wrapping

This commit is contained in:
Quentin McGaw (desktop)
2021-05-30 16:14:08 +00:00
parent be22c8547f
commit 876563c492
19 changed files with 96 additions and 41 deletions

View File

@@ -3,6 +3,7 @@ package server
import (
"context"
"errors"
"net/http"
"time"
@@ -51,7 +52,7 @@ func (s *server) Run(ctx context.Context, done chan<- struct{}) {
}()
s.logger.Info("listening on %s", s.address)
err := server.ListenAndServe()
if err != nil && ctx.Err() != context.Canceled {
if err != nil && errors.Is(ctx.Err(), context.Canceled) {
s.logger.Error(err)
}
}

View File

@@ -1,6 +1,7 @@
package server
import (
"errors"
"fmt"
"github.com/qdm12/gluetun/internal/constants"
@@ -11,15 +12,16 @@ type statusWrapper struct {
Status string `json:"status"`
}
var errInvalidStatus = errors.New("invalid status")
func (sw *statusWrapper) getStatus() (status models.LoopStatus, err error) {
status = models.LoopStatus(sw.Status)
switch status {
case constants.Stopped, constants.Running:
return status, nil
default:
return "", fmt.Errorf(
"invalid status %q: possible values are: %s, %s",
sw.Status, constants.Stopped, constants.Running)
return "", fmt.Errorf("%w: %s: possible values are: %s, %s",
errInvalidStatus, sw.Status, constants.Stopped, constants.Running)
}
}