aboutsummaryrefslogtreecommitdiff
path: root/api/api.go
diff options
context:
space:
mode:
authorjloup <jloup@jloup.work>2018-05-16 19:48:05 +0200
committerjloup <jloup@jloup.work>2018-05-16 19:48:05 +0200
commit4495b984d3e60874281f37cdb2dbe2cf1c3874ab (patch)
treeeeb5e0b4950f340151fda79be0b2e3cac80c4035 /api/api.go
parent372ed7400f57355eb7dd98058b10ddeb1e1ff635 (diff)
downloadFront-4495b984d3e60874281f37cdb2dbe2cf1c3874ab.tar.gz
Front-4495b984d3e60874281f37cdb2dbe2cf1c3874ab.tar.zst
Front-4495b984d3e60874281f37cdb2dbe2cf1c3874ab.zip
Error flags.
Diffstat (limited to 'api/api.go')
-rw-r--r--api/api.go43
1 files changed, 30 insertions, 13 deletions
diff --git a/api/api.go b/api/api.go
index e011811..f921301 100644
--- a/api/api.go
+++ b/api/api.go
@@ -5,6 +5,7 @@ import (
5 "unicode" 5 "unicode"
6 6
7 "github.com/gin-gonic/gin" 7 "github.com/gin-gonic/gin"
8 "github.com/jloup/utils"
8) 9)
9 10
10var CONFIG Config 11var CONFIG Config
@@ -43,11 +44,30 @@ func SetMailConfig(config MailConfig) {
43} 44}
44 45
45type Error struct { 46type Error struct {
46 Code ErrorCode 47 Code utils.Flag
47 UserMessage string 48 UserMessage string
48 err error 49 err error
49} 50}
50 51
52func (e Error) FlagString() string {
53 return e.Code.String()
54}
55
56func (e Error) Flag() utils.Flag {
57 return e.Code
58}
59
60func (e Error) Msg() string {
61 return e.UserMessage
62}
63
64func (e Error) ErrorWithCode(f utils.Flag) utils.ErrorFlagged {
65 if utils.Intersect(e.Code, f) {
66 return e
67 }
68
69 return nil
70}
51func (e Error) Err() error { 71func (e Error) Err() error {
52 if e.err != nil { 72 if e.err != nil {
53 return e 73 return e
@@ -64,7 +84,7 @@ func (e Error) Error() string {
64 return "" 84 return ""
65} 85}
66 86
67func ErrorIs(err error, code ErrorCode) bool { 87func ErrorIs(err error, code utils.Flag) bool {
68 if err == nil { 88 if err == nil {
69 return false 89 return false
70 } 90 }
@@ -72,7 +92,7 @@ func ErrorIs(err error, code ErrorCode) bool {
72 if apiError, ok := err.(*Error); !ok { 92 if apiError, ok := err.(*Error); !ok {
73 return false 93 return false
74 } else { 94 } else {
75 return apiError.Code == code 95 return utils.Intersect(apiError.Code, code) && apiError.Code.String() == code.String()
76 } 96 }
77} 97}
78 98
@@ -99,8 +119,8 @@ func ToSnake(in string) string {
99} 119}
100 120
101type Response struct { 121type Response struct {
102 StatusCode Status `json:"-"` 122 StatusCode Status `json:"-"`
103 ErrorCode ErrorCode `json:"-"` 123 ErrorCode utils.Flag `json:"-"`
104 124
105 Status string `json:"status"` 125 Status string `json:"status"`
106 Code string `json:"code,omitempty"` 126 Code string `json:"code,omitempty"`
@@ -110,15 +130,12 @@ type Response struct {
110 130
111func (r Response) populateStatus() Response { 131func (r Response) populateStatus() Response {
112 r.Status = ToSnake(r.StatusCode.String()) 132 r.Status = ToSnake(r.StatusCode.String())
113 133 r.Code = ToSnake(r.ErrorCode.String())
114 if r.ErrorCode != 0 {
115 r.Code = ToSnake(r.ErrorCode.String())
116 }
117 134
118 return r 135 return r
119} 136}
120 137
121func ErrorResponse(code ErrorCode, message string) Response { 138func ErrorResponse(code utils.Flag, message string) Response {
122 return Response{ 139 return Response{
123 StatusCode: NOK, 140 StatusCode: NOK,
124 ErrorCode: code, 141 ErrorCode: code,
@@ -150,7 +167,7 @@ func M(handler Middleware) gin.HandlerFunc {
150 err := handler(c) 167 err := handler(c)
151 168
152 if err != nil { 169 if err != nil {
153 WriteJsonResponse(ErrorResponse(err.Code, err.UserMessage), c) 170 WriteJsonResponse(ErrorResponse(err.Flag(), err.UserMessage), c)
154 c.Error(err) 171 c.Error(err)
155 c.Abort() 172 c.Abort()
156 } else { 173 } else {
@@ -166,13 +183,13 @@ type Query interface {
166 183
167func RunQuery(query Query, c *gin.Context) { 184func RunQuery(query Query, c *gin.Context) {
168 if err := query.ValidateParams(); err != nil { 185 if err := query.ValidateParams(); err != nil {
169 WriteJsonResponse(ErrorResponse(err.Code, err.UserMessage), c) 186 WriteJsonResponse(ErrorResponse(err.Flag(), err.UserMessage), c)
170 c.Error(err) 187 c.Error(err)
171 return 188 return
172 } 189 }
173 190
174 if out, err := query.Run(); err != nil { 191 if out, err := query.Run(); err != nil {
175 WriteJsonResponse(ErrorResponse(err.Code, err.UserMessage), c) 192 WriteJsonResponse(ErrorResponse(err.Flag(), err.UserMessage), c)
176 c.Error(err) 193 c.Error(err)
177 } else { 194 } else {
178 WriteJsonResponse(SuccessResponse(out), c) 195 WriteJsonResponse(SuccessResponse(out), c)