Skip to content

Commit

Permalink
Add more details to Crisp REST API errors
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriansaliou committed Nov 11, 2024
1 parent a766dd9 commit fdd2f45
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions crisp/crisp.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,25 @@ type Response struct {

type errorResponse struct {
Response *http.Response
Reason string `json:"reason,omitempty"`
Reason string `json:"reason,omitempty"`
Data *interface{} `json:"data,omitempty"`
}


// Error prints an error response
func (response *errorResponse) Error() string {
return fmt.Sprintf("%v %v: %d %v",
response.Response.Request.Method, response.Response.Request.URL,
response.Response.StatusCode, response.Reason)
// Marshal error data to a JSON string (if any)
var errorData []byte

if response.Data != nil {
errorData, _ = json.Marshal(*response.Data)
} else {
errorData = []byte("{}")
}

return fmt.Sprintf("HTTP %d %v\nPath → %v %v\nData → %s",
response.Response.StatusCode, response.Reason,
response.Response.Request.Method, response.Response.Request.URL, errorData)
}


Expand Down

0 comments on commit fdd2f45

Please sign in to comment.