Skip to content

Commit fc5a84d

Browse files
corelchenunknwon
andcommitted
Support global custom error handler (#31)
* Support global custom error handler * Update binding.go Co-authored-by: ᴜɴᴋɴᴡᴏɴ <u@gogs.io>
1 parent 5ae81f9 commit fc5a84d

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

binding.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ func errorHandler(errs Errors, rw http.ResponseWriter) {
8888
}
8989
}
9090

91+
// CustomErrorHandler will be invoked if errors occured.
92+
var CustomErrorHandler func(*macaron.Context, Errors)
93+
9194
// Bind wraps up the functionality of the Form and Json middleware
9295
// according to the Content-Type and verb of the request.
9396
// A Content-Type is required for POST and PUT requests.
@@ -101,6 +104,8 @@ func Bind(obj interface{}, ifacePtr ...interface{}) macaron.Handler {
101104
bind(ctx, obj, ifacePtr...)
102105
if handler, ok := obj.(ErrorHandler); ok {
103106
ctx.Invoke(handler.Error)
107+
} else if CustomErrorHandler != nil {
108+
ctx.Invoke(CustomErrorHandler)
104109
} else {
105110
ctx.Invoke(errorHandler)
106111
}

0 commit comments

Comments
 (0)