From 1f185f329bb82c87b250fb2312ae873d69a20d38 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sat, 22 Mar 2025 13:59:00 +0800 Subject: Use a custom errPage500 --- http_error_page.go | 6 ++++++ http_handle_group_index.go | 22 +++++++++++----------- http_handle_index.go | 2 +- http_handle_login.go | 8 ++++---- http_handle_repo_commit.go | 6 +++--- http_handle_repo_contrib_index.go | 6 +++--- http_handle_repo_contrib_one.go | 14 +++++++------- http_handle_repo_log.go | 4 ++-- http_handle_repo_raw.go | 10 +++++----- http_handle_repo_tree.go | 14 +++++++------- http_server.go | 10 +++++----- http_template.go | 9 +++++++-- templates/500.tmpl | 25 +++++++++++++++++++++++++ 13 files changed, 86 insertions(+), 50 deletions(-) create mode 100644 templates/500.tmpl diff --git a/http_error_page.go b/http_error_page.go index 77fdc86..0ddf055 100644 --- a/http_error_page.go +++ b/http_error_page.go @@ -23,3 +23,9 @@ func errorPage451(w http.ResponseWriter, params map[string]any, msg string) { params["complete_error_msg"] = msg _ = templates.ExecuteTemplate(w, "451", params) } + +func errorPage500(w http.ResponseWriter, params map[string]any, msg string) { + w.WriteHeader(http.StatusInternalServerError) + params["complete_error_msg"] = msg + _ = templates.ExecuteTemplate(w, "500", params) +} diff --git a/http_handle_group_index.go b/http_handle_group_index.go index 0667a11..d8e496f 100644 --- a/http_handle_group_index.go +++ b/http_handle_group_index.go @@ -59,7 +59,7 @@ func httpHandleGroupIndex(writer http.ResponseWriter, request *http.Request, par errorPage404(writer, params) return } else if err != nil { - http.Error(writer, "Error getting group: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error getting group: "+err.Error()) return } @@ -72,7 +72,7 @@ func httpHandleGroupIndex(writer http.ResponseWriter, request *http.Request, par AND group_id = $2 `, params["user_id"].(int), groupID).Scan(&count) if err != nil { - http.Error(writer, "Error checking access: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error checking access: "+err.Error()) return } directAccess := (count > 0) @@ -103,7 +103,7 @@ func httpHandleGroupIndex(writer http.ResponseWriter, request *http.Request, par contribReq, ).Scan(&newRepoID) if err != nil { - http.Error(writer, "Error creating repo: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error creating repo: "+err.Error()) return } @@ -118,12 +118,12 @@ func httpHandleGroupIndex(writer http.ResponseWriter, request *http.Request, par newRepoID, ) if err != nil { - http.Error(writer, "Error updating repo path: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error updating repo path: "+err.Error()) return } if err = gitInit(filePath); err != nil { - http.Error(writer, "Error initializing repo: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error initializing repo: "+err.Error()) return } @@ -139,7 +139,7 @@ func httpHandleGroupIndex(writer http.ResponseWriter, request *http.Request, par WHERE group_id = $1 `, groupID) if err != nil { - http.Error(writer, "Error getting repos: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error getting repos: "+err.Error()) return } defer rows.Close() @@ -147,13 +147,13 @@ func httpHandleGroupIndex(writer http.ResponseWriter, request *http.Request, par for rows.Next() { var name, description string if err = rows.Scan(&name, &description); err != nil { - http.Error(writer, "Error getting repos: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error getting repos: "+err.Error()) return } repos = append(repos, nameDesc{name, description}) } if err = rows.Err(); err != nil { - http.Error(writer, "Error getting repos: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error getting repos: "+err.Error()) return } @@ -164,7 +164,7 @@ func httpHandleGroupIndex(writer http.ResponseWriter, request *http.Request, par WHERE parent_group = $1 `, groupID) if err != nil { - http.Error(writer, "Error getting subgroups: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error getting subgroups: "+err.Error()) return } defer rows.Close() @@ -172,13 +172,13 @@ func httpHandleGroupIndex(writer http.ResponseWriter, request *http.Request, par for rows.Next() { var name, description string if err = rows.Scan(&name, &description); err != nil { - http.Error(writer, "Error getting subgroups: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error getting subgroups: "+err.Error()) return } subgroups = append(subgroups, nameDesc{name, description}) } if err = rows.Err(); err != nil { - http.Error(writer, "Error getting subgroups: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error getting subgroups: "+err.Error()) return } diff --git a/http_handle_index.go b/http_handle_index.go index 16d343a..a9a4b88 100644 --- a/http_handle_index.go +++ b/http_handle_index.go @@ -16,7 +16,7 @@ func httpHandleIndex(writer http.ResponseWriter, request *http.Request, params m groups, err = queryNameDesc(request.Context(), "SELECT name, COALESCE(description, '') FROM groups WHERE parent_group IS NULL") if err != nil { - http.Error(writer, "Error querying groups: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error querying groups: "+err.Error()) return } params["groups"] = groups diff --git a/http_handle_login.go b/http_handle_login.go index ed56e0a..424c42f 100644 --- a/http_handle_login.go +++ b/http_handle_login.go @@ -44,7 +44,7 @@ func httpHandleLogin(writer http.ResponseWriter, request *http.Request, params m renderTemplate(writer, "login", params) return } - http.Error(writer, "Error querying user information: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error querying user information: "+err.Error()) return } if passwordHash == "" { @@ -54,7 +54,7 @@ func httpHandleLogin(writer http.ResponseWriter, request *http.Request, params m } if passwordMatches, err = argon2id.ComparePasswordAndHash(password, passwordHash); err != nil { - http.Error(writer, "Error comparing password and hash: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error comparing password and hash: "+err.Error()) return } @@ -65,7 +65,7 @@ func httpHandleLogin(writer http.ResponseWriter, request *http.Request, params m } if cookieValue, err = randomUrlsafeStr(16); err != nil { - http.Error(writer, "Error getting random string: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error getting random string: "+err.Error()) return } @@ -86,7 +86,7 @@ func httpHandleLogin(writer http.ResponseWriter, request *http.Request, params m _, err = database.Exec(request.Context(), "INSERT INTO sessions (user_id, session_id) VALUES ($1, $2)", userID, cookieValue) if err != nil { - http.Error(writer, "Error inserting session: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error inserting session: "+err.Error()) return } diff --git a/http_handle_repo_commit.go b/http_handle_repo_commit.go index b3c2172..c15ae7d 100644 --- a/http_handle_repo_commit.go +++ b/http_handle_repo_commit.go @@ -44,13 +44,13 @@ func httpHandleRepoCommit(writer http.ResponseWriter, request *http.Request, par commitIDStrSpecNoSuffix = strings.TrimSuffix(commitIDStrSpec, ".patch") commitID = plumbing.NewHash(commitIDStrSpecNoSuffix) if commitObj, err = repo.CommitObject(commitID); err != nil { - http.Error(writer, "Error getting commit object: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error getting commit object: "+err.Error()) return } if commitIDStrSpecNoSuffix != commitIDStrSpec { var patchStr string if patchStr, err = fmtCommitPatch(commitObj); err != nil { - http.Error(writer, "Error formatting patch: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error formatting patch: "+err.Error()) return } fmt.Fprintln(writer, patchStr) @@ -68,7 +68,7 @@ func httpHandleRepoCommit(writer http.ResponseWriter, request *http.Request, par parentCommitHash, patch, err = fmtCommitAsPatch(commitObj) if err != nil { - http.Error(writer, "Error getting patch from commit: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error getting patch from commit: "+err.Error()) return } params["parent_commitHash"] = parentCommitHash.String() diff --git a/http_handle_repo_contrib_index.go b/http_handle_repo_contrib_index.go index af3f8b6..caffbe9 100644 --- a/http_handle_repo_contrib_index.go +++ b/http_handle_repo_contrib_index.go @@ -24,7 +24,7 @@ func httpHandleRepoContribIndex(writer http.ResponseWriter, request *http.Reques "SELECT id, COALESCE(title, 'Untitled'), status FROM merge_requests WHERE repo_id = $1", params["repo_id"], ); err != nil { - http.Error(writer, "Error querying merge requests: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error querying merge requests: "+err.Error()) return } defer rows.Close() @@ -33,13 +33,13 @@ func httpHandleRepoContribIndex(writer http.ResponseWriter, request *http.Reques var mrID int var mrTitle, mrStatus string if err = rows.Scan(&mrID, &mrTitle, &mrStatus); err != nil { - http.Error(writer, "Error scanning merge request: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error scanning merge request: "+err.Error()) return } result = append(result, idTitleStatus{mrID, mrTitle, mrStatus}) } if err = rows.Err(); err != nil { - http.Error(writer, "Error ranging over merge requests: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error ranging over merge requests: "+err.Error()) return } params["merge_requests"] = result diff --git a/http_handle_repo_contrib_one.go b/http_handle_repo_contrib_one.go index 29e2ef6..4a5f6b8 100644 --- a/http_handle_repo_contrib_one.go +++ b/http_handle_repo_contrib_one.go @@ -35,18 +35,18 @@ func httpHandleRepoContribOne(writer http.ResponseWriter, request *http.Request, "SELECT COALESCE(title, ''), status, source_ref, COALESCE(destination_branch, '') FROM merge_requests WHERE id = $1", mrIDInt, ).Scan(&title, &status, &srcRefStr, &dstBranchStr); err != nil { - http.Error(writer, "Error querying merge request: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error querying merge request: "+err.Error()) return } repo = params["repo"].(*git.Repository) if srcRefHash, err = getRefHash(repo, "branch", srcRefStr); err != nil { - http.Error(writer, "Error getting source ref hash: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error getting source ref hash: "+err.Error()) return } if srcCommit, err = repo.CommitObject(srcRefHash); err != nil { - http.Error(writer, "Error getting source commit: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error getting source commit: "+err.Error()) return } params["source_commit"] = srcCommit @@ -58,18 +58,18 @@ func httpHandleRepoContribOne(writer http.ResponseWriter, request *http.Request, dstBranchHash, err = getRefHash(repo, "branch", dstBranchStr) } if err != nil { - http.Error(writer, "Error getting destination branch hash: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error getting destination branch hash: "+err.Error()) return } if dstCommit, err = repo.CommitObject(dstBranchHash); err != nil { - http.Error(writer, "Error getting destination commit: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error getting destination commit: "+err.Error()) return } params["destination_commit"] = dstCommit if mergeBases, err = srcCommit.MergeBase(dstCommit); err != nil { - http.Error(writer, "Error getting merge base: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error getting merge base: "+err.Error()) return } mergeBaseCommit = mergeBases[0] @@ -77,7 +77,7 @@ func httpHandleRepoContribOne(writer http.ResponseWriter, request *http.Request, patch, err := mergeBaseCommit.Patch(srcCommit) if err != nil { - http.Error(writer, "Error getting patch: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error getting patch: "+err.Error()) return } params["file_patches"] = makeUsableFilePatches(patch) diff --git a/http_handle_repo_log.go b/http_handle_repo_log.go index 8d54d28..fbae0d5 100644 --- a/http_handle_repo_log.go +++ b/http_handle_repo_log.go @@ -21,12 +21,12 @@ func httpHandleRepoLog(writer http.ResponseWriter, _ *http.Request, params map[s repo = params["repo"].(*git.Repository) if refHash, err = getRefHash(repo, params["ref_type"].(string), params["ref_name"].(string)); err != nil { - http.Error(writer, "Error getting ref hash: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error getting ref hash: "+err.Error()) return } if commits, err = getRecentCommits(repo, refHash, -1); err != nil { - http.Error(writer, "Error getting recent commits: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error getting recent commits: "+err.Error()) return } params["commits"] = commits diff --git a/http_handle_repo_raw.go b/http_handle_repo_raw.go index db20791..6b69720 100644 --- a/http_handle_repo_raw.go +++ b/http_handle_repo_raw.go @@ -26,16 +26,16 @@ func httpHandleRepoRaw(writer http.ResponseWriter, request *http.Request, params params["path_spec"] = pathSpec if refHash, err = getRefHash(repo, params["ref_type"].(string), params["ref_name"].(string)); err != nil { - http.Error(writer, "Error getting ref hash: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error getting ref hash: "+err.Error()) return } if commitObj, err = repo.CommitObject(refHash); err != nil { - http.Error(writer, "Error getting commit object: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error getting commit object: "+err.Error()) return } if tree, err = commitObj.Tree(); err != nil { - http.Error(writer, "Error getting file tree: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error getting file tree: "+err.Error()) return } @@ -47,14 +47,14 @@ func httpHandleRepoRaw(writer http.ResponseWriter, request *http.Request, params var file *object.File var fileContent string if file, err = tree.File(pathSpec); err != nil { - http.Error(writer, "Error retrieving path: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error retrieving path: "+err.Error()) return } if redirectNoDir(writer, request) { return } if fileContent, err = file.Contents(); err != nil { - http.Error(writer, "Error reading file: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error reading file: "+err.Error()) return } fmt.Fprint(writer, fileContent) diff --git a/http_handle_repo_tree.go b/http_handle_repo_tree.go index 1238fd5..229b029 100644 --- a/http_handle_repo_tree.go +++ b/http_handle_repo_tree.go @@ -32,15 +32,15 @@ func httpHandleRepoTree(writer http.ResponseWriter, request *http.Request, param params["path_spec"] = pathSpec if refHash, err = getRefHash(repo, params["ref_type"].(string), params["ref_name"].(string)); err != nil { - http.Error(writer, "Error getting ref hash: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error getting ref hash: "+err.Error()) return } if commitObject, err = repo.CommitObject(refHash); err != nil { - http.Error(writer, "Error getting commit object: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error getting commit object: "+err.Error()) return } if tree, err = commitObject.Tree(); err != nil { - http.Error(writer, "Error getting file tree: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error getting file tree: "+err.Error()) return } @@ -58,14 +58,14 @@ func httpHandleRepoTree(writer http.ResponseWriter, request *http.Request, param var formattedHTML template.HTML if file, err = tree.File(pathSpec); err != nil { - http.Error(writer, "Error retrieving path: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error retrieving path: "+err.Error()) return } if redirectNoDir(writer, request) { return } if fileContent, err = file.Contents(); err != nil { - http.Error(writer, "Error reading file: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error reading file: "+err.Error()) return } lexer = chromaLexers.Match(pathSpec) @@ -73,14 +73,14 @@ func httpHandleRepoTree(writer http.ResponseWriter, request *http.Request, param lexer = chromaLexers.Fallback } if iterator, err = lexer.Tokenise(nil, fileContent); err != nil { - http.Error(writer, "Error tokenizing code: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error tokenizing code: "+err.Error()) return } var formattedHTMLStr bytes.Buffer style = chromaStyles.Get("autumn") formatter = chromaHTML.New(chromaHTML.WithClasses(true), chromaHTML.TabWidth(8)) if err = formatter.Format(&formattedHTMLStr, style, iterator); err != nil { - http.Error(writer, "Error formatting code: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error formatting code: "+err.Error()) return } formattedHTML = template.HTML(formattedHTMLStr.Bytes()) //#nosec G203 diff --git a/http_server.go b/http_server.go index 10b7d03..c86dae6 100644 --- a/http_server.go +++ b/http_server.go @@ -37,7 +37,7 @@ func (router *forgeHTTPRouter) ServeHTTP(writer http.ResponseWriter, request *ht userID, params["username"], err = getUserFromRequest(request) params["user_id"] = userID if err != nil && !errors.Is(err, http.ErrNoCookie) && !errors.Is(err, pgx.ErrNoRows) { - http.Error(writer, "Error getting user info from request: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error getting user info from request: "+err.Error()) return } @@ -131,12 +131,12 @@ func (router *forgeHTTPRouter) ServeHTTP(writer http.ResponseWriter, request *ht switch segments[sepIndex+3] { case "info": if err = httpHandleRepoInfo(writer, request, params); err != nil { - http.Error(writer, err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, err.Error()) } return case "git-upload-pack": if err = httpHandleUploadPack(writer, request, params); err != nil { - http.Error(writer, err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, err.Error()) } return } @@ -146,7 +146,7 @@ func (router *forgeHTTPRouter) ServeHTTP(writer http.ResponseWriter, request *ht if errors.Is(err, errNoRefSpec) { params["ref_type"] = "" } else { - http.Error(writer, "Error querying ref type: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error querying ref type: "+err.Error()) return } } @@ -154,7 +154,7 @@ func (router *forgeHTTPRouter) ServeHTTP(writer http.ResponseWriter, request *ht // TODO: subgroups if params["repo"], params["repo_description"], params["repo_id"], err = openRepo(request.Context(), groupPath, moduleName); err != nil { - http.Error(writer, "Error opening repo: "+err.Error(), http.StatusInternalServerError) + errorPage500(writer, params, "Error opening repo: "+err.Error()) return } diff --git a/http_template.go b/http_template.go index e8520a9..82a1497 100644 --- a/http_template.go +++ b/http_template.go @@ -3,12 +3,17 @@ package main -import "net/http" +import ( + "net/http" + + "go.lindenii.runxiyu.org/lindenii-common/clog" +) // renderTemplate abstracts out the annoyances of reporting template rendering // errors. func renderTemplate(w http.ResponseWriter, templateName string, params map[string]any) { if err := templates.ExecuteTemplate(w, templateName, params); err != nil { - http.Error(w, "Error rendering template: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "error rendering template: "+err.Error(), http.StatusInternalServerError) + clog.Error(err.Error()) } } diff --git a/templates/500.tmpl b/templates/500.tmpl new file mode 100644 index 0000000..2fe66b8 --- /dev/null +++ b/templates/500.tmpl @@ -0,0 +1,25 @@ +{{/* + SPDX-License-Identifier: AGPL-3.0-only + SPDX-FileContributor: Runxi Yu +*/}} +{{- define "500" -}} + + + + {{- template "head_common" . -}} + 500 Internal Server Error – {{ .global.forge_title }} + + + {{- template "header" . -}} +
+

500 Internal Server Error

+

{{- .complete_error_msg -}}

+
+
Lindenii Forge
+
+ + + +{{- end -}} -- cgit v1.2.3