forked from xendit/xendit-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
57 lines (48 loc) · 1.35 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
package xendit
import (
"encoding/json"
"net/http"
)
// Contains constants for the ErrorCode in xendit.Error
const (
// APIValidationErrCode error code for parameters validation
APIValidationErrCode string = "API_VALIDATION_ERROR"
// GoErrCode error code for errors happen inside Go code
GoErrCode string = "GO_ERROR"
)
// Error is the conventional Xendit error
type Error struct {
Status int `json:"status,omitempty"`
ErrorCode string `json:"error_code,omitempty"`
Message string `json:"message,omitempty"`
}
// Error returns error message.
// This enables xendit.Error to comply with Go error interface
func (e *Error) Error() string {
return e.Message
}
// GetErrorCode returns error code coming from xendit backend
func (e *Error) GetErrorCode() string {
return e.ErrorCode
}
// GetStatus returns http status code
func (e *Error) GetStatus() int {
return e.Status
}
// FromGoErr generates xendit.Error from generic go errors
func FromGoErr(err error) *Error {
return &Error{
Status: http.StatusTeapot,
ErrorCode: GoErrCode,
Message: err.Error(),
}
}
// FromHTTPErr generates xendit.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
}