/
error.go
67 lines (59 loc) · 1.17 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
package domain
import (
"fmt"
)
var (
ErrCodeUnexpected = 1000
ErrInvalidAddressHex = NewError(
2001,
"Address is not hex",
)
ErrInvalidSignatureSize = NewError(
2002,
fmt.Sprintf("Signature must be %d bytes", SignatureSize),
)
ErrInvalidSignatureHex = NewError(
2003,
"Signature is not hex",
)
ErrInvalidSignature = NewError(
2004,
"Signature is invalid",
)
ErrTooShortUserName = NewError(
3001,
fmt.Sprintf("User name must be %d characters or more", UserNameLengthMin),
)
ErrTooLongUserName = NewError(
3002,
fmt.Sprintf("User name must be %d characters or less", UserNameLengthMax),
)
ErrUserNotFound = NewError(
3003,
"User is not found",
)
ErrUserAlreadyExists = NewError(
3004,
"User already exists",
)
ErrUserBroken = NewError(
3005,
"User was broken",
)
)
type Error struct {
Code int `json:"code"`
Message string `json:"message"`
}
func NewError(code int, msg string) *Error {
return &Error{
Code: code,
Message: msg,
}
}
func NewUnexpectedError() *Error {
return NewError(ErrCodeUnexpected, "Internal server error")
}
func (err *Error) Error() string {
return fmt.Sprintf("%s [%d]", err.Message, err.Code)
}