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

@@ -2,6 +2,7 @@ package openvpn
import (
"context"
"errors"
"fmt"
"strings"
@@ -14,6 +15,8 @@ func (c *configurator) Start(ctx context.Context) (
return c.commander.Start(ctx, "openvpn", "--config", constants.OpenVPNConf)
}
var ErrVersionTooShort = errors.New("version output is too short")
func (c *configurator) Version(ctx context.Context) (string, error) {
output, err := c.commander.Run(ctx, "openvpn", "--version")
if err != nil && err.Error() != "exit status 1" {
@@ -23,7 +26,7 @@ func (c *configurator) Version(ctx context.Context) (string, error) {
words := strings.Fields(firstLine)
const minWords = 2
if len(words) < minWords {
return "", fmt.Errorf("openvpn --version: first line is too short: %q", firstLine)
return "", fmt.Errorf("%w: %s", ErrVersionTooShort, firstLine)
}
return words[1], nil
}

View File

@@ -1,6 +1,7 @@
package openvpn
import (
"errors"
"fmt"
"reflect"
"sync"
@@ -43,6 +44,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()
@@ -78,8 +81,8 @@ func (l *looper) SetStatus(status models.LoopStatus) (outcome string, err error)
l.state.status = constants.Stopped
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)
}
}