Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Use wrapped static errors#343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
lucacome merged 2 commits intomainfromchore/static-errors
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions.golangci.yml
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -32,6 +32,7 @@ linters-settings:
linters:
enable:
- asasalint
- err113
- asciicheck
- bidichk
- contextcheck
Expand Down
39 changes: 23 additions & 16 deletionsclient/nginx.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 supportedby the client", c.apiVersion)
return nil, fmt.Errorf("API version %v: %wby the client", c.apiVersion, ErrNotSupported)
}

if c.ctxTimeout <= 0 {
return nil, fmt.Errorf("timeouthasto be greater than 0 %v", c.ctxTimeout)
return nil, fmt.Errorf("timeout%q needsto 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 supportedby the server", c.apiVersion)
return nil, fmt.Errorf("API version %v: %wby the server", c.apiVersion, ErrNotSupported)
}
}

Expand DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand Up@@ -1594,7 +1601,7 @@ func (client *NginxClient) addKeyValPair(zone string, key string, val string, st
base = "stream"
}
if zone == "" {
returnerrors.New("zone required")
returnfmt.Errorf("zone: %w", ErrParameterRequired)
}

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

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

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

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

[8]ページ先頭

©2009-2025 Movatter.jp