Customize 403/404 error pages

This commit is contained in:
cuigh 2017-09-29 20:18:29 +08:00
parent 4bf5588c9c
commit 8c00a7f69d
3 changed files with 38 additions and 0 deletions

View File

@ -10,6 +10,7 @@ import (
type HomeController struct {
Index web.HandlerFunc `path:"/" name:"index" authorize:"?" desc:"index page"`
Error403 web.HandlerFunc `path:"/403" name:"403" authorize:"?" desc:"403 page"`
Error404 web.HandlerFunc `path:"/404" name:"404" authorize:"*" desc:"404 page"`
Login web.HandlerFunc `path:"/login" name:"login" authorize:"*" desc:"sign in page"`
InitGet web.HandlerFunc `path:"/init" name:"init" authorize:"*" desc:"initialize page"`
@ -95,8 +96,13 @@ func Home() (c *HomeController) {
return ajaxResult(ctx, err)
}
c.Error403 = func(ctx web.Context) error {
return ctx.Render("403", nil)
}
c.Error404 = func(ctx web.Context) error {
return ctx.Render("404", nil)
}
return
}

View File

@ -1,6 +1,7 @@
package main
import (
"net/http"
"path/filepath"
"runtime"
"time"
@ -18,6 +19,14 @@ import (
)
func main() {
// customize error handler
web.DefaultErrorHandler.OnCode(http.StatusNotFound, func(ctx web.Context, err error) {
ctx.Redirect("/404")
})
web.DefaultErrorHandler.OnCode(http.StatusForbidden, func(ctx web.Context, err error) {
ctx.Redirect("/403")
})
ws := web.Auto()
// set render/validator..

23
views/403.jet Normal file
View File

@ -0,0 +1,23 @@
{{ extends "_layouts/empty" }}
{{ block body() }}
<section class="hero is-light is-fullheight is-bold">
<div class="hero-body">
<div class="container">
<div class="columns">
<div class="column is-6 is-offset-3">
<div class="content">
<h1 class="title is-1 has-text-centered has-text-danger is-marginless">403</h1>
<h2 class="title has-text-centered has-text-black">Forbidden</h2>
<p class="has-text-centered">
You do not have permission to access this page.
</p>
<div class="has-text-centered">
<a href="/" class="has-text-centered button is-primary is-outlined">To Home Page</a>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
{{ end }}