2023-08-03 12:43:17 -03:00
|
|
|
package webserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-08-10 12:36:28 -03:00
|
|
|
"net/http"
|
|
|
|
"runtime/debug"
|
2023-08-03 12:43:17 -03:00
|
|
|
)
|
|
|
|
|
|
|
|
func panic_if(err error) {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// func (app *Application) error_400(w http.ResponseWriter) {
|
|
|
|
// http.Error(w, "Bad Request", 400)
|
|
|
|
// }
|
|
|
|
|
|
|
|
func (app *Application) error_400_with_message(w http.ResponseWriter, msg string) {
|
|
|
|
http.Error(w, fmt.Sprintf("Bad Request\n\n%s", msg), 400)
|
|
|
|
}
|
|
|
|
|
2023-12-24 14:09:46 -06:00
|
|
|
func (app *Application) error_401(w http.ResponseWriter) {
|
|
|
|
http.Error(w, "Please log in or set an active session", 401)
|
|
|
|
}
|
|
|
|
|
2023-08-03 12:43:17 -03:00
|
|
|
func (app *Application) error_404(w http.ResponseWriter) {
|
|
|
|
http.Error(w, "Not Found", 404)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *Application) error_500(w http.ResponseWriter, err error) {
|
|
|
|
trace := fmt.Sprintf("%s\n%s", err.Error(), debug.Stack())
|
|
|
|
err2 := app.ErrorLog.Output(2, trace) // Magic
|
|
|
|
if err2 != nil {
|
|
|
|
panic(err2)
|
|
|
|
}
|
2023-12-31 22:27:35 -06:00
|
|
|
|
2024-04-12 17:33:00 -07:00
|
|
|
// Reset the HTMX response to return an error toast and put it in the
|
2023-12-31 22:27:35 -06:00
|
|
|
w.Header().Set("HX-Reswap", "beforeend")
|
2024-04-12 17:33:00 -07:00
|
|
|
w.Header().Set("HX-Retarget", "#errorMessages")
|
2023-12-31 22:27:35 -06:00
|
|
|
w.Header().Set("HX-Push-Url", "false")
|
|
|
|
|
|
|
|
r := renderer{
|
|
|
|
Filenames: []string{get_filepath("tpl/http_500.tpl")},
|
2023-12-31 22:28:07 -06:00
|
|
|
TplName: "error-toast",
|
|
|
|
Data: struct {
|
2023-12-31 22:27:35 -06:00
|
|
|
ErrorMsg string
|
|
|
|
}{err.Error()},
|
|
|
|
}
|
|
|
|
r.BufferedRender(w)
|
2023-08-03 12:43:17 -03:00
|
|
|
}
|