From 98a6a283a5e9a85ededad9485d394555e4c09537 Mon Sep 17 00:00:00 2001 From: shanyuanyuan Date: Thu, 15 Aug 2019 19:57:58 +0800 Subject: [PATCH] Add a gracefull method to set error Context#GraceError. --- context.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/context.go b/context.go index d9fcc285..a811cbf5 100644 --- a/context.go +++ b/context.go @@ -186,6 +186,16 @@ func (c *Context) AbortWithError(code int, err error) *Error { return c.Error(err) } +// GraceError calls `Error` internally. +// This method does not panic when err is nil, and no return value. +// See Context.Error() for more details. +func (c *Context) GraceError(err error) { + if err == nil { + return + } + c.Error(err) // nolint: errcheck +} + /************************************/ /********* ERROR MANAGEMENT *********/ /************************************/