From 03c01d829df811448f3b82f07a94a08d70a722f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 25 Jun 2022 21:40:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=85=B3=E4=BA=8E=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E7=9A=84=E5=AE=9A=E4=B9=89=E4=B8=8E=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 0 .idea/exception.iml | 9 +++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 ++ .idea/watcherTasks.xml | 9 +++ .idea/workspace.xml | 93 +++++++++++++++++++++++++++++++ abstrace.go | 26 +++++++++ code.go | 75 +++++++++++++++++++++++++ exception.go | 122 +++++++++++++++++++++++++++++++++++++++++ go.mod | 3 + 10 files changed, 351 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/exception.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/watcherTasks.xml create mode 100644 .idea/workspace.xml create mode 100644 abstrace.go create mode 100644 code.go create mode 100644 exception.go create mode 100644 go.mod diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/.idea/exception.iml b/.idea/exception.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/exception.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..4433a5c --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/watcherTasks.xml b/.idea/watcherTasks.xml new file mode 100644 index 0000000..27aa2e8 --- /dev/null +++ b/.idea/watcherTasks.xml @@ -0,0 +1,9 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..2b86b2c --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,93 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + \ No newline at end of file diff --git a/abstrace.go b/abstrace.go new file mode 100644 index 0000000..814b725 --- /dev/null +++ b/abstrace.go @@ -0,0 +1,26 @@ +// Package exception ... +// +// Description : exception ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2022-06-25 21:11 +package exception + +// IException 异常的接口定义 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date *: 21:05 2022/6/25 +type IException interface { + // Error 兼容 go 内置 error + Error() string + // GetCode *获取错误码 + GetCode() interface{} + // GetMessage *获取错误信息 + GetMessage() string + // GetData 获取异常时的返回数据 + GetData() map[string]interface{} + // GetHttpCode *获取当前异常要返回的http状态码, 不设置则 默认 200 + GetHttpCode() int +} diff --git a/code.go b/code.go new file mode 100644 index 0000000..08cf63f --- /dev/null +++ b/code.go @@ -0,0 +1,75 @@ +// Package exception ... +// +// Description : exception ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2022-06-25 21:03 +package exception + +import ( + "fmt" + "net/http" +) + +var ( + // CodeTable 响应状态码和文案的映射表 + // + // Author : go_developer@163.com<白茶清欢> + // + // Date : 20:53 2022/6/25 + codeTable map[interface{}]string +) + +func init() { + // 规避没调用 InitCodeTable 导致空指针 + codeTable = make(map[interface{}]string) +} + +// InitCodeTable 初始化码表, 同时只指定代表业务成功的状态码 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 20:55 2022/6/25 +func InitCodeTable(table map[interface{}]string, convertDefaultSuccessCode interface{}) { + codeTable = table + if nil == convertDefaultSuccessCode { + convertDefaultSuccessCode = 0 + } + defaultSuccessCode = convertDefaultSuccessCode +} + +var ( + // messageWithCode 自动在message文案后追加状态码 + messageWithCode = true + // defaultHttpCode 默认的http状态码 + defaultHttpCode = http.StatusOK + // defaultSuccessCode 默认代表成功的状态码 + defaultSuccessCode interface{} +) + +// MessageWithoutCode 关闭在文案后追加状态码 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 21:15 2022/6/25 +func MessageWithoutCode() { + messageWithCode = false +} + +// getMessage 根据code获取文案 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 21:16 2022/6/25 +func getMessage(code interface{}) string { + message, exist := codeTable[code] + if !exist { + // 无论是否开启 messageWithCode , 未知错误强行带 code + return fmt.Sprintf("未知错误【%v】", codeTable) + } + if messageWithCode { + return fmt.Sprintf(message+"【%v】", codeTable) + } + return message +} diff --git a/exception.go b/exception.go new file mode 100644 index 0000000..4afc495 --- /dev/null +++ b/exception.go @@ -0,0 +1,122 @@ +// Package exception ... +// +// Description : exception ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date *: 2022-06-25 21:04 +package exception + +import "errors" + +// Exception 异常接口的具体实现 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date *: 21:09 2022/6/25 +type Exception struct { + code interface{} + message string + httpCode int + data map[string]interface{} +} + +func (e *Exception) Error() string { + return e.GetMessage() +} + +func (e *Exception) GetCode() interface{} { + return e.code +} + +func (e *Exception) GetMessage() string { + return e.message +} + +func (e *Exception) GetData() map[string]interface{} { + return e.data +} + +func (e *Exception) GetHttpCode() int { + return e.httpCode +} + +// NewWithCode 仅使用错误码实例化异常 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 21:22 2022/6/25 +func NewWithCode(code interface{}) *Exception { + return New(code, defaultHttpCode, map[string]interface{}{}) +} + +// NewWithCodeAndHttpCode 使用 code + http_code 获取实例 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 21:25 2022/6/25 +func NewWithCodeAndHttpCode(code interface{}, httpCode int) *Exception { + return New(code, httpCode, nil) +} + +// NewWithCodeAndData 使用 code + data 获取异常实例 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 21:28 2022/6/25 +func NewWithCodeAndData(code interface{}, data map[string]interface{}) *Exception { + return New(code, defaultHttpCode, data) +} + +// New 获取异常实例 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 21:28 2022/6/25 +func New(code interface{}, httpCode int, data map[string]interface{}) *Exception { + if nil == data { + // 保证数据结构的一致性, 同时避免后续使用出现空指针 + data = map[string]interface{}{} + } + return &Exception{ + code: code, + message: getMessage(code), + httpCode: httpCode, + data: data, + } +} + +// NewFromError 从error转换为Exception +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 21:36 2022/6/25 +func NewFromError(code interface{}, err error) *Exception { + if nil == err { + return nil + } + return New(code, defaultHttpCode, map[string]interface{}{ + "error": err.Error(), + }) +} + +// ToError 转换成内置error +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 21:37 2022/6/25 +func ToError(e *Exception) error { + if nil == e { + return nil + } + return errors.New(e.GetMessage()) +} + +// IsSuccess 判断一个异常是否为成功 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 21:34 2022/6/25 +func IsSuccess(e *Exception) bool { + return nil == e || e.GetCode() == defaultSuccessCode +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..cb8e24a --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.zhangdeman.cn/zhangdeman/exception + +go 1.17