Skip to content

Commit

Permalink
feat: 支持使用 super.RegError 函数为错误注册全局错误码,使用 super.GetErrorCode 根据错误获取全局错误码
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Jul 19, 2023
1 parent 31ad0ee commit 1dcbd0a
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions utils/super/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package super

import (
"errors"
)

var errorMapper = make(map[error]int)

// RegError 通过错误码注册错误,返回错误的引用
func RegError(code int, message string) error {
if code == 0 {
panic("error code can not be 0")
}
err := errors.New(message)
errorMapper[err] = code
return err
}

// GetErrorCode 通过错误引用获取错误码,如果错误不存在则返回 0
func GetErrorCode(err error) int {
return errorMapper[err]
}

0 comments on commit 1dcbd0a

Please sign in to comment.