-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patherror.go
115 lines (94 loc) · 2.54 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package rm
import (
"fmt"
"strings"
"github.com/tidwall/gjson"
)
// error codes :
const (
errTemplate = "rm: %s"
ErrorCodePaymentAlreadyRefunded = "PAYMENT_FULLY_REFUNDED"
ErrorCodeTransactionNotFound = "TRANSACTION_NOT_FOUND"
ErrorCodeStoreNotFound = "STORE_NOT_FOUND"
ErrorCodeValidationError = "VALIDATION_ERROR"
ErrorCodeRefundAmountExceedPerDay = "PAYMENT_REFUND_AMOUNT_EXCEED_PER_DAY"
ErrorCodeMerchantSettlementAccNotVerified = "MERCHANT_SETTLEMENT_ACCOUNT_NOT_VERIFIED"
)
// error to compare, you may compare using errors.Is(err, rm.ErrPaymentAlreadyRefunded)
var (
ErrPaymentAlreadyRefunded = newErrorCode(ErrorCodePaymentAlreadyRefunded)
ErrTransactionNotFound = newErrorCode(ErrorCodeTransactionNotFound)
ErrStoreNotFound = newErrorCode(ErrorCodeStoreNotFound)
ErrRefundExceedLimitPerDay = newErrorCode(ErrorCodeRefundAmountExceedPerDay)
ErrValidation = newErrorCode(ErrorCodeValidationError)
)
type errorCode struct{ id string }
var (
_ error = (*errorCode)(nil)
_ ErrorCode = (*errorCode)(nil)
)
func newErrorCode(id string) error {
return &errorCode{id: id}
}
func (err errorCode) isCode(id string) bool {
return err.id == id
}
func (err errorCode) Error() string {
return fmt.Sprintf(errTemplate, err.id)
}
type ErrorCode interface {
isCode(id string) bool
}
// Error :
type Error struct {
code string
url string
rawRequest []byte
rawResponse []byte
}
var (
_ fmt.Formatter = (*Error)(nil)
_ error = (*Error)(nil)
)
func newError(url string, reqBytes, respBytes []byte) *Error {
e := new(Error)
e.code = strings.ToUpper(strings.TrimSpace(gjson.GetBytes(respBytes, "error.code").String()))
e.url = url
e.rawResponse = respBytes
e.rawRequest = reqBytes
return e
}
func (e Error) isCode(errID string) bool {
return e.code == errID
}
func (e Error) Is(err error) bool {
v, ok := err.(ErrorCode)
if ok {
return v.isCode(e.code)
}
return e.Error() == err.Error()
}
// Error :
func (e Error) Error() string {
return fmt.Sprintf(errTemplate, e.code)
}
func (e Error) Format(f fmt.State, verb rune) {
if !f.Flag('+') {
f.Write([]byte(e.Error()))
return
}
f.Write([]byte("URL : "))
f.Write([]byte(e.url))
f.Write([]byte("\n"))
f.Write([]byte("Request Body :\n"))
f.Write(e.rawRequest)
f.Write([]byte("\n"))
f.Write([]byte("Response Body :\n"))
f.Write(e.rawResponse)
}
func (e Error) Response() string {
return string(e.rawResponse)
}
func (e Error) ResponseBytes() []byte {
return e.rawResponse
}