-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
43 lines (36 loc) · 911 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
39
40
41
42
43
package oygo
import (
"encoding/json"
"net/http"
)
type Error struct {
Message string `json:"message,omitempty"`
Url string `json:"url,omitempty"`
Status int `json:"status,omitempty"`
}
// Error returns error message.
// This enables oygo.Error to comply with Go error interface
func (e *Error) Error() string {
return e.Message
}
// GetStatus returns http status code
func (e *Error) GetStatus() int {
return e.Status
}
// FromGoErr generates oygo.Error from generic go errors
func FromGoErr(err error) *Error {
return &Error{
Status: http.StatusTeapot,
Message: err.Error(),
Url: "",
}
}
// FromHTTPErr generates oygo.Error from http errors with non 2xx status
func FromHTTPErr(status int, respBody []byte) *Error {
var httpError *Error
if err := json.Unmarshal(respBody, &httpError); err != nil {
return FromGoErr(err)
}
httpError.Status = status
return httpError
}