gin icon indicating copy to clipboard operation
gin copied to clipboard

Need a gracefull way to set Error instead of c.Error?

Open sleagon opened this issue 4 years ago • 9 comments

  • go version: 1.12.6
  • gin version (or commit ref): 1.4.0
  • operating system: Mac osx

Description

We are use golangci as our lint tool, errchek is one of the basic rule. We use c.Error to append errors happened in the controller and there is no need to handle return value of c.Error. Every time we use c.Error, we need add a comment to disable lint for that line.

router.GET("/:name", func(c *gin.Context) {
                // do sth
		c.Error(err)  // nolint: errcheck
})

What's more, we do not want our application panic when err is nil.

sleagon avatar Aug 15 '19 12:08 sleagon

It seems to me that this is an implementation detail, not something that merits a framework change.

austinheap avatar Aug 23 '19 21:08 austinheap

It seems to me that this is an implementation detail, not something that merits a framework change.

c.Erroris not quite friendly to developers, add a simple method will solve it. Otherwise, we need add a utils.WithError to do this kind of stuff.

sleagon avatar Aug 27 '19 02:08 sleagon

I'm also frustrated with c.Error. It against gonlangci-lint. Is possible to erase return *Error ? What I am using now is that set my own key "error" for each error handling, when an error occurred. c.Set("error", err). Then in my error handler middleware to handle it. I just think that gin provided an Errors as a list to concentrate handle error, why we cannot use it well.

LiangXianSen avatar Sep 28 '19 08:09 LiangXianSen

my linter is also unstatisfy with the return error.

coolyrat avatar Dec 14 '19 10:12 coolyrat

feel free to close this, by the way, if anyone want to catch error and format the response especially doing I18N at the same time, here is a extremely simple and clean package to do this.

sleagon avatar Jul 22 '20 04:07 sleagon

Any good solution for the issue?

c.Error(err) would be good if no linter warning, the only one which does not return *Error is AbortWithJSON

inv2004 avatar Jun 21 '22 18:06 inv2004

@bestgopher There is open PR for it. Can you please comment there? c.Error cannot return nil, that is why the function is not for error-check and it would be good to have smth like c.GraceError which does not return error like c.AbortWithStatusJSON does

inv2004 avatar Jul 25 '22 10:07 inv2004

_ = c.Error(err) @inv2004 @sleagon

bestgopher avatar Jul 25 '22 11:07 bestgopher

@bestgopher Yes, but it looks more like workaround because c.AbortWithStatusJSON does not require the W/O

inv2004 avatar Jul 25 '22 12:07 inv2004