iris 错误处理
当程序产生了一个特定的 http 错误的时候,你可以定义你自己的错误处理代码。
错误代码是大于或者等于 400 的 http 状态码,像 404 not found 或者 500 服务器内部错误。
代码例子:
package main import "github.com/kataras/iris" func main(){ app := iris.New() app.OnErrorCode(iris.StatusNotFound, notFound) app.OnErrorCode(iris.StatusInternalServerError, internalServerError) // 为所有的大于等于400的状态码注册一个处理器: // app.OnAnyErrorCode(handler) app.Get("/", index) app.Run(iris.Addr(":8080")) } func notFound(ctx iris.Context) { // 出现 404 的时候,就跳转到 $views_dir/errors/404.html 模板 ctx.View("errors/404.html") } func internalServerError(ctx iris.Context) { ctx.WriteString("Oups something went wrong, try again") } func index(ctx context.Context) { ctx.View("index.html") }