offline-twitter/internal/webserver/response_helpers.go
Alessio 24364a26b0 REFACTOR: rework the rendering helpers
- rendering helpers moved to their own file (separate from response helpers)
- create a unified render helper instead of "buffered_render_basic_X" and "buffered_render_tweet_X"
	- this helper takes 2 data objects: one with global data (tweet trove, logged in user, etc) and one page-specific
	- this lets us remove the disgusting interface type
- modify the User List template to use UserIDs indexing into a global data object instead of a list of Users
2023-12-31 15:56:12 -06:00

39 lines
855 B
Go

package webserver
import (
"fmt"
"net/http"
"runtime/debug"
)
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)
}
func (app *Application) error_401(w http.ResponseWriter) {
http.Error(w, "Please log in or set an active session", 401)
}
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)
}
http.Error(w, "Server error :(", 500)
}