This would be really janky but one way to do it would be to set the error handler within the request's context, I'm not sure if we want to go down this path though, seems like a misuse of the context.
You can do it today outside of the stdlib by wrapping the http.ResponseWriter with your own writer and intercepting the call to WriteHeader, and then, using the status code from there to decide whether you want to serve an error.
It is a little slow, involved and quirky though.
meirf
changed the title
Custom error handlers for http.FileServer
net/http: custom error handlers for http.FileServer
Aug 23, 2018
Yup. The issue with that is implementing all of the response writer upgrade interfaces afterwards. Super janky especially when the underlying response writer may not support all of them.
Noticed that in Go 1.11 a error handler field was added to httputil's ReverseProxy to allow sending allow writing a custom error.
I think the same should be done for http.FileServer, http.ServeFile and http.ServeContent.
I don't see an easy way to do this without breaking the API or adding more API so maybe this is better for Go 2.
The text was updated successfully, but these errors were encountered: