diff options
author | Runxi Yu <me@runxiyu.org> | 2025-02-13 09:33:19 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-02-13 09:33:19 +0800 |
commit | 91ca7bf1baf7ab077bdd63a7a3930c15af5be325 (patch) | |
tree | 1c670aa1da48f1e3edc7144a0c63231aa7b465e2 /http_handle_repo_tree.go | |
parent | TODO: Fix diff view (diff) | |
download | forge-91ca7bf1baf7ab077bdd63a7a3930c15af5be325.tar.gz forge-91ca7bf1baf7ab077bdd63a7a3930c15af5be325.tar.zst forge-91ca7bf1baf7ab077bdd63a7a3930c15af5be325.zip |
http_*.go: Use http.Error
Diffstat (limited to 'http_handle_repo_tree.go')
-rw-r--r-- | http_handle_repo_tree.go | 23 |
1 files changed, 11 insertions, 12 deletions
diff --git a/http_handle_repo_tree.go b/http_handle_repo_tree.go index b1a2d62..cd88a6f 100644 --- a/http_handle_repo_tree.go +++ b/http_handle_repo_tree.go @@ -3,7 +3,6 @@ package main import ( "bytes" "errors" - "fmt" "html/template" "net/http" "path" @@ -23,31 +22,31 @@ func handle_repo_tree(w http.ResponseWriter, r *http.Request, params map[string] if errors.Is(err, err_no_ref_spec) { ref_type = "head" } else { - fmt.Fprintln(w, "Error querying ref type:", err.Error()) + 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 { - fmt.Fprintln(w, "Error opening repo:", err.Error()) + 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 { - fmt.Fprintln(w, "Error getting ref hash:", err.Error()) + http.Error(w, "Error getting ref hash:: "+err.Error(), http.StatusInternalServerError) return } commit_object, err := repo.CommitObject(ref_hash) if err != nil { - fmt.Fprintln(w, "Error getting commit object:", err.Error()) + http.Error(w, "Error getting commit object:: "+err.Error(), http.StatusInternalServerError) return } tree, err := commit_object.Tree() if err != nil { - fmt.Fprintln(w, "Error getting file tree:", err.Error()) + http.Error(w, "Error getting file tree:: "+err.Error(), http.StatusInternalServerError) return } @@ -59,7 +58,7 @@ func handle_repo_tree(w http.ResponseWriter, r *http.Request, params map[string] if err != nil { file, err := tree.File(path_spec) if err != nil { - fmt.Fprintln(w, "Error retrieving path:", err.Error()) + 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] == '/' { @@ -68,7 +67,7 @@ func handle_repo_tree(w http.ResponseWriter, r *http.Request, params map[string] } file_contents, err := file.Contents() if err != nil { - fmt.Fprintln(w, "Error reading file:", err.Error()) + http.Error(w, "Error reading file:: "+err.Error(), http.StatusInternalServerError) return } lexer := chroma_lexers.Match(path_spec) @@ -77,7 +76,7 @@ func handle_repo_tree(w http.ResponseWriter, r *http.Request, params map[string] } iterator, err := lexer.Tokenise(nil, file_contents) if err != nil { - fmt.Fprintln(w, "Error tokenizing code:", err.Error()) + http.Error(w, "Error tokenizing code:: "+err.Error(), http.StatusInternalServerError) return } var formatted_unencapsulated bytes.Buffer @@ -85,7 +84,7 @@ func handle_repo_tree(w http.ResponseWriter, r *http.Request, params map[string] 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 { - fmt.Fprintln(w, "Error formatting code:", err.Error()) + http.Error(w, "Error formatting code:: "+err.Error(), http.StatusInternalServerError) return } formatted_encapsulated := template.HTML(formatted_unencapsulated.Bytes()) @@ -93,7 +92,7 @@ func handle_repo_tree(w http.ResponseWriter, r *http.Request, params map[string] err = templates.ExecuteTemplate(w, "repo_tree_file", params) if err != nil { - fmt.Fprintln(w, "Error rendering template:", err.Error()) + http.Error(w, "Error rendering template:: "+err.Error(), http.StatusInternalServerError) return } return @@ -110,7 +109,7 @@ func handle_repo_tree(w http.ResponseWriter, r *http.Request, params map[string] err = templates.ExecuteTemplate(w, "repo_tree_dir", params) if err != nil { - fmt.Fprintln(w, "Error rendering template:", err.Error()) + http.Error(w, "Error rendering template:: "+err.Error(), http.StatusInternalServerError) return } } |