瀏覽代碼

Merge pull request #910 from ammario/develop

panic if err is nil on c.Error
Javier Provecho Fernandez 8 年之前
父節點
當前提交
e0518111f8
共有 2 個文件被更改,包括 11 次插入0 次删除
  1. 4 0
      context.go
  2. 7 0
      context_test.go

+ 4 - 0
context.go

@@ -149,7 +149,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:

+ 7 - 0
context_test.go

@@ -1014,6 +1014,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) {