Skip to content

Commit c04e15a

Browse files
committed
- Fixed error code issue on route and exception handler execution
- Updated body type to map[string]interface{}
1 parent fa96be3 commit c04e15a

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

api.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,26 @@ func (api *API) OnError(code string, handle Handler) {
134134
api.exceptions = append(api.exceptions, exp)
135135
}
136136

137+
// OnErrors method is used to handle a custom errors thrown by users
138+
func (api *API) OnErrors(codes []string, handle Handler) {
139+
for _, code := range codes {
140+
exp := exception{
141+
code: code,
142+
handle: handle,
143+
}
144+
api.exceptions = append(api.exceptions, exp)
145+
}
146+
}
147+
137148
// UnhandledException method is used to handle all unhandled exceptions
138149
func (api *API) UnhandledException(handle Handler) {
139150
api.unhandled = handle
140151
}
141152

142153
// error variables to handle expected errors
143154
var (
144-
codeNotFound = "URL_NOT_FOUND"
145-
codeUncaughtException = "UNCAUGHT_EXCEPTION"
155+
ErrCodeNotFound = "URL_NOT_FOUND"
156+
ErrCodeUncaughtException = "UNCAUGHT_EXCEPTION"
146157
)
147158

148159
// It's required handle for http module.
@@ -163,7 +174,7 @@ func (api *API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
163174
err := recover()
164175
if err != nil {
165176
if !ctx.end {
166-
ctx.code = codeUncaughtException
177+
ctx.code = ErrCodeUncaughtException
167178
ctx.err = fmt.Errorf("%v", err)
168179
ctx.unhandledException()
169180
}
@@ -173,7 +184,7 @@ func (api *API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
173184

174185
// STEP 2: execute all interceptors
175186
for _, task := range api.interceptors {
176-
if ctx.end || ctx.err != nil {
187+
if ctx.end || ctx.code != "" {
177188
break
178189
}
179190

@@ -183,7 +194,7 @@ func (api *API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
183194
// STEP 3: check routes
184195
urlPath := []byte(req.URL.Path)
185196
for _, route := range api.routes {
186-
if ctx.end || ctx.err != nil {
197+
if ctx.end || ctx.code != "" {
187198
break
188199
}
189200

@@ -208,7 +219,7 @@ func (api *API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
208219
// STEP 5: unhandled exceptions
209220
if !ctx.end {
210221
if ctx.code == "" && !ctx.found {
211-
ctx.Throw(codeNotFound)
222+
ctx.Throw(ErrCodeNotFound)
212223
}
213224

214225
if api.unhandled != nil {

context.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type Context struct {
2323
Request *http.Request
2424
Response http.ResponseWriter
2525
Query url.Values
26-
Body interface{}
26+
Body map[string]interface{}
2727
Params map[string]string
2828

2929
// for internal use
@@ -215,18 +215,20 @@ func (ctx *Context) unhandledException() {
215215
}
216216

217217
// NOT FOUND handler
218-
if ctx.code == codeNotFound {
218+
if ctx.code == ErrCodeNotFound {
219219
http.NotFound(ctx.Response, ctx.Request)
220220
return
221221
}
222222

223223
if ctx.code != "" || ctx.err != nil {
224224
msg := ctx.code
225225
if ctx.err != nil {
226-
msg = ctx.err.Error()
226+
msg += "\nError: " + ctx.err.Error()
227227
}
228228
ctx.SetHeader("Content-Type", "text/plain;charset=UTF-8")
229-
ctx.Status(http.StatusInternalServerError)
229+
if ctx.status > 400 {
230+
ctx.Status(http.StatusInternalServerError)
231+
}
230232
ctx.Write([]byte(msg))
231233
}
232234
}

0 commit comments

Comments
 (0)