From 1dcbd0a2203c5b0384969836bf4083f3fedce418 Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Wed, 19 Jul 2023 11:07:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E4=BD=BF=E7=94=A8=20?= =?UTF-8?q?super.RegError=20=E5=87=BD=E6=95=B0=E4=B8=BA=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E6=B3=A8=E5=86=8C=E5=85=A8=E5=B1=80=E9=94=99=E8=AF=AF=E7=A0=81?= =?UTF-8?q?=EF=BC=8C=E4=BD=BF=E7=94=A8=20super.GetErrorCode=20=E6=A0=B9?= =?UTF-8?q?=E6=8D=AE=E9=94=99=E8=AF=AF=E8=8E=B7=E5=8F=96=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/super/error.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 utils/super/error.go diff --git a/utils/super/error.go b/utils/super/error.go new file mode 100644 index 0000000..e89d276 --- /dev/null +++ b/utils/super/error.go @@ -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] +}