M handlers/auth.go => handlers/auth.go +18 -11
@@ 13,6 13,7 @@ import (
"tixe/auth"
"tixe/db"
"tixe/types"
+ "tixe/util"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
@@ 24,26 25,29 @@ func AuthCallback(auth *auth.Auth) gin.HandlerFunc {
return func(c *gin.Context) {
session := sessions.Default(c)
if c.Query("state") != session.Get("state") {
- c.String(http.StatusBadRequest, "Invalid state parameter!")
+ errStr := "Invalid state parameter"
+ util.RenderError(c, "state error", errStr, nil)
return
}
token, err := auth.Exchange(c.Request.Context(), c.Query("code"))
if err != nil {
- c.String(http.StatusUnauthorized, "Failed to exchange authorization code for token!")
+ errStr := "Failed to exchange authorization code for token"
+ util.RenderError(c, "token error", errStr, nil)
return
}
idToken, err := auth.VerifyIDToken(c.Request.Context(), token)
if err != nil {
- c.String(http.StatusInternalServerError, "Failed to verify ID token!")
+ errStr := "Failed to verify ID token"
+ util.RenderError(c, "token error", errStr, nil)
return
}
var profile map[string]interface{}
if err := idToken.Claims(&profile); err != nil {
- log.Printf("[tixe/auth] ERROR: Failed to get claims")
- c.String(http.StatusInternalServerError, "Failed to get claims!")
+ errStr := "Failed to get claims"
+ util.RenderError(c, "claims error", errStr, nil)
return
}
@@ 62,14 66,16 @@ func AuthCallback(auth *auth.Auth) gin.HandlerFunc {
"INSERT INTO users(id, display_name, oidc_subject) VALUES($1, $2, $3)",
userId, userDisplayName, idToken.Subject)
if err != nil {
- log.Printf("[tixe/auth] ERROR: Could not create database entry for oidc user")
- c.String(http.StatusInternalServerError, "Could not create database entry for oidc user")
+ errStr := "Could not create database entry for oidc user"
+ log.Printf("[tixe/auth] ERROR: %s", errStr)
+ util.RenderError(c, "db error", errStr, nil)
return
}
} else if scanErr != nil {
// scanErr was some other error than ErrNoRows
- log.Printf("[tixe/auth] ERROR: Could not query database for oidc user: %v", scanErr)
- c.String(http.StatusInternalServerError, "Could not query database for oidc user")
+ errStr := "Could not query database for oidc user"
+ log.Printf("[tixe/auth] ERROR: %s: %v", errStr, scanErr)
+ util.RenderError(c, "db error", errStr, nil)
return
}
@@ 78,8 84,9 @@ func AuthCallback(auth *auth.Auth) gin.HandlerFunc {
session.Set("access_token", token.AccessToken)
session.Set("profile", profile)
if err := session.Save(); err != nil {
- log.Printf("[tixe/auth] ERROR: Failed to save session: %v", err)
- c.String(http.StatusInternalServerError, "Failed to save session!")
+ errStr := "Failed to save session"
+ log.Printf("[tixe/auth] ERROR: %s: %v", errStr, err)
+ util.RenderError(c, "session error", errStr, nil)
return
}
M handlers/link.go => handlers/link.go +3 -2
@@ 12,6 12,7 @@ import (
"tixe/db"
"tixe/template"
"tixe/types"
+ "tixe/util"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
@@ 28,8 29,8 @@ func LinkEdit(c *gin.Context) {
"SELECT visual, link FROM links WHERE id = $1 AND user_id = $2",
linkId, user.Id).Scan(&link.Visual, &link.Link)
if err != nil {
- // something something show error page and abort i guess
- c.Abort()
+ errStr := "Could not query link entry from database"
+ util.RenderError(c, "db error", errStr, user)
return
}
R template/templates/404.tmpl => template/templates/error.tmpl +1 -1
@@ 1,3 1,3 @@
{{ define "content" }}
-<h1>404</h1>
+<p class="font-bold">{{ .error }}</p>
{{ end }}
M tixe.go => tixe.go +1 -1
@@ 32,7 32,7 @@ func handleNoRoute(c *gin.Context) {
session := sessions.Default(c)
user := session.Get("user")
- html := template.TmplEngine.Render("404.tmpl", map[string]interface{}{"title": "404", "user": user})
+ html := template.TmplEngine.Render("error.tmpl", map[string]interface{}{"title": "404", "user": user, "error": "404"})
c.Data(http.StatusNotFound, "text/html", html)
}
A util/error.go => util/error.go +25 -0
@@ 0,0 1,25 @@
+/*
+ * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
+ *
+ * This file is licensed under AGPL-3.0-or-later, see NOTICE and LICENSE for
+ * more information.
+ */
+package util
+
+import (
+ "net/http"
+ "tixe/template"
+
+ "github.com/gin-gonic/gin"
+)
+
+func RenderError(c *gin.Context, title string, errStr string, user interface{}) {
+ var html []byte
+ if user == nil {
+ html = template.TmplEngine.Render("error.tmpl", map[string]interface{}{"title": title, "error": errStr, "notauthed": true})
+ } else {
+ html = template.TmplEngine.Render("error.tmpl", map[string]interface{}{"title": title, "error": errStr, "user": user})
+ }
+ c.Data(http.StatusInternalServerError, "text/html", html)
+ c.Abort()
+}