From d7889bf3eab55f56d2ca94c462ca130fde705871 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Thu, 13 Feb 2025 19:05:22 +0800 Subject: [PATCH] http_handle_*.go: Fix http.Error calls Previously when I was converting the fmt.Fprintln calls into http.Error, there was a mistake in my sed command which caused some of the messages to have double colons. This should fix them. --- http_handle_login.go | 12 ++++++------ http_handle_repo_commit.go | 10 +++++----- http_handle_repo_index.go | 12 ++++++------ http_handle_repo_log.go | 8 ++++---- http_handle_repo_raw.go | 16 ++++++++-------- http_handle_repo_tree.go | 22 +++++++++++----------- diff --git a/http_handle_login.go b/http_handle_login.go index 70a8c8b70685b3fd36c32ad3e72c2690107fe6b6..2d2dbf6cd86f20a6bc119fe5d417e811dc23a426 100644 --- a/http_handle_login.go +++ b/http_handle_login.go @@ -16,7 +16,7 @@ func handle_login(w http.ResponseWriter, r *http.Request, params map[string]any) { if r.Method != "POST" { err := templates.ExecuteTemplate(w, "login", params) if err != nil { - http.Error(w, "Error rendering template:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error rendering template: "+err.Error(), http.StatusInternalServerError) } return } @@ -32,17 +32,17 @@ if errors.Is(err, pgx.ErrNoRows) { params["login_error"] = "Unknown username" err := templates.ExecuteTemplate(w, "login", params) if err != nil { - http.Error(w, "Error rendering template:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error rendering template: "+err.Error(), http.StatusInternalServerError) } return } - http.Error(w, "Error querying user information:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error querying user information: "+err.Error(), http.StatusInternalServerError) return } match, err := argon2id.ComparePasswordAndHash(password, password_hash) if err != nil { - http.Error(w, "Error comparing password and hash:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error comparing password and hash: "+err.Error(), http.StatusInternalServerError) return } @@ -50,7 +50,7 @@ if !match { params["login_error"] = "Invalid password" err := templates.ExecuteTemplate(w, "login", params) if err != nil { - http.Error(w, "Error rendering template:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error rendering template: "+err.Error(), http.StatusInternalServerError) return } return @@ -75,7 +75,7 @@ http.SetCookie(w, &cookie) _, err = database.Exec(r.Context(), "INSERT INTO sessions (user_id, session_id) VALUES ($1, $2)", user_id, cookie_value) if err != nil { - http.Error(w, "Error inserting session:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error inserting session: "+err.Error(), http.StatusInternalServerError) return } diff --git a/http_handle_repo_commit.go b/http_handle_repo_commit.go index 25f1331eb658090a810a2e61865a9e64cdb95a19..ee21f160ca1f006e0b10898ab6e17ceb3ed65628 100644 --- a/http_handle_repo_commit.go +++ b/http_handle_repo_commit.go @@ -21,7 +21,7 @@ func handle_repo_commit(w http.ResponseWriter, r *http.Request, params map[string]any) { group_name, repo_name, commit_id_specified_string := params["group_name"].(string), params["repo_name"].(string), params["commit_id"].(string) repo, description, err := open_git_repo(r.Context(), group_name, repo_name) if err != nil { - http.Error(w, "Error opening repo:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error opening repo: "+err.Error(), http.StatusInternalServerError) return } params["repo_description"] = description @@ -29,13 +29,13 @@ commit_id_specified_string_without_suffix := strings.TrimSuffix(commit_id_specified_string, ".patch") commit_id := plumbing.NewHash(commit_id_specified_string_without_suffix) commit_object, err := repo.CommitObject(commit_id) if err != nil { - http.Error(w, "Error getting commit object:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error getting commit object: "+err.Error(), http.StatusInternalServerError) return } if commit_id_specified_string_without_suffix != commit_id_specified_string { patch, err := format_patch_from_commit(commit_object) if err != nil { - http.Error(w, "Error formatting patch:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error formatting patch: "+err.Error(), http.StatusInternalServerError) return } fmt.Fprintln(w, patch) @@ -53,7 +53,7 @@ params["commit_id"] = commit_id_string parent_commit_hash, patch, err := get_patch_from_commit(commit_object) if err != nil { - http.Error(w, "Error getting patch from commit:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error getting patch from commit: "+err.Error(), http.StatusInternalServerError) return } params["parent_commit_hash"] = parent_commit_hash.String() @@ -81,7 +81,7 @@ params["file_patches"] = usable_file_patches err = templates.ExecuteTemplate(w, "repo_commit", params) if err != nil { - http.Error(w, "Error rendering template:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error rendering template: "+err.Error(), http.StatusInternalServerError) return } } diff --git a/http_handle_repo_index.go b/http_handle_repo_index.go index 25bc10787e14ccbad0ca44eb4b6b0fc2f34c6467..c9c4949978f64b1dedea7ef812e4b37aae0de784 100644 --- a/http_handle_repo_index.go +++ b/http_handle_repo_index.go @@ -8,31 +8,31 @@ func handle_repo_index(w http.ResponseWriter, r *http.Request, params map[string]any) { group_name, repo_name := params["group_name"].(string), params["repo_name"].(string) repo, description, err := open_git_repo(r.Context(), group_name, repo_name) if err != nil { - http.Error(w, "Error opening repo:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error opening repo: "+err.Error(), http.StatusInternalServerError) return } params["repo_description"] = description head, err := repo.Head() if err != nil { - http.Error(w, "Error getting repo HEAD:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error getting repo HEAD: "+err.Error(), http.StatusInternalServerError) return } params["ref"] = head.Name().Short() head_hash := head.Hash() recent_commits, err := get_recent_commits(repo, head_hash, 3) if err != nil { - http.Error(w, "Error getting recent commits:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error getting recent commits: "+err.Error(), http.StatusInternalServerError) return } params["commits"] = recent_commits commit_object, err := repo.CommitObject(head_hash) if err != nil { - http.Error(w, "Error getting commit object:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error getting commit object: "+err.Error(), http.StatusInternalServerError) return } tree, err := commit_object.Tree() if err != nil { - http.Error(w, "Error getting file tree:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error getting file tree: "+err.Error(), http.StatusInternalServerError) return } @@ -43,7 +43,7 @@ params["clone_url"] = generate_ssh_remote_url(group_name, repo_name) err = templates.ExecuteTemplate(w, "repo_index", params) if err != nil { - http.Error(w, "Error rendering template:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error rendering template: "+err.Error(), http.StatusInternalServerError) return } } diff --git a/http_handle_repo_log.go b/http_handle_repo_log.go index bdc0d780c5907801953184afda6423e6f2ca8e55..72943be72c64657bb6b08372dd7d412496081705 100644 --- a/http_handle_repo_log.go +++ b/http_handle_repo_log.go @@ -11,26 +11,26 @@ func handle_repo_log(w http.ResponseWriter, r *http.Request, params map[string]any) { group_name, repo_name, ref_name := params["group_name"].(string), params["repo_name"].(string), params["ref"].(string) repo, description, err := open_git_repo(r.Context(), group_name, repo_name) if err != nil { - http.Error(w, "Error opening repo:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error opening repo: "+err.Error(), http.StatusInternalServerError) return } params["repo_description"] = description ref, err := repo.Reference(plumbing.NewBranchReferenceName(ref_name), true) if err != nil { - http.Error(w, "Error getting repo reference:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error getting repo reference: "+err.Error(), http.StatusInternalServerError) return } ref_hash := ref.Hash() commits, err := get_recent_commits(repo, ref_hash, -1) if err != nil { - http.Error(w, "Error getting recent commits:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error getting recent commits: "+err.Error(), http.StatusInternalServerError) return } params["commits"] = commits err = templates.ExecuteTemplate(w, "repo_log", params) if err != nil { - http.Error(w, "Error rendering template:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error rendering template: "+err.Error(), http.StatusInternalServerError) return } } diff --git a/http_handle_repo_raw.go b/http_handle_repo_raw.go index ba67ac1947fd11caf5dde8eb0f00e8c9dc559b48..b134f8dc9400da3212bc52159271f08a864ee612 100644 --- a/http_handle_repo_raw.go +++ b/http_handle_repo_raw.go @@ -19,7 +19,7 @@ if err != nil { if errors.Is(err, err_no_ref_spec) { ref_type = "head" } else { - http.Error(w, "Error querying ref type:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error querying ref type: "+err.Error(), http.StatusInternalServerError) return } } @@ -28,25 +28,25 @@ params["ref_type"], params["ref"], params["path_spec"] = ref_type, ref_name, path_spec repo, description, err := open_git_repo(r.Context(), group_name, repo_name) if err != nil { - http.Error(w, "Error opening repo:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error opening repo: "+err.Error(), http.StatusInternalServerError) return } params["repo_description"] = description ref_hash, err := get_ref_hash_from_type_and_name(repo, ref_type, ref_name) if err != nil { - http.Error(w, "Error getting ref hash:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error getting ref hash: "+err.Error(), http.StatusInternalServerError) return } commit_object, err := repo.CommitObject(ref_hash) if err != nil { - http.Error(w, "Error getting commit object:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error getting commit object: "+err.Error(), http.StatusInternalServerError) return } tree, err := commit_object.Tree() if err != nil { - http.Error(w, "Error getting file tree:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error getting file tree: "+err.Error(), http.StatusInternalServerError) return } @@ -58,7 +58,7 @@ target, err = tree.Tree(path_spec) if err != nil { file, err := tree.File(path_spec) if err != nil { - http.Error(w, "Error retrieving path:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error retrieving path: "+err.Error(), http.StatusInternalServerError) return } if len(raw_path_spec) != 0 && raw_path_spec[len(raw_path_spec)-1] == '/' { @@ -67,7 +67,7 @@ return } file_contents, err := file.Contents() if err != nil { - http.Error(w, "Error reading file:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error reading file: "+err.Error(), http.StatusInternalServerError) return } fmt.Fprintln(w, file_contents) @@ -84,7 +84,7 @@ params["files"] = build_display_git_tree(target) err = templates.ExecuteTemplate(w, "repo_raw_dir", params) if err != nil { - http.Error(w, "Error rendering template:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error rendering template: "+err.Error(), http.StatusInternalServerError) return } } diff --git a/http_handle_repo_tree.go b/http_handle_repo_tree.go index cd88a6f541a04f7f2f1f49043f783ace5404b4dc..69bc40f87742e624aab1304d8993963ad9aa8a7f 100644 --- a/http_handle_repo_tree.go +++ b/http_handle_repo_tree.go @@ -22,31 +22,31 @@ if err != nil { if errors.Is(err, err_no_ref_spec) { ref_type = "head" } else { - http.Error(w, "Error querying ref type:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error querying ref type: "+err.Error(), http.StatusInternalServerError) return } } params["ref_type"], params["ref"], params["path_spec"] = ref_type, ref_name, path_spec repo, description, err := open_git_repo(r.Context(), group_name, repo_name) if err != nil { - http.Error(w, "Error opening repo:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error opening repo: "+err.Error(), http.StatusInternalServerError) return } params["repo_description"] = description ref_hash, err := get_ref_hash_from_type_and_name(repo, ref_type, ref_name) if err != nil { - http.Error(w, "Error getting ref hash:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error getting ref hash: "+err.Error(), http.StatusInternalServerError) return } commit_object, err := repo.CommitObject(ref_hash) if err != nil { - http.Error(w, "Error getting commit object:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error getting commit object: "+err.Error(), http.StatusInternalServerError) return } tree, err := commit_object.Tree() if err != nil { - http.Error(w, "Error getting file tree:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error getting file tree: "+err.Error(), http.StatusInternalServerError) return } @@ -58,7 +58,7 @@ target, err = tree.Tree(path_spec) if err != nil { file, err := tree.File(path_spec) if err != nil { - http.Error(w, "Error retrieving path:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error retrieving path: "+err.Error(), http.StatusInternalServerError) return } if len(raw_path_spec) != 0 && raw_path_spec[len(raw_path_spec)-1] == '/' { @@ -67,7 +67,7 @@ return } file_contents, err := file.Contents() if err != nil { - http.Error(w, "Error reading file:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error reading file: "+err.Error(), http.StatusInternalServerError) return } lexer := chroma_lexers.Match(path_spec) @@ -76,7 +76,7 @@ lexer = chroma_lexers.Fallback } iterator, err := lexer.Tokenise(nil, file_contents) if err != nil { - http.Error(w, "Error tokenizing code:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error tokenizing code: "+err.Error(), http.StatusInternalServerError) return } var formatted_unencapsulated bytes.Buffer @@ -84,7 +84,7 @@ style := chroma_styles.Get("autumn") formatter := chroma_formatters_html.New(chroma_formatters_html.WithClasses(true), chroma_formatters_html.TabWidth(8)) err = formatter.Format(&formatted_unencapsulated, style, iterator) if err != nil { - http.Error(w, "Error formatting code:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error formatting code: "+err.Error(), http.StatusInternalServerError) return } formatted_encapsulated := template.HTML(formatted_unencapsulated.Bytes()) @@ -92,7 +92,7 @@ params["file_contents"] = formatted_encapsulated err = templates.ExecuteTemplate(w, "repo_tree_file", params) if err != nil { - http.Error(w, "Error rendering template:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error rendering template: "+err.Error(), http.StatusInternalServerError) return } return @@ -109,7 +109,7 @@ params["files"] = build_display_git_tree(target) err = templates.ExecuteTemplate(w, "repo_tree_dir", params) if err != nil { - http.Error(w, "Error rendering template:: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Error rendering template: "+err.Error(), http.StatusInternalServerError) return } } -- 2.48.1