Skip to content

Commit

Permalink
Create new errors layout
Browse files Browse the repository at this point in the history
- Set default layout for all errors to point to layouts/errors
  • Loading branch information
aarondl committed May 16, 2020
1 parent df8d094 commit b328d3f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 11 deletions.
14 changes: 6 additions & 8 deletions abcmiddleware/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,15 @@ func (m *ErrorManager) Errors(ctrl AppHandler) http.HandlerFunc {
log.Error("request error", fields...)
requestID := chimiddleware.GetReqID(r.Context())

if len(layout) != 0 {
m.render.HTMLWithLayout(w, code, template, requestID, layout)
} else {
m.render.HTML(w, code, template, requestID)
err = m.render.HTMLWithLayout(w, code, template, requestID, layout)
if err != nil {
panic(err)
}
default: // warn does not log stacktrace in prod, but error and above does
log.Warn("request failed", fields...)
if len(layout) != 0 {
m.render.HTMLWithLayout(w, code, template, nil, layout)
} else {
m.render.HTML(w, code, template, nil)
err = m.render.HTMLWithLayout(w, code, template, nil, layout)
if err != nil {
panic(err)
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions templates/app/setup.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ func NewRootController(renderer abcrender.Renderer {{- if not .NoSessions}}, ses
}
}

// NewErrorManager creates a new error manager
func NewErrorManager(renderer abcrender.Renderer) *abcmiddleware.ErrorManager {
// The empty string here can be replaced for a specific layout for errors
return abcmiddleware.NewErrorManager(renderer, "layouts/errors")
}

// NewManifest makes an assets manifest if it's set in the config
func NewManifest(cfg *Config) (map[string]string, error) {
if !cfg.AppConfig.Server.AssetsManifest {
Expand Down
4 changes: 2 additions & 2 deletions templates/routes/routes.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func NewRouter(
methodNotAllowed := abcserver.NewMethodNotAllowedHandler()
router.MethodNotAllowed(methodNotAllowed.Handler(renderer))

errMgr.Add(abcmiddleware.NewError(controllers.ErrUnauthorized, http.StatusUnauthorized, "errors/401", nil))
errMgr.Add(abcmiddleware.NewError(controllers.ErrForbidden, http.StatusForbidden, "errors/403", nil))
errMgr.Add(abcmiddleware.NewError(controllers.ErrUnauthorized, http.StatusUnauthorized, "layouts/errors", "errors/401", nil))
errMgr.Add(abcmiddleware.NewError(controllers.ErrForbidden, http.StatusForbidden, "layouts/errors", "errors/403", nil))

// Make a pointer to the errMgr.Errors function so it's easier to call
e := errMgr.Errors
Expand Down
49 changes: 49 additions & 0 deletions templates/templates/layouts/errors.html.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!DOCTYPE html>
<html>
<head>
<title>{{.AppName}}</title>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->

<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="/favicon.ico">

{{if eq .Bootstrap "regular" -}}
{{"{"}}{ cssPath "bootstrap/bootstrap.css" | cssTag }{{"}"}}
{{- else if eq .Bootstrap "gridonly" -}}
{{"{"}}{ cssPath "bootstrap/bootstrap-grid.css" | cssTag }{{"}"}}
{{- else if eq .Bootstrap "rebootonly" -}}
{{"{"}}{ cssPath "bootstrap/bootstrap-reboot.css" | cssTag }{{"}"}}
{{- else if eq .Bootstrap "gridandrebootonly" -}}
{{"{"}}{ cssPath "bootstrap/bootstrap-grid.css" | cssTag }{{"}"}}
{{"{"}}{ cssPath "bootstrap/bootstrap-reboot.css" | cssTag }{{"}"}}
{{- end}}
{{if not .NoFontAwesome -}}
{{"{"}}{ cssPath "font-awesome/font-awesome.css" | cssTag }{{"}"}}
{{- end}}
{{"{"}}{ cssPath "main.css" | cssTag }{{"}"}}
{{if not .NoLiveReload -}}
{{"{"}}{if config.Server.LiveReload -}{{"}"}}
{{"{"}}{liveReload "livereload.js" "localhost" | jsTag }{{"}"}}
{{"{"}}{- end}{{"}"}}
{{- end}}
</head>
<body>
{{"{"}}{ yield }{{"}"}}

{{- if (and (eq .Bootstrap "regular") (not .NoBootstrapJS)) -}}
<!-- jquery is a twitter bootstrap dependency -->
{{"{"}}{ jsPath "jquery-3.1.1.js" | jsTag }{{"}"}}

<!-- tether is a twitter bootstrap tooltips dependency -->
{{"{"}}{ jsPath "tether.js" | jsTag }{{"}"}}

<!-- include all twitter bootstrap javascript plugins -->
{{"{"}}{ jsBootstrap }{{"}"}}
{{- end}}
</body>
</html>
2 changes: 1 addition & 1 deletion templates/wire.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func BuildApp(flags *pflag.FlagSet) (*app.App, func(), error) {
{{if not .NoSessions -}}
app.NewSessions,
{{end -}}
abcmiddleware.NewErrorManager,
app.NewErrorManager,
routes.NewRouter,
rendering.New,
app.NewRootController,
Expand Down

0 comments on commit b328d3f

Please sign in to comment.