package utils import ( "fmt" "log" "net/http" ) func Err(w http.ResponseWriter, code int, a ...interface{}) { var msg string // format string for error message var hc int // HTTP error code var prefix string = fmt.Sprintf("Error %d - ", code) // ATTENTION: the used error codes in this switch statements should be // stable. Do not change them, just append to the list! switch code { case 1: msg = "Failed to load recipes from database." hc = http.StatusInternalServerError case 2: msg = "Could not parse recipe from database request." hc = http.StatusInternalServerError case 3: msg = "Exactly 1 'id' URL parameter expected but %d provided." hc = http.StatusBadRequest case 4: msg = "'id' URL parameter '%s' doas not match the regex '%s'." hc = http.StatusBadRequest case 5: msg = "Received error from database: '%s'." hc = http.StatusInternalServerError case 6: msg = "Expected exactly 1 recipe from database but got %d." hc = http.StatusInternalServerError // deprecated case 7: msg = "Exactly 1 'type' URL parameter expected but %d provided." hc = http.StatusBadRequest case 8: msg = "Form data does not contain recipe URL (key is '%s')." hc = http.StatusBadRequest case 9: msg = "Could not add recipe: '%s'." hc = http.StatusInternalServerError case 10: msg = "Expected exactly 1 recipe URL in '%s' but got %d (regex is '%s')." hc = http.StatusBadRequest case 11: msg = "Could not get recipe ID from database: '%s'." hc = http.StatusInternalServerError // deprecated case 12: msg = "Given recipe type '%s' is unknown." hc = http.StatusBadRequest default: msg = "An unknown error occured." hc = http.StatusInternalServerError } // format full error message final := fmt.Sprintf(prefix + msg, a...) // send message to log and user log.Println(final) http.Error(w, final, hc) }