-
Notifications
You must be signed in to change notification settings - Fork 1
/
error.go
46 lines (39 loc) · 900 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
44
45
46
package biteship
import (
"encoding/json"
"log"
"net/http"
)
type Error struct {
Status int `json:"status,omitempty"`
ErrorCode string `json:"error_code,omitempty"`
Message string `json:"message,omitempty"`
RawError string `json:"error,omitempty"`
Code int `json:"code,omitempty"`
}
func (e *Error) Error() string {
return e.Message
}
func ErrorGo(err error) *Error {
return &Error{
Status: http.StatusInternalServerError,
ErrorCode: "Error Go",
Message: err.Error(),
}
}
func ErrorRequestParam(err error) *Error {
return &Error{
Status: http.StatusBadRequest,
ErrorCode: "Bad Request",
Message: err.Error(),
}
}
func ErrorHttp(status int, respBody []byte) *Error {
var httpError *Error
if err := json.Unmarshal(respBody, &httpError); err != nil {
log.Println(err)
return ErrorGo(err)
}
httpError.Status = status
return httpError
}