diff --git a/internal/http_executor/http.go b/internal/http_executor/http.go index 08358d6..bcb0fd5 100644 --- a/internal/http_executor/http.go +++ b/internal/http_executor/http.go @@ -101,15 +101,18 @@ func (c *Client) handleErrorResponse(resp *http.Response) error { return fmt.Errorf("reading error response body failed: %v", err) } - var apiError struct { - Message string `json:"message"` + var errorMessageMap map[string]interface{} + err = json.Unmarshal(body, &errorMessageMap) + if err != nil { + return fmt.Errorf("failed to unmarshal error response body: %v", err) } - if err := json.Unmarshal(body, &apiError); err != nil { - // Fallback to raw body text if the body cannot be parsed as JSON - return fmt.Errorf("HTTP %d: %s", resp.StatusCode, body) + + errFormatted, err := json.MarshalIndent(errorMessageMap, "", " ") + if err != nil { + return fmt.Errorf("failed to format error response body: %v", err) } - return fmt.Errorf("HTTP %d: %s", resp.StatusCode, apiError.Message) + return fmt.Errorf("%s", errFormatted) } // addQueryParameters adds the parameters in the struct params as URL query parameters to s. diff --git a/internal/http_executor/http_test.go b/internal/http_executor/http_test.go index 45d9959..d4f3acc 100644 --- a/internal/http_executor/http_test.go +++ b/internal/http_executor/http_test.go @@ -7,6 +7,7 @@ import ( "net/http" "net/http/httptest" "net/url" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -126,8 +127,12 @@ func TestExecuteRequest_ServerErrorPOST(t *testing.T) { t.Fatalf("Expected an error, got nil") } - expectedErrorMessage := "processing response failed: HTTP 500: internal server error" - if err.Error() != expectedErrorMessage { + expectedErrorMessage := `processing response failed: { + "message": "internal server error" +}` + + // Remove all whitespace when comparing the errors + if strings.Join(strings.Fields(err.Error()), "") != strings.Join(strings.Fields(expectedErrorMessage), "") { t.Errorf("Expected error message '%s', got '%s'", expectedErrorMessage, err.Error()) } }