Merge pull request #2110 from zhulongcheng/panic-handler

feat(http): add panic handler
pull/10616/head
Chris Goller 2018-12-21 08:43:20 -06:00 committed by GitHub
commit 9c01be5377
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 115 additions and 2 deletions

View File

@ -1,20 +1,22 @@
package http
import (
"fmt"
"net/http"
"github.com/influxdata/platform"
"github.com/julienschmidt/httprouter"
)
// NewRouter returns a new router with a 404 handler that returns a JSON response.
// NewRouter returns a new router with a 404 handler and a panic handler.
func NewRouter() *httprouter.Router {
router := httprouter.New()
router.NotFound = http.HandlerFunc(notFoundHandler)
router.PanicHandler = panicHandler
return router
}
// notFoundHandler represents a 404 handler that return a JSON response
// notFoundHandler represents a 404 handler that return a JSON response.
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
pe := &platform.Error{
@ -24,3 +26,16 @@ func notFoundHandler(w http.ResponseWriter, r *http.Request) {
EncodeError(ctx, pe, w)
}
// panicHandler handles panics recovered from http handlers.
// It returns a json response with http status code 500 and the recovered error message.
func panicHandler(w http.ResponseWriter, r *http.Request, rcv interface{}) {
ctx := r.Context()
pe := &platform.Error{
Code: platform.EInternal,
Msg: "a panic has occurred",
Err: fmt.Errorf("%v", rcv),
}
EncodeError(ctx, pe, w)
}

View File

@ -97,3 +97,101 @@ func TestRouter_NotFound(t *testing.T) {
})
}
}
func TestRouter_Panic(t *testing.T) {
type fields struct {
method string
path string
handlerFn http.HandlerFunc
}
type args struct {
method string
path string
}
type wants struct {
statusCode int
contentType string
body string
}
tests := []struct {
name string
fields fields
args args
wants wants
}{
{
name: "no panic",
fields: fields{
method: "GET",
path: "/ping",
handlerFn: func(w http.ResponseWriter, r *http.Request) {
encodeResponse(r.Context(), w, http.StatusOK, map[string]string{"message": "pong"})
},
},
args: args{
method: "GET",
path: "/ping",
},
wants: wants{
statusCode: http.StatusOK,
contentType: "application/json; charset=utf-8",
body: `
{
"message": "pong"
}
`,
},
},
{
name: "panic",
fields: fields{
method: "GET",
path: "/ping",
handlerFn: func(w http.ResponseWriter, r *http.Request) {
panic("not implemented")
},
},
args: args{
method: "GET",
path: "/ping",
},
wants: wants{
statusCode: http.StatusInternalServerError,
contentType: "application/json; charset=utf-8",
body: `
{
"code": "internal error",
"msg": "a panic has occurred",
"err": "not implemented"
}`,
},
},
}
for _, tt := range tests[1:] {
t.Run(tt.name, func(t *testing.T) {
router := NewRouter()
router.HandlerFunc(tt.fields.method, tt.fields.path, tt.fields.handlerFn)
r := httptest.NewRequest(tt.args.method, tt.args.path, nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
res := w.Result()
content := res.Header.Get("Content-Type")
body, _ := ioutil.ReadAll(res.Body)
if res.StatusCode != tt.wants.statusCode {
t.Errorf("%q. get %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode)
}
if tt.wants.contentType != "" && content != tt.wants.contentType {
t.Errorf("%q. get %v, want %v", tt.name, content, tt.wants.contentType)
}
if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq {
t.Errorf("%q. get\n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body)
}
})
}
}