在应用程序的每个模块中,将错误(常量错误)分组的好方法是什么?

huangapple go评论79阅读模式
英文:

Good way to group errors (constant errors) in each module of app

问题

我有一个返回 JSON 格式错误的 Go 应用程序(Web 服务),格式如下:

{errorString: "internal server error"}

这样不太好,因为 "internal server error" 只是一些程序员错误字符串,对客户端没有用处。解决方案是添加错误代码:

{errCode: 1, errorString: "internal server error"}

现在,客户端知道 1 表示 "internal server error",可以根据需要进行处理。例如,为用户显示消息 "Internal Server Error" 或者(在我的情况下)用俄语显示相同的消息。

好的。

所以,显然我需要一个文件来描述所有错误常量。
例如,errors.go:

const (
   ErrNo = iota
   
   // 通用错误
   ErrNotFound
   ErrInternalServerError
   
   **// 更具体的错误**
)

问题在于更具体的错误部分。

我有两种方式:

  1. 将所有错误定义放在一个文件 errors.go 中。
  2. 尝试将特定的错误放在每个相关文件中:

我的控制器分为几个文件,位于 server 包中:

clienthandler.go -- 处理客户端请求,
orderhandler.go -- 处理订单请求等等。

特定的客户端错误应该放在 clienthandler.go 中,订单错误应该放在 orderhandler.go 中。

但是如何实现呢?

我知道一种简单的解决方案:
为每个控制器设置一些最大错误数量,例如 1000。

clienthandler.go

package server

const (
   ErrCheckIdCity = 1000*1 + iota
   ErrCheckName
)

这样就为该文件保留了 1000 个错误(从 1000 到 1999)。

orderhandler.go

package server

const (
   ErrCheckIdCity = 1000*2 + iota
   ErrCheckItem
)

这样就为该文件保留了 1000 个错误(从 2000 到 2999)。

但缺点是我们限制了每个控制器的错误数量为 1000。

也许有更好的方法吗?
或者我只需要使用一个全局的 errors.go 文件就可以了吗?

英文:

I have an Go app (web service) which returns errors in json like this:

{errorString: "internal server error"}

It's not ok because "internal server error" is just some programmer error string not usefull for client. The solution is to add error codes:

{errCode: 1, errorString: "internal server error"}

Now, client known that 1 means "internal server error" and can process it as want. For example, show for user message "Internal Server Error" or (in my case) the same in russian lang.

Ok.

So, obviously i need some file where all error constants will be described.
For ex. errors.go

const (
   ErrNo = iota
   
   // Common Errors
   ErrNotFound
   ErrInternalServerError
   
   **// More specified errors**
)

The problem is in More specified errors section.

I have 2 ways:

  1. places all errors definiton in one file errors.go
  2. try to place specific errors to each related file:

my controller is devided on several files in package server:

clienthandler.go -- for client requests,
orderhandler.go -- for orders requests and so on.

specific client errors must be places in clienthandler.go, order errors in orderhandler.go

But how it can be realized?

I know one simple solution:
Take some max count of errors for each controller, for example 1000.

clienthandler.go

package server

const (
   ErrCheckIdCity = 1000*1 + iota
   ErrCheckName
)

that is 1000 errors (from 1000 to 1999) reserved for this file

orderhandler.go

package server

const (
   ErrCheckIdCity = 1000*2 + iota
   ErrCheckItem
)

that is 1000 errors (from 2000 to 2999) reserved for this file

But disadvantge is that we limit myself by 1000 errors per controller

May be thers is some better way?
Or i need just use one global errros.go file ) ?

答案1

得分: 3

将每个错误放在它产生的地方并导出它。

请查看我评论中的链接

var ErrInvalidParam = fmt.Errorf("无效参数[%s]", param)

如果您想添加错误代码,请创建一个满足错误接口的新类型,并添加适当的成员,如errorCode或相关数据。

如果需要,可以创建一个类似于errors.New的辅助构建方法。

英文:

Place each error where it's originated and export it.

See the link from my comment.

var ErrInvalidParam = fmt.Errorf(“invalid parameter [%s]”, param)

If you want to add an error code, create a new type satisfying the error interface and add the appropriate members like errorCode or related data.

If you want, create a build method as helper, similar as errors.New does

huangapple
  • 本文由 发表于 2015年12月17日 20:10:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/34334276.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定