httpx: Add new redirect

This commit is contained in:
Tom Wiesing 2022-11-24 14:19:17 +01:00
parent 41d41035e3
commit 8e2d2cce3e
No known key found for this signature in database
5 changed files with 123 additions and 35 deletions

View file

@ -5,9 +5,6 @@ import (
"net/http"
)
var jsonInternalServerErr = []byte(`{"status":"internal server error"}`)
var jsonNotFound = []byte(`{"status":"not found"}`)
// JSON creates a new JSONHandler
func JSON[T any](f func(r *http.Request) (T, error)) JSONHandler[T] {
return JSONHandler[T](f)
@ -22,19 +19,8 @@ func (j JSONHandler[T]) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// call the function
result, err := j(r)
// entity not found
if err == ErrNotFound {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
w.Write(jsonNotFound)
return
}
// handle other errors
if err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
w.Write(jsonInternalServerErr)
// handle any errors
if jsonInterceptor.Intercept(w, r, err) {
return
}