Goroute provides out of the box global error handling. This is possible due to route handler func signature.
Each route handler requires this signature:
HandlerFunc func(Context) error
Which allows to directly return error from handler:
mux.GET("/user/:id", func(c Context) error {
id := c.Param("id")
if id == "" {
return errors.New("id is empty")
}
if len(id) != 32 {
return errors.New("id is invalid")
}
return c.NoContent(http.StatusOk)
})