-
Notifications
You must be signed in to change notification settings - Fork 8
/
error.go
38 lines (32 loc) · 1009 Bytes
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package client
import (
"encoding/json"
"fmt"
"net/http"
)
// ResponseError is a response from the API that indicates an error.
type ResponseError struct {
Request *http.Request `json:"-"`
Response *http.Response `json:"-"`
ErrorData struct {
Code int `json:"code"`
Message string `json:"message"`
Validation map[string]string `json:"validation"`
} `json:"error"`
Debug struct {
Source string `json:"source"`
} `json:"debug"`
}
func (r ResponseError) Error() string {
return fmt.Sprintf("received error from api: %+v", r.ErrorData)
}
func parseEngineError(req *http.Request, res *http.Response) error {
if res.StatusCode < http.StatusOK || res.StatusCode >= http.StatusMultipleChoices {
errResponse := ResponseError{Request: req, Response: res}
if decodeErr := json.NewDecoder(res.Body).Decode(&errResponse); decodeErr != nil {
return fmt.Errorf("could not decode error response: %w", decodeErr)
}
return &errResponse
}
return nil
}