Skip to content

Commit

Permalink
Use wrapped static errors (#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacome authored Aug 15, 2024
1 parent 2d16357 commit 1f669d1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ linters-settings:
linters:
enable:
- asasalint
- err113
- asciicheck
- bidichk
- contextcheck
Expand Down
39 changes: 23 additions & 16 deletions client/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ var (
defaultTimeout = 10 * time.Second
)

// ErrUnsupportedVer means that client's API version is not supported by NGINX plus API.
var ErrUnsupportedVer = errors.New("API version of the client is not supported by running NGINX Plus")
var (
ErrParameterRequired = errors.New("parameter is required")
ErrServerNotFound = errors.New("server not found")
ErrServerExists = errors.New("server already exists")
ErrNotSupported = errors.New("not supported")
ErrInvalidTimeout = errors.New("invalid timeout")
)

// NginxClient lets you access NGINX Plus API.
type NginxClient struct {
Expand Down Expand Up @@ -570,15 +575,15 @@ func NewNginxClient(apiEndpoint string, opts ...Option) (*NginxClient, error) {
}

if c.httpClient == nil {
return nil, errors.New("http client is not set")
return nil, fmt.Errorf("http client: %w", ErrParameterRequired)
}

if !versionSupported(c.apiVersion) {
return nil, fmt.Errorf("API version %v is not supported by the client", c.apiVersion)
return nil, fmt.Errorf("API version %v: %w by the client", c.apiVersion, ErrNotSupported)
}

if c.ctxTimeout <= 0 {
return nil, fmt.Errorf("timeout has to be greater than 0 %v", c.ctxTimeout)
return nil, fmt.Errorf("timeout %q needs to be greater than 0: %w", c.ctxTimeout, ErrInvalidTimeout)
}

if c.checkAPI {
Expand All @@ -594,7 +599,7 @@ func NewNginxClient(apiEndpoint string, opts ...Option) (*NginxClient, error) {
}
}
if !found {
return nil, fmt.Errorf("API version %v is not supported by the server", c.apiVersion)
return nil, fmt.Errorf("API version %v: %w by the server", c.apiVersion, ErrNotSupported)
}
}

Expand Down Expand Up @@ -642,7 +647,9 @@ func (client *NginxClient) getAPIVersions(httpClient *http.Client, endpoint stri
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%v is not accessible: expected %v response, got %v", endpoint, http.StatusOK, resp.StatusCode)
return nil, createResponseMismatchError(resp.Body).Wrap(fmt.Sprintf(
"failed to get endpoint %q, expected %v response, got %v",
endpoint, http.StatusOK, resp.StatusCode))
}

body, err := io.ReadAll(resp.Body)
Expand Down Expand Up @@ -714,7 +721,7 @@ func (client *NginxClient) AddHTTPServer(upstream string, server UpstreamServer)
return fmt.Errorf("failed to add %v server to %v upstream: %w", server.Server, upstream, err)
}
if id != -1 {
return fmt.Errorf("failed to add %v server to %v upstream: server already exists", server.Server, upstream)
return fmt.Errorf("failed to add %v server to %v upstream: %w", server.Server, upstream, ErrServerExists)
}

path := fmt.Sprintf("http/upstreams/%v/servers/", upstream)
Expand All @@ -733,7 +740,7 @@ func (client *NginxClient) DeleteHTTPServer(upstream string, server string) erro
return fmt.Errorf("failed to remove %v server from %v upstream: %w", server, upstream, err)
}
if id == -1 {
return fmt.Errorf("failed to remove %v server from %v upstream: server doesn't exist", server, upstream)
return fmt.Errorf("failed to remove %v server from %v upstream: %w", server, upstream, ErrServerNotFound)
}

path := fmt.Sprintf("http/upstreams/%v/servers/%v", upstream, id)
Expand Down Expand Up @@ -1028,7 +1035,7 @@ func (client *NginxClient) AddStreamServer(upstream string, server StreamUpstrea
return fmt.Errorf("failed to add %v stream server to %v upstream: %w", server.Server, upstream, err)
}
if id != -1 {
return fmt.Errorf("failed to add %v stream server to %v upstream: server already exists", server.Server, upstream)
return fmt.Errorf("failed to add %v stream server to %v upstream: %w", server.Server, upstream, ErrServerExists)
}

path := fmt.Sprintf("stream/upstreams/%v/servers/", upstream)
Expand All @@ -1046,7 +1053,7 @@ func (client *NginxClient) DeleteStreamServer(upstream string, server string) er
return fmt.Errorf("failed to remove %v stream server from %v upstream: %w", server, upstream, err)
}
if id == -1 {
return fmt.Errorf("failed to remove %v stream server from %v upstream: server doesn't exist", server, upstream)
return fmt.Errorf("failed to remove %v stream server from %v upstream: %w", server, upstream, ErrServerNotFound)
}

path := fmt.Sprintf("stream/upstreams/%v/servers/%v", upstream, id)
Expand Down Expand Up @@ -1541,7 +1548,7 @@ func (client *NginxClient) getKeyValPairs(zone string, stream bool) (KeyValPairs
base = "stream"
}
if zone == "" {
return nil, errors.New("zone required")
return nil, fmt.Errorf("zone: %w", ErrParameterRequired)
}

path := fmt.Sprintf("%v/keyvals/%v", base, zone)
Expand Down Expand Up @@ -1594,7 +1601,7 @@ func (client *NginxClient) addKeyValPair(zone string, key string, val string, st
base = "stream"
}
if zone == "" {
return errors.New("zone required")
return fmt.Errorf("zone: %w", ErrParameterRequired)
}

path := fmt.Sprintf("%v/keyvals/%v", base, zone)
Expand Down Expand Up @@ -1622,7 +1629,7 @@ func (client *NginxClient) modifyKeyValPair(zone string, key string, val string,
base = "stream"
}
if zone == "" {
return errors.New("zone required")
return fmt.Errorf("zone: %w", ErrParameterRequired)
}

path := fmt.Sprintf("%v/keyvals/%v", base, zone)
Expand Down Expand Up @@ -1652,7 +1659,7 @@ func (client *NginxClient) deleteKeyValuePair(zone string, key string, stream bo
base = "stream"
}
if zone == "" {
return errors.New("zone required")
return fmt.Errorf("zone: %w", ErrParameterRequired)
}

// map[string]string can't have a nil value so we use a different type here.
Expand Down Expand Up @@ -1683,7 +1690,7 @@ func (client *NginxClient) deleteKeyValPairs(zone string, stream bool) error {
base = "stream"
}
if zone == "" {
return errors.New("zone required")
return fmt.Errorf("zone: %w", ErrParameterRequired)
}

path := fmt.Sprintf("%v/keyvals/%v", base, zone)
Expand Down

0 comments on commit 1f669d1

Please sign in to comment.