You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For some of my http.Handlers I would like to have validation (or other errors) sent back inside a html/template (instead of simply output as JSON).
The Context struct is passed to the error handler.
var defaultErrorHandler = func(err error, c *gongular.Context) {`
My first thought was to figure out how to use the Context as a middleman so that I could look for a flag set by the controller/handler. Then I could write an ErrorHandler that would return JSON or another format depending on if a flag was set. However, c *gongular.Context does not seem to provide such a flagging mechanism.
What is the correct way to support outputs other than JSON? Is there a way for a middleware to handle this?
Update: it looks like a middleware can call .StopChain() to avoid the ErrorHandler. However, you can't seem to access other handlers/middlewares from a middleware.
type multiParam struct {
Param struct {
UserID string
Page int
}
}
func (m *multiParam) Handle(c *gongular.Context) error {
fmt.Println("multiparam")
c.SetBody(fmt.Sprintf("%s:%d", m.Param.UserID, m.Param.Page))
return nil
}
// The middleware to intercept validation errors
type middlewareHandler struct {}
func (m *middlewareHandler) Handle(c *gongular.Context) error {
fmt.Println("middleware")
return nil
}
func main() {
e := newEngineTest()
g := e.GetRouter().Group("/user/:UserID/page", &middlewareHandler{})
g.GET("/:Page", &multiParam{})
}
The text was updated successfully, but these errors were encountered:
xeoncross
changed the title
How to handle altering ErrorHandler based on Handler?
How to handle non-JSON responses based on Handler?
Feb 4, 2018
For some of my http.Handlers I would like to have validation (or other errors) sent back inside a
html/template
(instead of simply output as JSON).The Context struct is passed to the error handler.
My first thought was to figure out how to use the
Context
as a middleman so that I could look for a flag set by the controller/handler. Then I could write an ErrorHandler that would return JSON or another format depending on if a flag was set. However,c *gongular.Context
does not seem to provide such a flagging mechanism.What is the correct way to support outputs other than JSON? Is there a way for a middleware to handle this?
Update: it looks like a middleware can call
.StopChain()
to avoid the ErrorHandler. However, you can't seem to access other handlers/middlewares from a middleware.The text was updated successfully, but these errors were encountered: