summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2024-02-06 14:43:06 +0100
committerJeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com>2024-02-19 14:15:07 +0100
commit3fd29f3e1c963196cac69fcbb9d68116f7ea80ec (patch)
tree7cb586d304167e4198166ff17dc79d33122a75e5 /internal
parent2337dcde60a710d2f65d3fe1107811202e34c633 (diff)
All: Prepare to get rid of go-errors/errors lib
Diffstat (limited to 'internal')
-rw-r--r--internal/discovery/discovery.go22
-rw-r--r--internal/failover/monitor.go5
-rw-r--r--internal/failover/ping.go8
-rw-r--r--internal/failover/ping_default.go4
-rw-r--r--internal/failover/ping_windows.go3
-rw-r--r--internal/fsm/fsm.go7
-rw-r--r--internal/http/http.go22
-rw-r--r--internal/log/log.go3
-rw-r--r--internal/test/server.go3
-rw-r--r--internal/util/util.go4
-rw-r--r--internal/verify/verify.go19
11 files changed, 47 insertions, 53 deletions
diff --git a/internal/discovery/discovery.go b/internal/discovery/discovery.go
index 06548f9..d89f0f5 100644
--- a/internal/discovery/discovery.go
+++ b/internal/discovery/discovery.go
@@ -10,7 +10,6 @@ import (
"github.com/eduvpn/eduvpn-common/internal/http"
"github.com/eduvpn/eduvpn-common/internal/verify"
discotypes "github.com/eduvpn/eduvpn-common/types/discovery"
- "github.com/go-errors/errors"
)
// HasCache denotes whether or not we have an embedded cache available
@@ -76,8 +75,7 @@ func (discovery *Discovery) file(ctx context.Context, jsonFile string, previousV
// Parse JSON to extract version and list
if err = json.Unmarshal(body, structure); err != nil {
- return errors.WrapPrefix(err,
- fmt.Sprintf("failed parsing discovery file: %s from the server", jsonFile), 0)
+ return fmt.Errorf("failed parsing discovery file: '%s' from the server with error: %w", jsonFile, err)
}
return nil
@@ -122,7 +120,15 @@ func (discovery *Discovery) ServerByURL(
return &currentServer, nil
}
}
- return nil, errors.Errorf("no server of type '%s' at URL '%s'", srvType, baseURL)
+ return nil, fmt.Errorf("no server of type '%s' at URL '%s'", srvType, baseURL)
+}
+
+type CountryNotFoundError struct {
+ CountryCode string
+}
+
+func (cnf *CountryNotFoundError) Error() string {
+ return fmt.Sprintf("no secure internet server with country code: '%s'", cnf.CountryCode)
}
// ServerByCountryCode returns the discovery server by the country code
@@ -133,7 +139,7 @@ func (discovery *Discovery) ServerByCountryCode(countryCode string) (*discotypes
return &srv, nil
}
}
- return nil, errors.Errorf("no server of type 'secure_internet' with country code '%s'", countryCode)
+ return nil, &CountryNotFoundError{CountryCode: countryCode}
}
// orgByID returns the discovery organization by the organization ID
@@ -144,7 +150,7 @@ func (discovery *Discovery) orgByID(orgID string) (*discotypes.Organization, err
return &org, nil
}
}
- return nil, errors.Errorf("no secure internet home found in organization '%s'", orgID)
+ return nil, fmt.Errorf("no secure internet home found in organization '%s'", orgID)
}
// SecureHomeArgs returns the secure internet home server arguments:
@@ -189,7 +195,7 @@ func (discovery *Discovery) previousOrganizations() (*discotypes.Organizations,
// We do not have a cached struct, this we need to get it using the embedded JSON
var eo discotypes.Organizations
if err := json.Unmarshal(eOrganizations, &eo); err != nil {
- return nil, errors.WrapPrefix(err, "failed parsing discovery organizations from the embedded cache", 0)
+ return nil, fmt.Errorf("failed parsing discovery organizations from the embedded cache with error: %w", err)
}
discovery.OrganizationList = eo
return &eo, nil
@@ -205,7 +211,7 @@ func (discovery *Discovery) previousServers() (*discotypes.Servers, error) {
// We do not have a cached struct, this we need to get it using the embedded JSON
var es discotypes.Servers
if err := json.Unmarshal(eServers, &es); err != nil {
- return nil, errors.WrapPrefix(err, "failed parsing discovery servers from the embedded cache", 0)
+ return nil, fmt.Errorf("failed parsing discovery servers from the embedded cache with error: %w", err)
}
discovery.ServerList = es
return &es, nil
diff --git a/internal/failover/monitor.go b/internal/failover/monitor.go
index 2219fc4..987cce6 100644
--- a/internal/failover/monitor.go
+++ b/internal/failover/monitor.go
@@ -1,11 +1,12 @@
package failover
import (
+ "fmt"
"context"
+ "errors"
"time"
"github.com/eduvpn/eduvpn-common/internal/log"
- "github.com/go-errors/errors"
)
// The DroppedConMon is a connection monitor that checks for an increase in rx bytes in certain intervals
@@ -91,7 +92,7 @@ func (m *DroppedConMon) Start(ctx context.Context, gateway string, mtuSize int)
case <-ticker.C:
continue
case <-ctx.Done():
- return false, errors.WrapPrefix(context.Canceled, "failover was stopped", 0)
+ return false, fmt.Errorf("failover was stopped with error: %w", context.Canceled)
}
}
diff --git a/internal/failover/ping.go b/internal/failover/ping.go
index fb1df4e..3b5faa8 100644
--- a/internal/failover/ping.go
+++ b/internal/failover/ping.go
@@ -8,8 +8,6 @@ import (
"golang.org/x/net/icmp"
"golang.org/x/net/ipv4"
-
- "github.com/go-errors/errors"
)
// mtuOverhead defines the total MTU overhead for an ICMP ECHO message: 20 bytes IP header + 8 bytes ICMP header
@@ -41,7 +39,7 @@ func (p Pinger) Read(deadline time.Time) error {
case ipv4.ICMPTypeEchoReply:
return nil
default:
- return errors.Errorf("Not a ping echo reply, got %+v", got)
+ return fmt.Errorf("not a ping echo reply, got: %+v", got)
}
}
@@ -58,12 +56,12 @@ func (p Pinger) Send(seq int) error {
// Marshal the message to bytes
b, err := m.Marshal(nil)
if err != nil {
- return errors.WrapPrefix(err, errorMessage, 0)
+ return fmt.Errorf("%s with error: %w", errorMessage, err)
}
// And send it to the gateway IP!
_, err = p.listener.WriteTo(b, p.gateway)
if err != nil {
- return errors.WrapPrefix(err, errorMessage, 0)
+ return fmt.Errorf("%s with error: %w", errorMessage, err)
}
return nil
diff --git a/internal/failover/ping_default.go b/internal/failover/ping_default.go
index ef56d5e..c490869 100644
--- a/internal/failover/ping_default.go
+++ b/internal/failover/ping_default.go
@@ -3,16 +3,16 @@
package failover
import (
+ "fmt"
"net"
- "github.com/go-errors/errors"
"golang.org/x/net/icmp"
)
func NewPinger(gateway string, size int) (*Pinger, error) {
l, err := icmp.ListenPacket("udp4", "0.0.0.0")
if err != nil {
- return nil, errors.WrapPrefix(err, "failed creating ping", 0)
+ return nil, fmt.Errorf("failed creating ping with error: %w", err)
}
return &Pinger{
listener: l,
diff --git a/internal/failover/ping_windows.go b/internal/failover/ping_windows.go
index 0de1cdf..d6b7703 100644
--- a/internal/failover/ping_windows.go
+++ b/internal/failover/ping_windows.go
@@ -3,14 +3,13 @@ package failover
import (
"net"
- "github.com/go-errors/errors"
"golang.org/x/net/icmp"
)
func NewPinger(gateway string, size int) (*Pinger, error) {
l, err := icmp.ListenPacket("ip4:icmp", "0.0.0.0")
if err != nil {
- return nil, errors.WrapPrefix(err, "failed creating ping", 0)
+ return nil, fmt.Errorf("failed creating ping with error: %w", err)
}
return &Pinger{
listener: l,
diff --git a/internal/fsm/fsm.go b/internal/fsm/fsm.go
index c7ae104..1b38e3a 100644
--- a/internal/fsm/fsm.go
+++ b/internal/fsm/fsm.go
@@ -7,8 +7,6 @@ import (
"os"
"path"
"sort"
-
- "github.com/go-errors/errors"
)
type (
@@ -108,7 +106,7 @@ func (fsm *FSM) CheckTransition(desired StateID) error {
return nil
}
}
- return errors.Errorf("fsm invalid transition attempt from '%s' to '%s'", fsm.GetStateName(fsm.Current), fsm.GetStateName(desired))
+ return fmt.Errorf("fsm invalid transition attempt from '%s' to '%s'", fsm.GetStateName(fsm.Current), fsm.GetStateName(desired))
}
// graphFilename gets the full path to the graph filename including the .graph extension.
@@ -146,7 +144,7 @@ func (fsm *FSM) GoTransitionRequired(newState StateID, data interface{}) error {
}
// transition is not handled
if !handled {
- return errors.Errorf("fsm failed transition from '%v' to '%v', is this required transition handled?", fsm.GetStateName(oldState), fsm.GetStateName(newState))
+ return fmt.Errorf("fsm failed transition from '%s' to '%s', is this required transition handled?", fsm.GetStateName(oldState), fsm.GetStateName(newState))
}
return nil
}
@@ -163,7 +161,6 @@ func (fsm *FSM) GoTransitionWithData(newState StateID, data interface{}) (bool,
if fsm.Generate {
fsm.writeGraph()
}
-
return fsm.StateCallback(prev, newState, data), nil
}
diff --git a/internal/http/http.go b/internal/http/http.go
index b5250b6..536a9c3 100644
--- a/internal/http/http.go
+++ b/internal/http/http.go
@@ -3,6 +3,7 @@ package http
import (
"context"
+ "errors"
"fmt"
"io"
"net/http"
@@ -11,7 +12,7 @@ import (
"strings"
"time"
- "github.com/go-errors/errors"
+ "github.com/eduvpn/eduvpn-common/internal/version"
)
// UserAgent is the user agent that is used for requests
@@ -53,7 +54,7 @@ func cleanPath(u *url.URL, trailing bool) string {
func EnsureValidURL(s string, trailing bool) (string, error) {
u, err := url.Parse(s)
if err != nil {
- return "", errors.WrapPrefix(err, "failed parsing url", 0)
+ return "", fmt.Errorf("failed parsing url with error: %w", err)
}
// Make sure the scheme is always https
@@ -67,11 +68,11 @@ func EnsureValidURL(s string, trailing bool) (string, error) {
func JoinURLPath(u string, p string) (string, error) {
pu, err := url.Parse(u)
if err != nil {
- return "", errors.WrapPrefix(err, "failed to parse url for joining paths", 0)
+ return "", fmt.Errorf("failed to parse url for joining paths with error: %w", err)
}
pp, err := url.Parse(p)
if err != nil {
- return "", errors.WrapPrefix(err, "failed to parse path for joining paths", 0)
+ return "", fmt.Errorf("failed to parse path for joining paths with error: %w", err)
}
fp := pu.ResolveReference(pp)
@@ -95,8 +96,7 @@ func ConstructURL(u *url.URL, params URLParameters) (string, error) {
func optionalURL(urlStr string, opts *OptionalParams) (string, error) {
u, err := url.Parse(urlStr)
if err != nil {
- return "", errors.WrapPrefix(err,
- fmt.Sprintf("failed to construct parse url '%s'", urlStr), 0)
+ return "", fmt.Errorf("failed to construct parse url '%s' with error: %w", urlStr, err)
}
// Make sure the scheme is always set to HTTPS
if u.Scheme != "https" {
@@ -190,8 +190,7 @@ func (c *Client) Do(ctx context.Context, method string, urlStr string, opts *Opt
// Create request object with the body reader generated from the optional arguments
req, err := http.NewRequestWithContext(ctx, method, urlStr, optionalBodyReader(opts))
if err != nil {
- return nil, nil, errors.WrapPrefix(err,
- fmt.Sprintf("failed HTTP request with method %s and url %s", method, urlStr), 0)
+ return nil, nil, fmt.Errorf("failed HTTP request with method: '%s', url: '%s' and error: %w", method, urlStr, err)
}
if UserAgent != "" {
req.Header.Add("User-Agent", UserAgent)
@@ -222,11 +221,10 @@ func (c *Client) Do(ctx context.Context, method string, urlStr string, opts *Opt
r := http.MaxBytesReader(nil, res.Body, c.ReadLimit)
body, err := io.ReadAll(r)
if err != nil {
- return res.Header, nil, errors.WrapPrefix(err,
- fmt.Sprintf("failed HTTP request with method: %s, url: %s and max bytes size: %v", method, urlStr, c.ReadLimit), 0)
+ return res.Header, nil, fmt.Errorf("failed HTTP request with method: '%s', url: '%s', max bytes size: '%v' and error: %w", method, urlStr, c.ReadLimit, err)
}
if res.StatusCode < 200 || res.StatusCode > 299 {
- return res.Header, body, errors.Wrap(&StatusError{URL: urlStr, Body: string(body), Status: res.StatusCode}, 0)
+ return res.Header, body, fmt.Errorf("failed HTTP request with method: '%s' due to a status error: %w", method, &StatusError{URL: urlStr, Body: string(body), Status: res.StatusCode})
}
// Return the body in bytes and signal the status error if there was one
@@ -258,7 +256,7 @@ type StatusError struct {
// Error returns the StatusError as an error string.
func (e *StatusError) Error() string {
return fmt.Sprintf(
- "failed obtaining HTTP resource: %s as it gave an unsuccessful status code: %d. Body: %s",
+ "failed obtaining HTTP resource: '%s' as it gave an unsuccessful status code: '%d'. Body: '%s'",
e.URL,
e.Status,
e.Body,
diff --git a/internal/log/log.go b/internal/log/log.go
index 638f2ae..8d3fbfb 100644
--- a/internal/log/log.go
+++ b/internal/log/log.go
@@ -9,7 +9,6 @@ import (
"path"
"github.com/eduvpn/eduvpn-common/internal/util"
- "github.com/go-errors/errors"
)
// FileLogger defines the type of logger that this package implements
@@ -80,7 +79,7 @@ func (logger *FileLogger) Init(lvl Level, dir string) error {
0o666,
)
if err != nil {
- return errors.WrapPrefix(err, "failed creating log", 0)
+ return fmt.Errorf("failed creating log: %w", err)
}
multi := io.MultiWriter(os.Stdout, f)
log.SetOutput(multi)
diff --git a/internal/test/server.go b/internal/test/server.go
index 841b565..71f1d00 100644
--- a/internal/test/server.go
+++ b/internal/test/server.go
@@ -8,7 +8,6 @@ import (
"net/http/httptest"
httpw "github.com/eduvpn/eduvpn-common/internal/http"
- "github.com/go-errors/errors"
)
type Server struct {
@@ -28,7 +27,7 @@ func (srv *Server) Client() (*httpw.Client, error) {
for _, c := range srv.TLS.Certificates {
roots, err := x509.ParseCertificates(c.Certificate[len(c.Certificate)-1])
if err != nil {
- return nil, errors.WrapPrefix(err, "failed to parse root certificate", 0)
+ return nil, err
}
for _, root := range roots {
certs.AddCert(root)
diff --git a/internal/util/util.go b/internal/util/util.go
index ebda183..85c4b37 100644
--- a/internal/util/util.go
+++ b/internal/util/util.go
@@ -7,8 +7,6 @@ import (
"net/url"
"os"
"strings"
-
- "github.com/go-errors/errors"
)
// MakeRandomByteSlice creates a cryptographically random bytes slice of `size`
@@ -26,7 +24,7 @@ func EnsureDirectory(dir string) error {
// Create with 700 permissions, read, write, execute only for the owner
err := os.MkdirAll(dir, 0o700)
if err != nil {
- return errors.WrapPrefix(err, fmt.Sprintf("failed to create directory '%s'", dir), 0)
+ return fmt.Errorf("failed to create directory '%s' with error: %w", dir, err)
}
return nil
}
diff --git a/internal/verify/verify.go b/internal/verify/verify.go
index cd74a2b..14a7d78 100644
--- a/internal/verify/verify.go
+++ b/internal/verify/verify.go
@@ -4,7 +4,6 @@ package verify
import (
"fmt"
- "github.com/go-errors/errors"
"github.com/jedisct1/go-minisign"
)
@@ -63,19 +62,19 @@ func verifyWithKeys(
case "server_list.json", "organization_list.json":
break
default:
- return false, errors.Errorf(
+ return false, fmt.Errorf(
"invalid filename '%s'; expected 'server_list.json' or 'organization_list.json'",
filename)
}
sig, err := minisign.DecodeSignature(signatureFileContent)
if err != nil {
- return false, errors.WrapPrefix(err, "invalid signature format", 0)
+ return false, fmt.Errorf("invalid signature format with error: %w", err)
}
// Check if signature is prehashed, see https://jedisct1.github.io/minisign/#signature-format
if forcePrehash && sig.SignatureAlgorithm != [2]byte{'E', 'D'} {
- return false, errors.Errorf(
+ return false, fmt.Errorf(
"invalid signature algorithm '%s'; expected `ED (BLAKE2b-prehashed EdDSA)`",
sig.SignatureAlgorithm[:])
}
@@ -85,7 +84,7 @@ func verifyWithKeys(
key, err := minisign.NewPublicKey(keyStr)
if err != nil {
// Should only happen if Verify is wrong or extraKey is invalid
- return false, errors.WrapPrefix(err, fmt.Sprintf("failed to create public key '%s'", keyStr), 0)
+ return false, fmt.Errorf("failed to create public key '%s' and error: %w", keyStr, err)
}
if sig.KeyId != key.KeyId {
@@ -94,7 +93,7 @@ func verifyWithKeys(
valid, err := key.Verify(signedJSON, sig)
if !valid {
- return false, errors.WrapPrefix(err, "invalid signature", 0)
+ return false, fmt.Errorf("invalid signature with error: %w", err)
}
// Parse trusted comment
@@ -108,21 +107,21 @@ func verifyWithKeys(
&sigFileName,
)
if err != nil {
- return false, errors.WrapPrefix(err, fmt.Sprintf("invalid trusted comment '%s'", sig.TrustedComment), 0)
+ return false, fmt.Errorf("invalid trusted comment '%s' with error: %w", sig.TrustedComment, err)
}
if sigFileName != filename {
- return false, errors.Errorf("wrong filename '%s'; expected filename '%s' for signature",
+ return false, fmt.Errorf("wrong filename '%s'; expected filename '%s' for signature",
filename, sigFileName)
}
if signTime < minSignTime {
- return false, errors.Errorf("sign time %d is before sign tim: %d", signTime, minSignTime)
+ return false, fmt.Errorf("sign time %d is before sign tim: %d", signTime, minSignTime)
}
return true, nil
}
// No matching allowed key found
- return false, errors.Errorf("signature for filename '%s' was created with an unknown key", filename)
+ return false, fmt.Errorf("signature for filename '%s' was created with an unknown key", filename)
}