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 publicip
import (
"context"
"errors"
"fmt"
"io"
"math/rand"
@@ -27,6 +28,8 @@ func NewIPGetter(client *http.Client) IPGetter {
}
}
var ErrParseIP = errors.New("cannot parse IP address")
func (i *ipGetter) Get(ctx context.Context) (ip net.IP, err error) {
urls := []string{
"https://ifconfig.me/ip",
@@ -63,7 +66,7 @@ func (i *ipGetter) Get(ctx context.Context) (ip net.IP, err error) {
s := strings.ReplaceAll(string(content), "\n", "")
ip = net.ParseIP(s)
if ip == nil {
return nil, fmt.Errorf("cannot parse IP address from %q", s)
return nil, fmt.Errorf("%w: %s", ErrParseIP, s)
}
return ip, nil
}

View File

@@ -1,6 +1,7 @@
package publicip
import (
"errors"
"fmt"
"net"
"reflect"
@@ -32,6 +33,8 @@ func (l *looper) GetStatus() (status models.LoopStatus) {
return l.state.status
}
var ErrInvalidStatus = errors.New("invalid status")
func (l *looper) SetStatus(status models.LoopStatus) (outcome string, err error) {
l.state.statusMu.Lock()
defer l.state.statusMu.Unlock()
@@ -67,8 +70,8 @@ func (l *looper) SetStatus(status models.LoopStatus) (outcome string, err error)
l.state.status = status
return status.String(), nil
default:
return "", fmt.Errorf("status %q can only be %q or %q",
status, constants.Running, constants.Stopped)
return "", fmt.Errorf("%w: %s: it can only be one of: %s, %s",
ErrInvalidStatus, status, constants.Running, constants.Stopped)
}
}