From: Chris Jaekl Date: Wed, 11 Nov 2020 05:05:35 +0000 (-0500) Subject: Rework error handling to use http.Error() X-Git-Url: https://jaekl.net/gitweb/?a=commitdiff_plain;h=508fafb1316cdc3a83a7b160a34b50017315977a;p=quanweb.git Rework error handling to use http.Error() --- diff --git a/main/handler.go b/main/handler.go index 599f5d5..d0771fa 100644 --- a/main/handler.go +++ b/main/handler.go @@ -17,7 +17,7 @@ const MAX_TERMS = 10 // ============================================================================ func bookMimeType(bookPath string) string { upper := strings.ToUpper(bookPath) - + if strings.HasSuffix(upper, ".EPUB") { return "application/epub+zip" } else if strings.HasSuffix(upper, ".PDF") { @@ -40,7 +40,7 @@ func efsPathForId(efsId int) string { func handler(w http.ResponseWriter, r *http.Request) { err := r.ParseForm() if nil != err { - fmt.Fprintln(w, "ERROR!", err) + handleError(w, err.Error()) return } @@ -56,32 +56,37 @@ func handler(w http.ResponseWriter, r *http.Request) { case "search": handleSearch(w, r) default: - fmt.Fprintf(w, "Unrecognized request: %s\n", r.URL.Path[1:]) - fmt.Fprintf(w, "id: %s\n", r.FormValue("id")) - - fmt.Fprintln(w, "URL: ", r.URL) - fmt.Fprintln(w, "Query: ", r.URL.Query()) + handleError( + w, + fmt.Sprintf( + "Unrecognized request: %s\nid: %s\nURL: %s\nQuery: %s\n", + r.URL.Path[1:], + r.FormValue("id"), + r.URL, + r.URL.Query())) } } // Download a book, based on the path stored in the DB func handleBook(w http.ResponseWriter, r *http.Request) { + fmt.Println("handleBook:", r.URL.Path) path := r.URL.Path[1:] tok := strings.Split(path, "/") if 2 != len(tok) { - fmt.Fprintln(w, "Unexpected path for book download:", path) + handleError(w, fmt.Sprintf("Unexpected path for book download: %s\n", path)) return } bookId, err := strconv.Atoi(tok[1]) if (nil != err) || (0 == bookId) { - fmt.Fprintln(w, "Invalid id for book download:", path, err) + msg := fmt.Sprintf("Invalid id for book download: \"%s\" (%s)\n", path, err.Error()) + handleError(w, msg) return } bookPath := queryBookPathById(bookId) if 0 == len(bookPath) { - fmt.Fprintln(w, "No book for ID:", bookId) + handleError(w, fmt.Sprintf("No book for ID: %s\n", bookId)) return } @@ -89,22 +94,22 @@ func handleBook(w http.ResponseWriter, r *http.Request) { mimeType := bookMimeType(bookPath) if 0 == len(mimeType) { - fmt.Fprintln(w, "Failed to determine MIME type for book:", bookPath) + handleError(w, fmt.Sprintf("Failed to determine MIME type for book: %s\n", bookPath)) return } var info os.FileInfo info, err = os.Stat(bookPath) if nil != err { - fmt.Fprintln(w, "Failed to read file metadata:", bookPath, err) + handleError(w, fmt.Sprintf("Failed to read file metadata: \"%s\" (%s)\n", bookPath, err.Error())) return } modTime := info.ModTime() - + var fd *os.File fd, err = os.Open(bookPath) if nil != err { - fmt.Fprintln(w, "Failed to open file:", bookPath, err) + handleError(w, fmt.Sprintf("Failed to open file: \"%s\" (%s)\n", bookPath, err.Error())) return } defer fd.Close() @@ -127,7 +132,7 @@ func handleDownload(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Invalid id for download:", path, err) return } - + mimeType := queryMimeTypeByEfsId(efsId) if 0 == len(mimeType) { fmt.Fprintln(w, "No MIME type found for id:", efsId) @@ -148,12 +153,17 @@ func handleDownload(w http.ResponseWriter, r *http.Request) { io.Copy(w, fd) } +func handleError(w http.ResponseWriter, msg string) { + fmt.Printf("ERROR: %s", msg) + http.Error(w, msg, http.StatusInternalServerError) +} + func handleInfo(w http.ResponseWriter, r *http.Request) { idParams := r.Form[PARAM_IDS] if 1 != len(idParams) { - fmt.Fprintln(w, "ERROR! Detected either zero or multiple ids= parameters. Exactly one expected.") - return - } + handleError(w, "ERROR! Detected either zero or multiple ids= parameters. Exactly one expected.") + return + } idParam := idParams[0] idStrings := strings.Split(idParam, ",") @@ -171,7 +181,7 @@ func handleInfo(w http.ResponseWriter, r *http.Request) { var jsonValue []byte jsonValue, err = json.Marshal(books) if nil != err { - fmt.Fprintln(w, "ERROR!", err) + handleError(w, err.Error()) } else { w.Write(jsonValue) } @@ -204,7 +214,7 @@ func handleSearch(w http.ResponseWriter, r *http.Request) { jsonValue, err := json.Marshal(ids) if nil != err { - fmt.Fprintln(w, "ERROR!", err) + handleError(w, err.Error()) } else { w.Write(jsonValue) }