From 6c8d37a05434d80de011517d48b73c7dc1845a3c Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Mon, 18 Nov 2024 22:25:49 +0100 Subject: [PATCH] Spruce up errors a bit --- go/daemon/daemon.go | 2 +- go/daemon/daemon_test.go | 4 ++-- go/daemon/errors.go | 10 +++++----- go/daemon/jsonrpc2/client_http.go | 5 ++++- go/daemon/jsonrpc2/client_rw.go | 5 ++++- go/daemon/rpc.go | 2 +- 6 files changed, 17 insertions(+), 11 deletions(-) diff --git a/go/daemon/daemon.go b/go/daemon/daemon.go index 481ca9a..970faa9 100644 --- a/go/daemon/daemon.go +++ b/go/daemon/daemon.go @@ -378,7 +378,7 @@ func (d *Daemon) SetConfig( d, func(ctx context.Context, n joinedNetwork) (struct{}, error) { if n.config != nil { - return struct{}{}, ErrUserManagedNetworkConfig + return struct{}{}, ErrManagedNetworkConfig } // TODO needs to check that public addresses aren't being shared diff --git a/go/daemon/daemon_test.go b/go/daemon/daemon_test.go index 58e2367..ee430a5 100644 --- a/go/daemon/daemon_test.go +++ b/go/daemon/daemon_test.go @@ -202,7 +202,7 @@ func TestDaemon_SetConfig(t *testing.T) { assert.NoError(t, err) }) - t.Run("ErrUserManagedNetworkConfig", func(t *testing.T) { + t.Run("ErrManagedNetworkConfig", func(t *testing.T) { var ( creationParams = bootstrap.NewCreationParams("AAA", "a.com") networkA = network.NewMockNetwork(t) @@ -222,6 +222,6 @@ func TestDaemon_SetConfig(t *testing.T) { networkConfig.VPN.Tun.Device = "foo" err := h.daemon.SetConfig(h.ctx, networkConfig) - assert.ErrorIs(t, err, ErrUserManagedNetworkConfig) + assert.ErrorIs(t, err, ErrManagedNetworkConfig) }) } diff --git a/go/daemon/errors.go b/go/daemon/errors.go index 1e4b2ce..561aa9f 100644 --- a/go/daemon/errors.go +++ b/go/daemon/errors.go @@ -10,7 +10,7 @@ const ( errCodeAlreadyJoined errCodeNoMatchingNetworks errCodeMultipleMatchingNetworks - errCodeUserManagedNetworkConfig + errCodeManagedNetworkConfig ) var ( @@ -35,10 +35,10 @@ var ( "Multiple networks matched the search string", ) - // ErrUserManagedNetworkConfig is returned when attempting to modify a + // ErrManagedNetworkConfig is returned when attempting to modify a // network config which is managed by the user. - ErrUserManagedNetworkConfig = jsonrpc2.NewError( - errCodeUserManagedNetworkConfig, - "Network configuration is managed by the user", + ErrManagedNetworkConfig = jsonrpc2.NewError( + errCodeManagedNetworkConfig, + "Network configuration is managed by the daemon.yml", ) ) diff --git a/go/daemon/jsonrpc2/client_http.go b/go/daemon/jsonrpc2/client_http.go index 74fea17..5e5529d 100644 --- a/go/daemon/jsonrpc2/client_http.go +++ b/go/daemon/jsonrpc2/client_http.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "errors" "fmt" "net/http" "net/url" @@ -75,7 +76,9 @@ func (c *httpClient) Call( dec := json.NewDecoder(res.Body) resID, err := decodeResponse(dec, rcv) - if err != nil { + if errors.As(err, new(Error)) { + return err + } else if err != nil { return fmt.Errorf("decoding response: %w", err) } else if resID != id { return fmt.Errorf( diff --git a/go/daemon/jsonrpc2/client_rw.go b/go/daemon/jsonrpc2/client_rw.go index 25c6454..c561cb2 100644 --- a/go/daemon/jsonrpc2/client_rw.go +++ b/go/daemon/jsonrpc2/client_rw.go @@ -3,6 +3,7 @@ package jsonrpc2 import ( "context" "encoding/json" + "errors" "fmt" "io" ) @@ -27,7 +28,9 @@ func (c rwClient) Call( } resID, err := decodeResponse(c.dec, rcv) - if err != nil { + if errors.As(err, new(Error)) { + return err + } else if err != nil { return fmt.Errorf("decoding response: %w", err) } else if resID != id { return fmt.Errorf( diff --git a/go/daemon/rpc.go b/go/daemon/rpc.go index f7ea426..63676f7 100644 --- a/go/daemon/rpc.go +++ b/go/daemon/rpc.go @@ -29,7 +29,7 @@ type RPC interface { GetNetworks(context.Context) ([]bootstrap.CreationParams, error) // SetConfig extends the [network.RPC] method of the same name such that - // [ErrUserManagedNetworkConfig] is returned if the picked network is + // [ErrManagedNetworkConfig] is returned if the picked network is // configured as part of the [daecommon.Config] which the Daemon was // initialized with. //