3 ms·
I've started playing around with using the ResponseRecorder from httptest to create a middleware that records the response from a handler and then deals with it
by marcus_holmes 3y ago
I've started playing around with using the ResponseRecorder from httptest to create a middleware that records the response from a handler and then deals with it appropriately:
- if it's an error then display an actual error page (or redirect to login, or whatever)
- if it's 200 OK then minimise the logging to just "successfully processed request for GET /foo?bar"
I like OP's way of dealing with the "http.Error then return" problem, especially since I usually find I'm also logging the error (so slog.Error, then http.Error, then return, which is painful since I'm repeating myself for the Error calls). But I don't like messing with the http.Handler interface. I think there's an approach involving both - like creating a type that implements http.Handler and handles logging, error reporting and error redirection while still allowing the core function to just "return err". I'm going to experiment with this. Thanks for the thought OP :)