From 781cbd19f02c42795bdc3adc2fda287d95f91500 Mon Sep 17 00:00:00 2001 From: Ammar Bandukwala Date: Mon, 8 May 2017 19:04:22 -0500 Subject: [PATCH] panic if err is nil on c.Error A panic here provides a more informative stack trace than the panic which would otherwise occur while errors are being collected. --- context.go | 4 ++++ context_test.go | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/context.go b/context.go index 7b57150..dd0dc5d 100644 --- a/context.go +++ b/context.go @@ -144,7 +144,11 @@ func (c *Context) AbortWithError(code int, err error) *Error { // It's a good idea to call Error for each error that occurred during the resolution of a request. // A middleware can be used to collect all the errors // and push them to a database together, print a log, or append it in the HTTP response. +// Error will panic if err is nil. func (c *Context) Error(err error) *Error { + if err == nil { + panic("err is nil") + } var parsedError *Error switch err.(type) { case *Error: diff --git a/context_test.go b/context_test.go index 0feeca8..b4c1f08 100644 --- a/context_test.go +++ b/context_test.go @@ -852,6 +852,13 @@ func TestContextError(t *testing.T) { assert.Equal(t, c.Errors[1].Type, ErrorTypePublic) assert.Equal(t, c.Errors.Last(), c.Errors[1]) + + defer func() { + if recover() == nil { + t.Error("didn't panic") + } + }() + c.Error(nil) } func TestContextTypedError(t *testing.T) {