-
Notifications
You must be signed in to change notification settings - Fork 576
Can't set response headers when implementing custom Recovery Formatter #241
Copy link
Copy link
Open
Description
The docs indicate that when overriding the default Recovery.Formatter, it's possible to set response headers. I'm currently attempting to set the Content-Type header to application/json, however it appears that doesn't stick and reverts to text/plain; charset=utf-8.
Example:
package main
import (
"fmt"
"net/http"
"github.com/urfave/negroni"
)
type PanicFormatter struct{}
func (t *PanicFormatter) FormatPanicError(rw http.ResponseWriter, r *http.Request, infos *negroni.PanicInformation) {
rw.Header().Set("Content-Type", "application/json")
fmt.Fprintf(rw, `{"error":"%s"}`, infos.RecoveredPanic)
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
panic("oh no!")
})
n := negroni.New()
recovery := negroni.NewRecovery()
recovery.Formatter = &PanicFormatter{}
n.Use(recovery)
n.UseHandler(mux)
http.ListenAndServe(":3003", n)
}$ curl -i localhost:3003
HTTP/1.1 500 Internal Server Error
Date: Fri, 16 Nov 2018 20:32:32 GMT
Content-Length: 18
Content-Type: text/plain; charset=utf-8
{"error":"oh no!"}Reactions are currently unavailable