diff options
Diffstat (limited to 'handler.go')
-rw-r--r-- | handler.go | 33 |
1 files changed, 17 insertions, 16 deletions
@@ -33,7 +33,7 @@ func indexGet(w http.ResponseWriter, r *http.Request, db *Database, templateRoot log.Printf("Query: %s", cmd) rows, err := db.Backend.Query(cmd) if err != nil { - Err(w, 1) + http.Error(w, "Failed to load recipes from database.", 500) return } defer rows.Close() @@ -50,7 +50,7 @@ func indexGet(w http.ResponseWriter, r *http.Request, db *Database, templateRoot var element Element err := rows.Scan(&element.Id, &element.Title) if err != nil { - Err(w, 2) + http.Error(w, "Could not parse recipe from database request.", 500) return } else { elements = append(elements, element) @@ -69,7 +69,8 @@ func recipe(db *Database, templateRoot string) func(http.ResponseWriter, *http.R // get id from URL parameters ids := r.URL.Query()["id"] if len(ids) != 1 { - Err(w, 3, len(ids)) + msg := fmt.Sprintf("Exactly 1 'id' URL parameter expected but %d provided.", len(ids)) + http.Error(w, msg, 400) return } idStr := ids[0] @@ -77,7 +78,7 @@ func recipe(db *Database, templateRoot string) func(http.ResponseWriter, *http.R // validate id idRegex := regexp.MustCompile(VALID_ID_REGEX) if !(idRegex.MatchString(idStr)) { - Err(w, 4, idStr, VALID_ID_REGEX) + http.Error(w, "Bad 'id' URL parameter.", 400) return } @@ -88,7 +89,7 @@ func recipe(db *Database, templateRoot string) func(http.ResponseWriter, *http.R log.Printf("Query: %s", cmd) rows, err := db.Backend.Query(cmd) if err != nil { - Err(w, 5, err) + http.Error(w, "Database returned error: " + err.Error(), 500) return } defer rows.Close() @@ -109,7 +110,7 @@ func recipe(db *Database, templateRoot string) func(http.ResponseWriter, *http.R element.Id = idStr err := rows.Scan(&element.Title, &element.UpstreamUrl, &element.DescriptionMarkdown) if err != nil { - Err(w, 2) + http.Error(w, "Could not parse recipe from database request.", 500) return } else { elements = append(elements, element) @@ -118,7 +119,7 @@ func recipe(db *Database, templateRoot string) func(http.ResponseWriter, *http.R // check result if len(elements) != 1 { - Err(w, 6, len(elements)) + http.Error(w, "Expected exactly 1 recipe from database.", 500) return } @@ -150,7 +151,7 @@ func recipe_edit(db *Database, templateRoot string) func(http.ResponseWriter, *h // get id from URL parameters ids := r.URL.Query()["id"] if len(ids) != 1 { - Err(w, 3, len(ids)) + http.Error(w, "Exactly 1 'id' URL parameter expected.", 400) return } idStr := ids[0] @@ -158,7 +159,7 @@ func recipe_edit(db *Database, templateRoot string) func(http.ResponseWriter, *h // validate id idRegex := regexp.MustCompile(VALID_ID_REGEX) if !(idRegex.MatchString(idStr)) { - Err(w, 4, idStr, VALID_ID_REGEX) + http.Error(w, "Bad 'id' URL parameter.", 400) return } @@ -169,7 +170,7 @@ func recipe_edit(db *Database, templateRoot string) func(http.ResponseWriter, *h log.Printf("Query: %s", cmd) rows, err := db.Backend.Query(cmd) if err != nil { - Err(w, 5, err) + http.Error(w, "Got error from database: " + err.Error(), 500) return } defer rows.Close() @@ -190,7 +191,7 @@ func recipe_edit(db *Database, templateRoot string) func(http.ResponseWriter, *h element.Id = idStr err := rows.Scan(&element.Title, &element.UpstreamUrl, &element.DescriptionMarkdown) if err != nil { - Err(w, 2) + http.Error(w, "Could not parse recipe from database request.", 500) return } else { elements = append(elements, element) @@ -199,7 +200,7 @@ func recipe_edit(db *Database, templateRoot string) func(http.ResponseWriter, *h // check result if len(elements) != 1 { - Err(w, 6, len(elements)) + http.Error(w, "Did not get exactly one recipe from database.", 500) return } @@ -248,7 +249,7 @@ func image(storageRoot string) func(http.ResponseWriter, *http.Request) { // get ID ids := r.URL.Query()["id"] if len(ids) != 1 { - Err(w, 3, len(ids)) + http.Error(w, "Expected exactly one 'id' URL parameter.", 400) return } idStr := ids[0] @@ -256,7 +257,7 @@ func image(storageRoot string) func(http.ResponseWriter, *http.Request) { // validate ID idRegex := regexp.MustCompile(VALID_ID_REGEX) if !idRegex.MatchString(idStr) { - Err(w, 4, idStr, VALID_ID_REGEX) + http.Error(w, "Bad 'id' URL parameter.", 400) return } @@ -286,12 +287,12 @@ func add_recipes(db *Database, storageRoot string, staticRoot string) func(http. log.Println(cmd) res,err := db.Backend.Exec(cmd) if err != nil { - Err(w, 9, err) + http.Error(w, "Could not add recipe.", 500) return } id,err := res.LastInsertId() if err != nil { - Err(w, 11, err) + http.Error(w, "Expected exactly one recipe URL.", 400) return } else { log.Println("Added custom recipe.") |