ex14-recover-middleware

command
v0.0.0-...-c470439 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 28, 2019 License: MIT Imports: 5 Imported by: 0

README

Exercise #14: Panic/Recover Middleware

Exercise details

In the recover exercise we will be looking at the panic and 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:

  1. Logs the error, as well as the stack trace.
  2. Sets the status code to http.StatusInternalServerError (500) whenever a panic occurs.
  3. Write a "Something went wrong" message when a panic occurs.
  4. Ensure that partial writes and 200 headers aren't set even if the handler started writing to the http.ResponseWriter BEFORE the panic occurred (this one may be trickier)
  5. 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).

Bonus

As a bonus exercises you can also look at ways to ensure you don't lose functionality like implementing the http.Flusher or the http.Hijacker interfaces.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL