Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http: Custom Error handlers #10123

Closed
mattfarina opened this issue Mar 10, 2015 · 6 comments
Closed

net/http: Custom Error handlers #10123

mattfarina opened this issue Mar 10, 2015 · 6 comments

Comments

@mattfarina
Copy link

In the http package Error displays just an error message. When building a web application you will often want to have style error pages matching the site or application. In order to get this with Go you need to implement your own Error handling along with any code that calls it.

For example, if you want custom error pages (including 404 not found pages) and want to serve files you need to bypass the built in file server and re-implement much of it to change the error page handling.

It would be far simpler to allow the Error to provide a default but be capable of being replaced.

@adg
Copy link
Contributor

adg commented Mar 10, 2015

It's hard to get excited about this.

Something like this?

var ErrorHandler func(w http.ResponseWriter, error string, code int) = func(w ResponseWriter, error string, code int) {
    w.Header().Set("Content-Type", "text/plain; charset=utf-8")
    w.WriteHeader(code)
    fmt.Fprintln(w, error)
}

func Error(w ResponseWriter, error string, code int) {
    ErrorHandleR(w, error, code)
}

Seems a bit gross TBH. I don't know that we want to add yet another hook.

@rsc
Copy link
Contributor

rsc commented Apr 10, 2015

It so easy to write your own functions.

@rsc rsc closed this as completed Apr 10, 2015
@mattfarina
Copy link
Author

@rsc how would one write an error handler for the not found case and use that in the built-in file server instead of the NotFound response? Something so I wouldn't need https://github.com/Masterminds/go-fileserver

@bradfitz
Copy link
Contributor

Give ServeContent or whatever a ResponseWriter which intercepts the 404/403/500/etc and writes its own message.

@mattfarina
Copy link
Author

@bradfitz that's an interesting take I had not thought of. It should work.

My issue with it is that it's not a good separation of concerns. It's a hack to work around this limitation. Something that's responsible with content now needs to look for certain types of responses and override the output to something custom.

It would be nice to have a clean solution.

@alecthomas
Copy link

There's also this and this that seem to bypass the ResponseWriter altogether.

@golang golang locked and limited conversation to collaborators May 6, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants