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