Handle HTTP errors
package main
import "github.com/kataras/iris/v12"
func main(){
app := iris.New()
app.RegisterView(iris.HTML("./views", ".html"))
app.OnErrorCode(iris.StatusNotFound, notFound)
app.OnErrorCode(iris.StatusInternalServerError, internalServerError)
// to register a handler for all error codes:
// app.OnAnyErrorCode(handler)
app.Get("/", index)
app.Listen(":8080")
}
func notFound(ctx iris.Context) {
// when 404 then render the template
// $views_dir/errors/404.html
ctx.View("errors/404.html")
// OR, if you had more paths and you want
// to suggest relative paths to the user:
// suggestPaths := ctx.FindClosest(3)
// if len(suggestPaths) == 0 {
// ctx.WriteString("404 not found")
// return
// }
// ctx.HTML("Did you mean?<ul>")
// for _, s := range suggestPaths {
// ctx.HTML(`<li><a href="%s">%s</a></li>`, s, s)
// }
// ctx.HTML("</ul>")
}
func internalServerError(ctx iris.Context) {
ctx.WriteString("Oups something went wrong, try again")
}
func index(ctx iris.Context) {
ctx.View("index.html")
}The Problem type
Last updated
Was this helpful?