/
error.go
73 lines (63 loc) · 1.37 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
package rest
import (
"errors"
"fmt"
"net/http"
)
type Error struct {
StatusCode int
Err string
Message string
}
func (e *Error) Error() string {
return fmt.Sprintf("%v - %v - %v", e.StatusCode, e.Err, e.Message)
}
func (e *Error) Is(target error) bool {
var restErr *Error
if !errors.As(target, &restErr) {
return false
}
return *e == *restErr
}
func NewInternalServerError(msg string) *Error {
return &Error{
StatusCode: http.StatusInternalServerError,
Err: "ERR_INTERNAL_ERROR",
Message: msg,
}
}
func NewBadRequestError(msg string) *Error {
return &Error{
StatusCode: http.StatusBadRequest,
Err: "ERR_BAD_REQUEST",
Message: msg,
}
}
func NewPartnerNotFoundError() *Error {
return &Error{
StatusCode: http.StatusNotFound,
Err: "ERR_PARTNER_NOT_FOUND",
Message: "given `partner_id` is not found",
}
}
func NewGameNotFoundError() *Error {
return &Error{
StatusCode: http.StatusNotFound,
Err: "ERR_GAME_NOT_FOUND",
Message: "game is not found",
}
}
func NewBattleNotFoundError() *Error {
return &Error{
StatusCode: http.StatusNotFound,
Err: "ERR_BATTLE_NOT_FOUND",
Message: "battle is not found",
}
}
func NewInvalidBattleStateError() *Error {
return &Error{
StatusCode: http.StatusConflict,
Err: "ERR_INVALID_BATTLE_STATE",
Message: "invalid battle state",
}
}