-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
113 lines (98 loc) · 3.16 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package handlers
import (
"net/http"
"github.com/gorilla/sessions"
"github.com/ugent-library/biblio-backoffice/ctx"
"github.com/ugent-library/biblio-backoffice/render"
"github.com/ugent-library/biblio-backoffice/views"
)
func UserNotFound(w http.ResponseWriter, r *http.Request) {
c := ctx.Get(r)
// clear session to make sure that baseHandler doesn't keep blocking other handlers
session, err := c.SessionStore.Get(r, c.SessionName)
if err != nil {
c.Log.Errorf("unable to retrieve session: %w", err)
InternalServerError(w, r)
return
}
if err := clearSession(w, r, session); err != nil {
c.Log.Errorf("unable to save session: %w", err)
InternalServerError(w, r)
return
}
w.WriteHeader(404)
views.UserNotFound(c).Render(r.Context(), w)
}
func NotFound(w http.ResponseWriter, r *http.Request) {
c := ctx.Get(r)
w.WriteHeader(404)
views.NotFound(c).Render(r.Context(), w)
}
func InternalServerError(w http.ResponseWriter, r *http.Request) {
c := ctx.Get(r)
w.WriteHeader(500)
views.InternalServerError(c).Render(r.Context(), w)
}
type YieldNotFound struct {
BaseContext
PageTitle string
ActiveNav string
ErrorTitle string
ErrorDescription string
}
type YieldModalError struct {
BaseContext
ID string
}
func (h *BaseHandler) NotFound(w http.ResponseWriter, r *http.Request, ctx BaseContext) {
w.WriteHeader(404)
render.Layout(w, "layouts/default", "pages/notfound", YieldNotFound{
BaseContext: ctx,
PageTitle: "Biblio",
ErrorTitle: "This page does not exist.",
ErrorDescription: "Your (re)search was too groundbreaking.",
})
}
func (h *BaseHandler) InternalServerError(w http.ResponseWriter, r *http.Request, ctx BaseContext) {
w.WriteHeader(500)
render.Layout(w, "layouts/default", "pages/internalerror", YieldNotFound{
BaseContext: ctx,
PageTitle: "Biblio",
ErrorTitle: "Something went wrong.",
ErrorDescription: "Your (re)search was too groundbreaking.",
})
}
func (h *BaseHandler) ErrorModal(w http.ResponseWriter, r *http.Request, errID string, ctx BaseContext) {
render.Layout(w, "show_modal", "modals/error", YieldModalError{
BaseContext: ctx,
ID: errID,
})
}
func (h *BaseHandler) UserNotFound(w http.ResponseWriter, r *http.Request, ctx BaseContext) {
// clear session to make sure that baseHandler doesn't keep blocking other handlers
session, err := h.SessionStore.Get(r, h.SessionName)
if err != nil {
h.Logger.Errorf("unable to retrieve session: %w", err)
h.InternalServerError(w, r, ctx)
return
}
if err := clearSession(w, r, session); err != nil {
h.Logger.Errorf("unable to save session: %w", err)
h.InternalServerError(w, r, ctx)
return
}
w.WriteHeader(404)
render.Layout(w, "layouts/default", "pages/usernotfound", YieldNotFound{
BaseContext: ctx,
PageTitle: "Biblio",
ErrorTitle: "Account not found",
ErrorDescription: "Account not found",
})
}
func clearSession(w http.ResponseWriter, r *http.Request, session *sessions.Session) error {
delete(session.Values, UserIDKey)
delete(session.Values, OriginalUserIDKey)
delete(session.Values, OriginalUserRoleKey)
delete(session.Values, UserRoleKey)
return session.Save(r, w)
}