/
error.go
64 lines (53 loc) · 1.78 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
package model
import (
"encoding/json"
"fmt"
)
var (
// general error
ErrNil = &BusinessError{Code: 0, Message: "OK"}
ErrValidation = &BusinessError{Code: 1, Message: "Invalid parameter"}
ErrInternalServer = &BusinessError{Code: 2, Message: "Internal server error"}
ErrAuth = &BusinessError{Code: 3, Message: "Authentication failed"}
// specific error
ErrAppNotFound = &BusinessError{Code: 10000, Message: "APP not found"}
ErrResourceNotFound = &BusinessError{Code: 10001, Message: "Resource not found"}
ErrInsufficentBalance = &BusinessError{Code: 10002, Message: "Insufficient balance"}
ErrAccountFrozen = &BusinessError{Code: 10003, Message: "Account fronzen"}
ErrInvalidAppOperator = &BusinessError{Code: 10004, Message: "Invalid APP operator"}
ErrNotBillingApp = &BusinessError{Code: 10005, Message: "Not a billing APP"}
)
type BusinessError struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
func (be *BusinessError) WithData(data interface{}) *BusinessError {
return &BusinessError{
Code: be.Code, Message: be.Message, Data: data,
}
}
func (err *BusinessError) Error() string {
return fmt.Sprintf("%s: %v", err.Message, err.Data)
}
func (err *BusinessError) IsNil() bool {
return err == nil || err.Code == ErrNil.Code
}
// GetObject converts the business error data to an arbitrary type.
//
// The function works as you would expect it from json.Unmarshal()
func (be *BusinessError) GetObject(toType interface{}) error {
js, err := json.Marshal(be.Data)
if err != nil {
return err
}
err = json.Unmarshal(js, toType)
if err != nil {
return err
}
return nil
}
func IsBusinessError(err error) (*BusinessError, bool) {
be, ok := err.(*BusinessError)
return be, ok
}