完成关于异常的定义与处理
This commit is contained in:
122
exception.go
Normal file
122
exception.go
Normal file
@ -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
|
||||
}
|
Reference in New Issue
Block a user