Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(error): handle wrapped errors in IsError() #374

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion hcloud/error.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hcloud

import (
"errors"
"fmt"
"net"
)
Expand Down Expand Up @@ -120,7 +121,8 @@ type ErrorDetailsInvalidInputField struct {

// IsError returns whether err is an API error with the given error code.
func IsError(err error, code ErrorCode) bool {
apiErr, ok := err.(Error)
var apiErr Error
ok := errors.As(err, &apiErr)
return ok && apiErr.Code == code
}

Expand Down
101 changes: 101 additions & 0 deletions hcloud/error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package hcloud

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestError_Error(t *testing.T) {
type fields struct {
Code ErrorCode
Message string
Details interface{}
response *Response
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "simple error",
fields: fields{
Code: ErrorCodeUnauthorized,
Message: "unable to authenticate",
},
want: "unable to authenticate (unauthorized)",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := Error{
Code: tt.fields.Code,
Message: tt.fields.Message,
Details: tt.fields.Details,
response: tt.fields.response,
}
assert.Equalf(t, tt.want, e.Error(), "Error()")
})
}
}

func TestIsError(t *testing.T) {
type args struct {
err error
code ErrorCode
}
tests := []struct {
name string
args args
want bool
}{
{
name: "hcloud error with code",
args: args{
err: Error{Code: ErrorCodeUnauthorized},
code: ErrorCodeUnauthorized,
},
want: true,
},
{
name: "hcloud error with different code",
args: args{
err: Error{Code: ErrorCodeConflict},
code: ErrorCodeUnauthorized,
},
want: false,
},
{
name: "wrapped hcloud error with code",
args: args{
err: fmt.Errorf("wrapped: %w", Error{Code: ErrorCodeUnauthorized}),
code: ErrorCodeUnauthorized,
},
want: true,
},
{
name: "wrapped hcloud error with different code",
args: args{
err: fmt.Errorf("wrapped: %w", Error{Code: ErrorCodeConflict}),
code: ErrorCodeUnauthorized,
},
want: false,
},
{
name: "non-hcloud error",
args: args{
err: fmt.Errorf("something went wrong"),
code: ErrorCodeUnauthorized,
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, IsError(tt.args.err, tt.args.code), "IsError(%v, %v)", tt.args.err, tt.args.code)
})
}
}
Loading