增加异常的定义

This commit is contained in:
白茶清欢 2022-05-01 21:11:52 +08:00
parent 61b8ff415e
commit 31fb310058
1 changed files with 70 additions and 0 deletions

70
curl/exception.go Normal file
View File

@ -0,0 +1,70 @@
// Package curl ...
//
// Author: go_developer@163.com<白茶清欢>
//
// Description: 定义异常
//
// File: exception.go
//
// Version: 1.0.0
//
// Date: 2022/05/01 20:40:08
package curl
import "strings"
// Exception 定义异常
//
// Author : go_developer@163.com<白茶清欢>
//
// Description: 对于各种异常的描述
//
// Date: 2022/05/01 20:48:21
type Exception struct {
Code string `json:"code"` // 异常的标识码
Message string `json:"message"` // 异常的描述
}
const (
// BindRouterParamNotFound 绑定到路由的参数不存在
BindRouterParamNotFound = "404001"
// ResponseCodeNotFound 响应结果获取不到状态码字段
ResponseCodeNotFound = "404002"
// ResponseMessageNotFound 响应结果获取不到状态码描述字段
ResponseMessageNotFound = "404003"
// ResponseDataNotFound 响应结果获取不到数据字段
ResponseDataNotFound = "404004"
// DomainInvalid 域名设置错误, 必须是 http:// 或者 https:// 开头
DomainInvalid = "400001"
)
// exceptionTable ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Description: 异常信息表
//
// Date: 2022/05/01 21:02:55
var exceptionTable = map[string]string{
BindRouterParamNotFound: "绑定到路由的参数{{bind_router_param}}不存在, 请确认参数配置",
ResponseCodeNotFound: "基于配置无法获取响应体状态码, 请检查 response_code_field 的配置, 当前配置 {response_code_field}",
ResponseMessageNotFound: "基于配置无法获取响应体状态码, 请检查 response_code_message 的配置, 当前配置 {response_code_message}",
ResponseDataNotFound: "基于配置无法获取响应体状态码, 请检查 {response_code_data} 的配置, 当前配置 {response_code_data}",
DomainInvalid: "api域名配置非法, 必须以 http:// 或者 https:// 开头, 当前配置 {domain}",
}
// NewException 实力化异常
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2022/05/01 21:02:07
func NewException(code string, exceptionData map[string]string) *Exception {
mes := exceptionTable[code]
for k, v := range exceptionData {
mes = strings.ReplaceAll(mes, "{"+k+"}", v)
}
return &Exception{
Code: code,
Message: mes,
}
}