Exercise #14: Panic/Recover Middleware
In the recover exercise we will be looking at the
recover mechanisms in Go and utilizing them to create middleware for an HTTP server.
Given a simple web server (see
main.go) that can potentially panic, create an
http.Handler that wraps the existing mux and will recover from any panics and then does the following:
- Logs the error, as well as the stack trace.
- Sets the status code to
http.StatusInternalServerError(500) whenever a panic occurs.
- Write a "Something went wrong" message when a panic occurs.
- Ensure that partial writes and 200 headers aren't set even if the handler started writing to the
http.ResponseWriterBEFORE the panic occurred (this one may be trickier)
- If the environment is set to be development, print the stack trace and the error to the webpage as well as to the logs. Otherwise default to the "Something went wrong" message described in (3).