@@ -34,53 +34,47 @@ func (e *APIError) Error() string {
3434 return fmt .Sprintf ("API Error (Type: %s, Code: %d): %s" , e .Type , e .StatusCode , e .Message )
3535}
3636
37- // handleAPIErrorResponse attempts to parse the error response from the API and logs using zap logger.
37+ // handleAPIErrorResponse attempts to parse the error response from the API and logs using the zap logger.
3838func handleAPIErrorResponse (resp * http.Response , log logger.Logger ) * APIError {
39- // Initialize apiError with the HTTP status code and other fields
4039 apiError := & APIError {
4140 StatusCode : resp .StatusCode ,
4241 Type : "APIError" , // Default error type
4342 Message : "An error occurred" , // Default error message
4443 }
4544
46- // Read the response body
4745 bodyBytes , err := io .ReadAll (resp .Body )
4846 if err != nil {
4947 // Log and return an error if reading the body fails
50- log .LogError ("READ" , resp .Request .URL .String (), resp .StatusCode , err , "Failed to read API error response body " )
48+ log .LogError ("api_response_read_error" , " READ" , resp .Request .URL .String (), resp .StatusCode , err , "" )
5149 return apiError
5250 }
5351
54- // Attempt to parse the response into a StructuredError
5552 if err := json .Unmarshal (bodyBytes , & apiError ); err == nil && apiError .Message != "" {
56- // Log the structured error with consistency
57- log .LogError ("API" , resp .Request .URL .String (), resp .StatusCode , fmt .Errorf (apiError .Message ), "" )
53+ // Log the structured error
54+ log .LogError ("api_structured_error" , " API" , resp .Request .URL .String (), resp .StatusCode , fmt .Errorf (apiError .Message ), "" )
5855 return apiError
5956 }
6057
6158 // If structured parsing fails, attempt to parse into a generic error map
6259 var genericErr map [string ]interface {}
6360 if err := json .Unmarshal (bodyBytes , & genericErr ); err == nil {
64- // Extract fields from the generic error map and update apiError accordingly
6561 apiError .updateFromGenericError (genericErr )
66-
67- // Log the error with extracted details consistently
68- log .LogError ("API" , resp .Request .URL .String (), resp .StatusCode , fmt .Errorf (apiError .Message ), "" )
62+ // Log the error with extracted details
63+ log .LogError ("api_generic_error" , "API" , resp .Request .URL .String (), resp .StatusCode , fmt .Errorf (apiError .Message ), "" )
6964 return apiError
7065 }
7166
7267 // If all parsing attempts fail, log the raw response
73- log .LogError ("API" , resp .Request .URL .String (), resp .StatusCode , fmt .Errorf ("failed to parse API error response" ), string (bodyBytes ))
68+ log .LogError ("api_unexpected_error" , " API" , resp .Request .URL .String (), resp .StatusCode , fmt .Errorf ("failed to parse API error response" ), string (bodyBytes ))
7469 return apiError
7570}
7671
77- // updateFromGenericError updates the APIError fields based on a generic error map
7872func (e * APIError ) updateFromGenericError (genericErr map [string ]interface {}) {
7973 if msg , ok := genericErr ["message" ].(string ); ok {
8074 e .Message = msg
8175 }
8276 if detail , ok := genericErr ["detail" ].(string ); ok {
8377 e .Detail = detail
8478 }
85- // Add more fields if necessary
79+ // Add more fields as needed
8680}
0 commit comments