diff --git a/pkg/client/artifacts_test.go b/pkg/client/artifacts_test.go index 1a43b56..782dd30 100644 --- a/pkg/client/artifacts_test.go +++ b/pkg/client/artifacts_test.go @@ -25,7 +25,10 @@ func TestArtifactsService_Create(t *testing.T) { t.Errorf("method = %v, want POST", r.Method) } 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() @@ -50,7 +53,10 @@ func TestArtifactsService_Get(t *testing.T) { t.Errorf("method = %v, want GET", r.Method) } 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() @@ -99,7 +105,10 @@ func TestArtifactsService_List(t *testing.T) { "results": []models.Artifact{{ID: uuid.New()}}, "count": 1, } - json.NewEncoder(w).Encode(resp) + err := json.NewEncoder(w).Encode(resp) + if err != nil { + return + } })) defer server.Close() @@ -118,7 +127,10 @@ func TestArtifactsService_List(t *testing.T) { func TestArtifactsService_Count(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") - w.Write([]byte(`5`)) + _, err := w.Write([]byte(`5`)) + if err != nil { + return + } })) defer server.Close() diff --git a/pkg/errors/errors.go b/pkg/errors/errors.go index bf56c2a..7d76c95 100644 --- a/pkg/errors/errors.go +++ b/pkg/errors/errors.go @@ -3,6 +3,7 @@ package errors import ( "encoding/json" + "errors" "fmt" "io" "net/http" @@ -98,7 +99,12 @@ func NewAPIError(resp *http.Response) error { // Try to read and parse the response body 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) if err == nil && len(body) > 0 { // 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. func IsNotFound(err error) bool { - if apiErr, ok := err.(*APIError); ok { + var apiErr *APIError + if errors.As(err, &apiErr) { return apiErr.IsNotFound() } return false @@ -189,7 +196,8 @@ func IsNotFound(err error) bool { // IsUnauthorized checks if an error is a 401 Unauthorized error. func IsUnauthorized(err error) bool { - if apiErr, ok := err.(*APIError); ok { + var apiErr *APIError + if errors.As(err, &apiErr) { return apiErr.IsUnauthorized() } return false @@ -197,7 +205,8 @@ func IsUnauthorized(err error) bool { // IsForbidden checks if an error is a 403 Forbidden error. func IsForbidden(err error) bool { - if apiErr, ok := err.(*APIError); ok { + var apiErr *APIError + if errors.As(err, &apiErr) { return apiErr.IsForbidden() } return false @@ -205,7 +214,8 @@ func IsForbidden(err error) bool { // IsRateLimited checks if an error is a 429 Too Many Requests error. func IsRateLimited(err error) bool { - if apiErr, ok := err.(*APIError); ok { + var apiErr *APIError + if errors.As(err, &apiErr) { return apiErr.IsRateLimited() } return false @@ -213,7 +223,8 @@ func IsRateLimited(err error) bool { // IsServerError checks if an error is a 5xx server error. func IsServerError(err error) bool { - if apiErr, ok := err.(*APIError); ok { + var apiErr *APIError + if errors.As(err, &apiErr) { return apiErr.IsServerError() } return false @@ -221,6 +232,7 @@ func IsServerError(err error) bool { // IsValidationError checks if an error is a validation error. func IsValidationError(err error) bool { - _, ok := err.(*ValidationError) + var validationError *ValidationError + ok := errors.As(err, &validationError) return ok }