From 9dfbbbcda4b388f8070cab1bdc1585e8a54a5794 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Thu, 30 Apr 2015 10:16:49 -0600 Subject: [PATCH] errors: Pointer to handler prevents nil pointer errors in handling (fixes #15) If we do not use a pointer here, the startup function that opens the log file stores the log file in a copy of the handler, not the same instance of the handler, causing panics during requests, which is bad, especially when the response is gzipped: the next recover() is beyond the gzip handler, so the browser downloads a gz file instead. --- middleware/errors/errors.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/middleware/errors/errors.go b/middleware/errors/errors.go index 36c7c5d7..392c31e8 100644 --- a/middleware/errors/errors.go +++ b/middleware/errors/errors.go @@ -113,8 +113,11 @@ func (h ErrorHandler) errorPage(w http.ResponseWriter, code int) { http.Error(w, defaultBody, code) } -func parse(c middleware.Controller) (ErrorHandler, error) { - handler := ErrorHandler{ErrorPages: make(map[int]string)} +func parse(c middleware.Controller) (*ErrorHandler, error) { + // Very important that we make a pointer because the Startup + // function that opens the log file must have access to the + // same instance of the handler, not a copy. + handler := &ErrorHandler{ErrorPages: make(map[int]string)} optionalBlock := func() (bool, error) { var hadBlock bool