-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
76 lines (64 loc) · 1.7 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
package templateless
import "fmt"
type BadRequestCode int
const (
AccountQuotaExceeded BadRequestCode = 200
ProviderKeyMissing BadRequestCode = 300
ProviderKeyInvalid BadRequestCode = 301
EmailNoContent BadRequestCode = 400
)
func (code BadRequestCode) Error() string {
switch code {
case AccountQuotaExceeded:
return "account quota exceeded"
case ProviderKeyMissing:
return "provider key missing"
case ProviderKeyInvalid:
return "provider key invalid"
case EmailNoContent:
return "email has no content"
default:
return "unknown bad request code"
}
}
type CustomError struct {
Type string `json:"type"`
BadRequestCode BadRequestCode `json:"code"`
Field string `json:"field"`
ErrorMsg string `json:"error"`
}
func (e *CustomError) Error() string {
return e.ErrorMsg
}
func UnauthorizedError() *CustomError {
return &CustomError{Type: "Unauthorized", ErrorMsg: "unauthorized"}
}
func ForbiddenError() *CustomError {
return &CustomError{Type: "Forbidden", ErrorMsg: "forbidden"}
}
func InvalidParameterError(field string) *CustomError {
return &CustomError{
Type: "InvalidParameter",
Field: field,
ErrorMsg: fmt.Sprintf("invalid parameter: %s", field),
}
}
func BadRequestError(code BadRequestCode, error string) *CustomError {
return &CustomError{
Type: "BadRequest",
BadRequestCode: code,
ErrorMsg: fmt.Sprintf("bad request: %s - %s", code.Error(), error),
}
}
func UnavailableError() *CustomError {
return &CustomError{
Type: "Unavailable",
ErrorMsg: "unavailable",
}
}
func UnknownError() *CustomError {
return &CustomError{
Type: "UnknownError",
ErrorMsg: "unknown error",
}
}