-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
middleware: panic handler for crashes
- Loading branch information
Showing
4 changed files
with
65 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package middleware | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"runtime/debug" | ||
) | ||
|
||
func NewPanic(next http.Handler, loggerFunc func(v ...interface{})) *Panic { | ||
return &Panic{ | ||
Next: next, | ||
loggerFunc: loggerFunc, | ||
} | ||
} | ||
|
||
// Panic recovers from API panics and logs encountered panics | ||
type Panic struct { | ||
Next http.Handler | ||
loggerFunc func(v ...interface{}) | ||
} | ||
|
||
// It recovers from panics of all next handlers and logs them | ||
func (h *Panic) ServeHTTP(r http.ResponseWriter, req *http.Request) { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
h.loggerFunc("begin: recovered from panic") | ||
h.loggerFunc(fmt.Sprintf("unkown value of recover (%v)", r)) | ||
h.loggerFunc(fmt.Sprintf("url %v", req.URL.String())) | ||
h.loggerFunc(fmt.Sprintf("method %v", req.Method)) | ||
h.loggerFunc(fmt.Sprintf("remote address %v", req.RemoteAddr)) | ||
h.loggerFunc(fmt.Sprintf("stack strace of cause \n %v", string(debug.Stack()))) | ||
h.loggerFunc("end: recovered from panic") | ||
} | ||
}() | ||
h.Next.ServeHTTP(r, req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestPanic(t *testing.T) { | ||
crashedHandler := http.HandlerFunc(func(http.ResponseWriter, *http.Request) { | ||
panic("hello world") | ||
}) | ||
|
||
middleware := NewPanic(crashedHandler, t.Log) | ||
|
||
resp := httptest.NewRecorder() | ||
req := httptest.NewRequest(http.MethodGet, "/panic", nil) | ||
|
||
middleware.ServeHTTP(resp, req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters