完成关于异常的定义与处理
This commit is contained in:
parent
8133dee472
commit
03c01d829d
0
.idea/.gitignore
vendored
Normal file
0
.idea/.gitignore
vendored
Normal file
9
.idea/exception.iml
Normal file
9
.idea/exception.iml
Normal file
@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="Go" enabled="true" />
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/exception.iml" filepath="$PROJECT_DIR$/.idea/exception.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
9
.idea/watcherTasks.xml
Normal file
9
.idea/watcherTasks.xml
Normal file
@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectTasksOptions">
|
||||
<enabled-global>
|
||||
<option value="go fmt" />
|
||||
<option value="goimports" />
|
||||
</enabled-global>
|
||||
</component>
|
||||
</project>
|
93
.idea/workspace.xml
Normal file
93
.idea/workspace.xml
Normal file
@ -0,0 +1,93 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="AutoImportSettings">
|
||||
<option name="autoReloadType" value="ALL" />
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="0c7a619f-b520-4d41-ab0d-cfa1799d3cdf" name="Changes" comment="" />
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||
<option name="LAST_RESOLUTION" value="IGNORE" />
|
||||
</component>
|
||||
<component name="FileTemplateManagerImpl">
|
||||
<option name="RECENT_TEMPLATES">
|
||||
<list>
|
||||
<option value="Go File" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="GOROOT" url="file://$USER_HOME$/go/go1.17.11" />
|
||||
<component name="Git.Settings">
|
||||
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
|
||||
</component>
|
||||
<component name="GitSEFilterConfiguration">
|
||||
<file-type-list>
|
||||
<filtered-out-file-type name="LOCAL_BRANCH" />
|
||||
<filtered-out-file-type name="REMOTE_BRANCH" />
|
||||
<filtered-out-file-type name="TAG" />
|
||||
<filtered-out-file-type name="COMMIT_BY_MESSAGE" />
|
||||
</file-type-list>
|
||||
</component>
|
||||
<component name="GoLibraries">
|
||||
<option name="indexEntireGoPath" value="false" />
|
||||
</component>
|
||||
<component name="ProjectId" id="2B4P2ryw4H3y3GlDs3w9wFxFqHs" />
|
||||
<component name="ProjectViewState">
|
||||
<option name="hideEmptyMiddlePackages" value="true" />
|
||||
<option name="showLibraryContents" value="true" />
|
||||
</component>
|
||||
<component name="PropertiesComponent">
|
||||
<property name="DefaultGoTemplateProperty" value="Go File" />
|
||||
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
|
||||
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
|
||||
<property name="WebServerToolWindowFactoryState" value="false" />
|
||||
<property name="go.formatter.settings.were.checked" value="true" />
|
||||
<property name="go.import.settings.migrated" value="true" />
|
||||
<property name="go.modules.go.list.on.any.changes.was.set" value="true" />
|
||||
<property name="go.sdk.automatically.set" value="true" />
|
||||
<property name="last_opened_file_path" value="$USER_HOME$" />
|
||||
<property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" />
|
||||
<property name="nodejs_npm_path_reset_for_default_project" value="true" />
|
||||
<property name="settings.editor.selected.configurable" value="watcher.settings" />
|
||||
</component>
|
||||
<component name="RunManager">
|
||||
<configuration default="true" type="GoApplicationRunConfiguration" factoryName="Go Application">
|
||||
<module name="exception" />
|
||||
<working_directory value="$PROJECT_DIR$" />
|
||||
<go_parameters value="-i" />
|
||||
<kind value="FILE" />
|
||||
<directory value="$PROJECT_DIR$" />
|
||||
<filePath value="$PROJECT_DIR$" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration default="true" type="GoTestRunConfiguration" factoryName="Go Test">
|
||||
<module name="exception" />
|
||||
<working_directory value="$PROJECT_DIR$" />
|
||||
<go_parameters value="-i" />
|
||||
<kind value="DIRECTORY" />
|
||||
<directory value="$PROJECT_DIR$" />
|
||||
<filePath value="$PROJECT_DIR$" />
|
||||
<framework value="gotest" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
||||
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
|
||||
<component name="TypeScriptGeneratedFilesManager">
|
||||
<option name="version" value="3" />
|
||||
</component>
|
||||
<component name="Vcs.Log.Tabs.Properties">
|
||||
<option name="TAB_STATES">
|
||||
<map>
|
||||
<entry key="MAIN">
|
||||
<value>
|
||||
<State />
|
||||
</value>
|
||||
</entry>
|
||||
</map>
|
||||
</option>
|
||||
</component>
|
||||
<component name="VgoProject">
|
||||
<integration-enabled>true</integration-enabled>
|
||||
</component>
|
||||
</project>
|
26
abstrace.go
Normal file
26
abstrace.go
Normal file
@ -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
|
||||
}
|
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
|
||||
}
|
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
|
||||
}
|
Loading…
Reference in New Issue
Block a user