-
Notifications
You must be signed in to change notification settings - Fork 3
/
errors.go
60 lines (49 loc) · 1.23 KB
/
errors.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package solus
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
)
// HTTPError represents errors occurred when some action failed due to some problem
// with request.
type HTTPError struct {
Method string
Path string
HTTPCode int `json:"http_code"`
Message string `json:"message"`
Errors map[string][]string `json:"errors"`
}
func (e HTTPError) Error() string {
buf := bytes.NewBufferString(fmt.Sprintf("HTTP %s %s returns %d status code", e.Method, e.Path, e.HTTPCode))
if len(e.Errors) > 0 {
buf.WriteString(" with errors")
}
if e.Message != "" {
//goland:noinspection GrazieInspection
buf.WriteString(fmt.Sprintf(": %s", e.Message))
}
return buf.String()
}
func newHTTPError(method, path string, httpCode int, body []byte) error {
e := HTTPError{
Method: method,
Path: path,
HTTPCode: httpCode,
}
if err := json.Unmarshal(body, &e); err != nil {
e.Message = string(body)
return e
}
return e
}
// IsNotFound returns true if specified error is produced 'cause requested resource
// is not found.
func IsNotFound(err error) bool {
var httpErr HTTPError
if !errors.As(err, &httpErr) {
return false
}
return httpErr.HTTPCode == http.StatusNotFound
}