@@ -7,13 +7,15 @@ import (
77 "fmt"
88 "net/http"
99
10+ "github.com/deploymenttheory/go-api-http-client/internal/logger"
1011 "go.uber.org/zap"
1112)
1213
1314// APIError represents a structured API error response.
1415type APIError struct {
15- StatusCode int
16- Message string
16+ StatusCode int // HTTP status code
17+ Type string // A brief identifier for the type of error (e.g., "RateLimit", "BadRequest", etc.)
18+ Message string // Human-readable message
1719}
1820
1921// StructuredError represents a structured error response from the API.
@@ -24,19 +26,25 @@ type StructuredError struct {
2426 } `json:"error"`
2527}
2628
29+ // Error returns a string representation of the APIError.
30+ func (e * APIError ) Error () string {
31+ return fmt .Sprintf ("API Error (Type: %s, Code: %d): %s" , e .Type , e .StatusCode , e .Message )
32+ }
33+
2734// HandleAPIError handles error responses from the API, converting them into a structured error if possible.
28- func HandleAPIError (logger Logger , resp * http.Response ) error {
35+ func HandleAPIError (resp * http.Response , log logger. Logger ) error {
2936 var structuredErr StructuredError
3037 err := json .NewDecoder (resp .Body ).Decode (& structuredErr )
3138 if err == nil && structuredErr .Error .Message != "" {
3239 // Using structured logging to log the structured error details
33- c . Logger .Warn ("API returned structured error" ,
40+ log .Warn ("API returned structured error" ,
3441 zap .String ("status" , resp .Status ),
3542 zap .String ("error_code" , structuredErr .Error .Code ),
3643 zap .String ("error_message" , structuredErr .Error .Message ),
3744 )
3845 return & APIError {
3946 StatusCode : resp .StatusCode ,
47+ Type : structuredErr .Error .Code ,
4048 Message : structuredErr .Error .Message ,
4149 }
4250 }
@@ -46,29 +54,25 @@ func HandleAPIError(logger Logger, resp *http.Response) error {
4654 if err != nil || errMsg == "" {
4755 errMsg = fmt .Sprintf ("Unexpected error with status code: %d" , resp .StatusCode )
4856 // Logging with structured fields
49- c . Logger .Warn ("Failed to decode API error message, using default error message" ,
57+ log .Warn ("Failed to decode API error message, using default error message" ,
5058 zap .String ("status" , resp .Status ),
5159 zap .String ("error_message" , errMsg ),
5260 )
5361 } else {
5462 // Logging non-structured error as a warning with structured fields
55- c . Logger .Warn ("API returned non-structured error" ,
63+ log .Warn ("API returned non-structured error" ,
5664 zap .String ("status" , resp .Status ),
5765 zap .String ("error_message" , errMsg ),
5866 )
5967 }
6068
6169 return & APIError {
6270 StatusCode : resp .StatusCode ,
71+ Type : "UnexpectedError" ,
6372 Message : errMsg ,
6473 }
6574}
6675
67- // Error returns a string representation of the APIError.
68- func (e * APIError ) Error () string {
69- return fmt .Sprintf ("API Error (Code: %d): %s" , e .StatusCode , e .Message )
70- }
71-
7276// TranslateStatusCode provides a human-readable message for HTTP status codes.
7377func TranslateStatusCode (statusCode int ) string {
7478 messages := map [int ]string {
0 commit comments