package api import ( "errors" "testing" "time" ) func TestAPIError_Error(t *testing.T) { tests := []struct { name string err *APIError want string }{ { name: "with message", err: &APIError{StatusCode: 403, ErrorType: "AuthRequired", Message: "token expired"}, want: "AuthRequired: token expired (HTTP 403)", }, { name: "without message", err: &APIError{StatusCode: 500, ErrorType: "InternalServerError"}, want: "InternalServerError (HTTP 500)", }, } 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_StatusChecks(t *testing.T) { e404 := &APIError{StatusCode: 404} e403 := &APIError{StatusCode: 403} e401 := &APIError{StatusCode: 401} e500 := &APIError{StatusCode: 500} if !e404.IsNotFound() { t.Error("404 should be not found") } if e500.IsNotFound() { t.Error("500 should not be not found") } if !e403.IsForbidden() { t.Error("403 should be forbidden") } if !e401.IsUnauthorized() { t.Error("401 should be unauthorized") } } func TestRateLimitError(t *testing.T) { inner := &APIError{StatusCode: 429, ErrorType: "RateLimited"} rl := &RateLimitError{RetryAfter: 30 * time.Second, Inner: inner} if rl.Error() != "rate limited: retry after 30s" { t.Errorf("Error() = %q", rl.Error()) } var apiErr *APIError if !errors.As(rl, &apiErr) { t.Error("RateLimitError should unwrap to APIError") } if apiErr.StatusCode != 429 { t.Errorf("unwrapped status = %d, want 429", apiErr.StatusCode) } }