add error template
Marco Andronaco andronacomarco@gmail.com
Thu, 24 Oct 2024 20:49:16 +0200
3 files changed,
26 insertions(+),
13 deletions(-)
M
src/app/functions.go
→
src/app/functions.go
@@ -24,6 +24,11 @@ validUsername = regexp.MustCompile(`(?i)^[a-z0-9._-]+$`)
validEmail = regexp.MustCompile(`^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$`) ) +func showError(w http.ResponseWriter, text string, status int) { + w.WriteHeader(status) + xt.ExecuteTemplate(w, "error.tmpl", map[string]interface{}{"Status": status, "Text": text}) +} + func getUserByName(username string, excluding uint) (user User, err error) { err = db.Model(&User{}).Where("upper(username) == upper(?) AND id != ?", username, excluding).First(&user).Error return@@ -57,7 +62,7 @@ }
cookie, err := g.GenerateCookie(duration) if err != nil { - http.Error(w, "Could not generate session cookie.", http.StatusInternalServerError) + showError(w, "Could not generate session cookie.", http.StatusInternalServerError) } ks.Set("session:"+cookie.Value, userID, duration)
M
src/app/handlers.go
→
src/app/handlers.go
@@ -12,7 +12,7 @@
func getProfileHandler(w http.ResponseWriter, r *http.Request) { user, ok := getLoggedUser(r) if !ok { - http.Error(w, "Could not find user in context.", http.StatusInternalServerError) + showError(w, "Could not find user in context.", http.StatusInternalServerError) return }@@ -38,31 +38,31 @@ }
func postRegisterHandler(w http.ResponseWriter, r *http.Request) { if !registrationEnabled { - http.Error(w, "Registration is currently disabled.", http.StatusForbidden) + showError(w, "Registration is currently disabled.", http.StatusForbidden) return } username, err := sanitizeUsername(r.FormValue("username")) if err != nil { - http.Error(w, "Invalid username.", http.StatusBadRequest) + showError(w, "Invalid username.", http.StatusBadRequest) return } email, err := sanitizeEmail(r.FormValue("email")) if err != nil { - http.Error(w, "Invalid email.", http.StatusBadRequest) + showError(w, "Invalid email.", http.StatusBadRequest) return } _, err = getUserByName(username, 0) if err == nil { - http.Error(w, "This username is already registered.", http.StatusConflict) + showError(w, "This username is already registered.", http.StatusConflict) return } hashedPassword, salt, err := g.HashPassword(r.FormValue("password")) if err != nil { - http.Error(w, "Invalid password.", http.StatusBadRequest) + showError(w, "Invalid password.", http.StatusBadRequest) return }@@ -75,7 +75,7 @@ }
db.Create(&user) if user.ID == 0 { - http.Error(w, "This email is already registered.", http.StatusConflict) + showError(w, "This email is already registered.", http.StatusConflict) return }@@ -91,7 +91,7 @@
user, err := getUserByName(username, 0) if err != nil || !g.CheckPassword(password, user.Salt, user.PasswordHash) { - http.Error(w, "Invalid credentials", http.StatusUnauthorized) + showError(w, "Invalid credentials", http.StatusUnauthorized) return }@@ -117,7 +117,7 @@ }
resetToken, err := g.GenerateRandomToken(32) if err != nil { - http.Error(w, "Could not generate reset token.", http.StatusInternalServerError) + showError(w, "Could not generate reset token.", http.StatusInternalServerError) return }@@ -132,7 +132,7 @@ func getResetPasswordConfirmHandler(w http.ResponseWriter, r *http.Request) {
token := r.URL.Query().Get("token") _, err := ks.Get("reset:" + token) if err != nil { - http.Error(w, "Token is invalid or expired.", http.StatusUnauthorized) + showError(w, "Token is invalid or expired.", http.StatusUnauthorized) return }@@ -143,7 +143,7 @@ func postResetPasswordConfirmHandler(w http.ResponseWriter, r *http.Request) {
token := r.URL.Query().Get("token") userID, err := ks.Get("reset:" + token) if err != nil { - http.Error(w, "Token is invalid or expired.", http.StatusUnauthorized) + showError(w, "Token is invalid or expired.", http.StatusUnauthorized) return }@@ -154,7 +154,7 @@ password := r.FormValue("password")
hashedPassword, salt, err := g.HashPassword(password) if err != nil { - http.Error(w, "Invalid password.", http.StatusBadRequest) + showError(w, "Invalid password.", http.StatusBadRequest) return }
A
templates/error.tmpl
@@ -0,0 +1,8 @@
+{{ extends "base.tmpl" }} + +{{define "title" -}}Error - {{end}} + +{{define "content" -}} + <h1>{{.Status}}</h1> + <p>{{.Text}}</p> +{{end}}