feat(bookstack-api-vl3): add unit tests for error types and APIError
Test APIError.Error() formatting, status code to sentinel error mapping via Is(), and error matching through fmt.Errorf wrapping.
This commit is contained in:
parent
4875540f21
commit
9478a9d36e
3 changed files with 68 additions and 5 deletions
63
errors_test.go
Normal file
63
errors_test.go
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
package bookstack
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAPIError_Error(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
err APIError
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "with code",
|
||||
err: APIError{StatusCode: 404, Code: "not_found", Message: "Page not found"},
|
||||
want: "bookstack API error (status 404, code not_found): Page not found",
|
||||
},
|
||||
{
|
||||
name: "without code",
|
||||
err: APIError{StatusCode: 500, Message: "Internal error"},
|
||||
want: "bookstack API error (status 500): Internal error",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := tt.err.Error(); got != tt.want {
|
||||
t.Errorf("Error() = %q, want %q", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAPIError_Is(t *testing.T) {
|
||||
tests := []struct {
|
||||
status int
|
||||
target error
|
||||
want bool
|
||||
}{
|
||||
{400, ErrBadRequest, true},
|
||||
{401, ErrUnauthorized, true},
|
||||
{403, ErrForbidden, true},
|
||||
{404, ErrNotFound, true},
|
||||
{429, ErrRateLimited, true},
|
||||
{500, ErrNotFound, false},
|
||||
{200, ErrBadRequest, false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
apiErr := &APIError{StatusCode: tt.status}
|
||||
if got := errors.Is(apiErr, tt.target); got != tt.want {
|
||||
t.Errorf("errors.Is(APIError{%d}, %v) = %v, want %v", tt.status, tt.target, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAPIError_Is_Wrapped(t *testing.T) {
|
||||
inner := &APIError{StatusCode: 404, Message: "not found"}
|
||||
wrapped := fmt.Errorf("fetching page: %w", inner)
|
||||
if !errors.Is(wrapped, ErrNotFound) {
|
||||
t.Error("expected errors.Is to match ErrNotFound through wrapping")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue