-
Notifications
You must be signed in to change notification settings - Fork 73
/
errors.go
96 lines (84 loc) · 2.23 KB
/
errors.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
/* Package common contains common generic structures
* like errors that can be used across other kite packages.
*/
package kiteconnect
import "net/http"
// API errors. Check documantation to learn about individual exception: https://kite.trade/docs/connect/v3/exceptions/.
const (
GeneralError = "GeneralException"
TokenError = "TokenException"
PermissionError = "PermissionError"
UserError = "UserException"
TwoFAError = "TwoFAException"
OrderError = "OrderException"
InputError = "InputException"
DataError = "DataException"
NetworkError = "NetworkException"
)
// Error is the error type used for all API errors.
type Error struct {
Code int
ErrorType string
Message string
Data interface{}
}
// This makes Error a valid Go error type.
func (e Error) Error() string {
return e.Message
}
// NewError creates and returns a new instace of Error
// with custom error metadata.
func NewError(etype string, message string, data interface{}) error {
var (
code = http.StatusInternalServerError
)
switch etype {
case GeneralError:
code = http.StatusInternalServerError
case TokenError:
code = http.StatusForbidden
case PermissionError:
code = http.StatusForbidden
case UserError:
code = http.StatusForbidden
case TwoFAError:
code = http.StatusForbidden
case OrderError:
code = http.StatusBadRequest
case InputError:
code = http.StatusBadRequest
case DataError:
code = http.StatusGatewayTimeout
case NetworkError:
code = http.StatusServiceUnavailable
default:
code = http.StatusInternalServerError
etype = GeneralError
}
return newError(etype, message, code, data)
}
func newError(etype, message string, code int, data interface{}) Error {
return Error{
Message: message,
ErrorType: etype,
Data: data,
Code: code,
}
}
// GetErrorName returns an error name given an HTTP code.
func GetErrorName(code int) string {
var err string
switch code {
case http.StatusInternalServerError:
err = GeneralError
case http.StatusForbidden, http.StatusUnauthorized:
err = TokenError
case http.StatusBadRequest:
err = InputError
case http.StatusServiceUnavailable, http.StatusGatewayTimeout:
err = NetworkError
default:
err = GeneralError
}
return err
}