In short, if I run some middleware for an http.Request and determine that the request deserves an HTTP 422, how can I "break-out" out of the middleware chain, and return early without invoking all of the middleware functions in the chain?
For example, if I have this:
func Routes(m *martini.ClassicMartini) {
m.Get("/cp/users", mw.AsJson, mw.RequestTimer, ctr.GetMany)
}
if I call return in any of the middleware funds above, to my knowledge it will still invoke all of the middleware funds registered in the chain, so that ctr.GetMany is always invoked.
is there some way to make the request/response as completed and tell martini to stop calling all the funcs in the chain?
if the first return value is an integer, I guess martini assumes it's a status code. My current best guess is according to the docs: https://github.com/go-martini/martini#middleware-handlers
we could use this:
m.Use(func(c martini.Context, w http.ResponseWriter){
if reqIsMalformed() {
http.Error(w, "Bad request because xyz", 422)
return;
}
c.Next()
})
and if a condition was not met, we could just never call c.Next()?
I did an experiment with the router/middleware, here are the results (the useful information is it at the end):
and here are the results when I hit the api:
there was meant to be added to this question. so here are the rules: