-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 支持使用 super.RegError 函数为错误注册全局错误码,使用 super.GetErrorCode 根据错误获取全局错误码
- Loading branch information
1 parent
31ad0ee
commit 1dcbd0a
Showing
1 changed file
with
22 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |