@@ -32,11 +32,11 @@ func (e *APIError) Error() string {
32
32
}
33
33
34
34
// HandleAPIError handles error responses from the API, converting them into a structured error if possible.
35
- func HandleAPIError (resp * http.Response , log logger.Logger ) error {
35
+ func HandleAPIError (resp * http.Response , log logger.Logger ) * APIError {
36
36
var structuredErr StructuredError
37
37
err := json .NewDecoder (resp .Body ).Decode (& structuredErr )
38
38
if err == nil && structuredErr .Error .Message != "" {
39
- // Using structured logging to log the structured error details
39
+ // Log the structured error details
40
40
log .Warn ("API returned structured error" ,
41
41
zap .String ("status" , resp .Status ),
42
42
zap .String ("error_code" , structuredErr .Error .Code ),
@@ -49,22 +49,12 @@ func HandleAPIError(resp *http.Response, log logger.Logger) error {
49
49
}
50
50
}
51
51
52
- var errMsg string
53
- err = json .NewDecoder (resp .Body ).Decode (& errMsg )
54
- if err != nil || errMsg == "" {
55
- errMsg = fmt .Sprintf ("Unexpected error with status code: %d" , resp .StatusCode )
56
- // Logging with structured fields
57
- log .Error ("Failed to decode API error message, using default error message" ,
58
- zap .String ("status" , resp .Status ),
59
- zap .String ("error_message" , errMsg ),
60
- )
61
- } else {
62
- // Logging non-structured error as a warning with structured fields
63
- log .Warn ("API returned non-structured error" ,
64
- zap .String ("status" , resp .Status ),
65
- zap .String ("error_message" , errMsg ),
66
- )
67
- }
52
+ // Default error message for non-structured responses or decode failures
53
+ errMsg := fmt .Sprintf ("Unexpected error with status code: %d" , resp .StatusCode )
54
+ log .Error ("Failed to decode API error message, using default error message" ,
55
+ zap .String ("status" , resp .Status ),
56
+ zap .String ("error_message" , errMsg ),
57
+ )
68
58
69
59
return & APIError {
70
60
StatusCode : resp .StatusCode ,
0 commit comments