跳转至

Error Handling

Default error handler send response to client according to Content-Type. It will send error message as text for normal request and respond a JSON object for JSON like this:

{
    "url":     "/user/124",
    "code":    10030,
    "message": "user is blocked",
    "detail":  "[optional detail message]"
}

The ErrorHandler providers some methods to customize

s.ErrorHandler.OnCode(http.StatusNotFound, func(ctx web.Context, err error) {
    if ctx.IsAJAX() {
        ctx.Status(http.StatusNotFound).HTML(http.StatusText(http.StatusNotFound))
    } else {
        ctx.Status(http.StatusNotFound).Render("404", nil)
    }
})
s.ErrorHandler.OnCode(http.StatusForbidden, func(ctx web.Context, err error) {
    if ctx.IsAJAX() {
        ctx.Status(http.StatusForbidden).HTML("You do not have permission to perform this operation")
    } else {
        ctx.Status(http.StatusForbidden).Render("403", nil)
    }
})