Refactor error handling with errors.As for type assertions; add error checks in test encoders and body closures

This commit is contained in:
Gregor Schulte
2026-04-08 16:10:09 +02:00
parent 2dcc0b13dd
commit 9b70c13556
2 changed files with 35 additions and 11 deletions

View File

@@ -25,7 +25,10 @@ func TestArtifactsService_Create(t *testing.T) {
t.Errorf("method = %v, want POST", r.Method) t.Errorf("method = %v, want POST", r.Method)
} }
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(expected) err := json.NewEncoder(w).Encode(expected)
if err != nil {
return
}
})) }))
defer server.Close() defer server.Close()
@@ -50,7 +53,10 @@ func TestArtifactsService_Get(t *testing.T) {
t.Errorf("method = %v, want GET", r.Method) t.Errorf("method = %v, want GET", r.Method)
} }
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(expected) err := json.NewEncoder(w).Encode(expected)
if err != nil {
return
}
})) }))
defer server.Close() defer server.Close()
@@ -99,7 +105,10 @@ func TestArtifactsService_List(t *testing.T) {
"results": []models.Artifact{{ID: uuid.New()}}, "results": []models.Artifact{{ID: uuid.New()}},
"count": 1, "count": 1,
} }
json.NewEncoder(w).Encode(resp) err := json.NewEncoder(w).Encode(resp)
if err != nil {
return
}
})) }))
defer server.Close() defer server.Close()
@@ -118,7 +127,10 @@ func TestArtifactsService_List(t *testing.T) {
func TestArtifactsService_Count(t *testing.T) { func TestArtifactsService_Count(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`5`)) _, err := w.Write([]byte(`5`))
if err != nil {
return
}
})) }))
defer server.Close() defer server.Close()

View File

@@ -3,6 +3,7 @@ package errors
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@@ -98,7 +99,12 @@ func NewAPIError(resp *http.Response) error {
// Try to read and parse the response body // Try to read and parse the response body
if resp.Body != nil { if resp.Body != nil {
defer resp.Body.Close() defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
return
}
}(resp.Body)
body, err := io.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err == nil && len(body) > 0 { if err == nil && len(body) > 0 {
// Try to parse as JSON // Try to parse as JSON
@@ -181,7 +187,8 @@ func parseValidationError(apiErr *APIError, detail interface{}) error {
// IsNotFound checks if an error is a 404 Not Found error. // IsNotFound checks if an error is a 404 Not Found error.
func IsNotFound(err error) bool { func IsNotFound(err error) bool {
if apiErr, ok := err.(*APIError); ok { var apiErr *APIError
if errors.As(err, &apiErr) {
return apiErr.IsNotFound() return apiErr.IsNotFound()
} }
return false return false
@@ -189,7 +196,8 @@ func IsNotFound(err error) bool {
// IsUnauthorized checks if an error is a 401 Unauthorized error. // IsUnauthorized checks if an error is a 401 Unauthorized error.
func IsUnauthorized(err error) bool { func IsUnauthorized(err error) bool {
if apiErr, ok := err.(*APIError); ok { var apiErr *APIError
if errors.As(err, &apiErr) {
return apiErr.IsUnauthorized() return apiErr.IsUnauthorized()
} }
return false return false
@@ -197,7 +205,8 @@ func IsUnauthorized(err error) bool {
// IsForbidden checks if an error is a 403 Forbidden error. // IsForbidden checks if an error is a 403 Forbidden error.
func IsForbidden(err error) bool { func IsForbidden(err error) bool {
if apiErr, ok := err.(*APIError); ok { var apiErr *APIError
if errors.As(err, &apiErr) {
return apiErr.IsForbidden() return apiErr.IsForbidden()
} }
return false return false
@@ -205,7 +214,8 @@ func IsForbidden(err error) bool {
// IsRateLimited checks if an error is a 429 Too Many Requests error. // IsRateLimited checks if an error is a 429 Too Many Requests error.
func IsRateLimited(err error) bool { func IsRateLimited(err error) bool {
if apiErr, ok := err.(*APIError); ok { var apiErr *APIError
if errors.As(err, &apiErr) {
return apiErr.IsRateLimited() return apiErr.IsRateLimited()
} }
return false return false
@@ -213,7 +223,8 @@ func IsRateLimited(err error) bool {
// IsServerError checks if an error is a 5xx server error. // IsServerError checks if an error is a 5xx server error.
func IsServerError(err error) bool { func IsServerError(err error) bool {
if apiErr, ok := err.(*APIError); ok { var apiErr *APIError
if errors.As(err, &apiErr) {
return apiErr.IsServerError() return apiErr.IsServerError()
} }
return false return false
@@ -221,6 +232,7 @@ func IsServerError(err error) bool {
// IsValidationError checks if an error is a validation error. // IsValidationError checks if an error is a validation error.
func IsValidationError(err error) bool { func IsValidationError(err error) bool {
_, ok := err.(*ValidationError) var validationError *ValidationError
ok := errors.As(err, &validationError)
return ok return ok
} }