包装try/catch
This commit is contained in:
41
try/try.go
Normal file
41
try/try.go
Normal file
@ -0,0 +1,41 @@
|
||||
// Package try ...
|
||||
//
|
||||
// Description : try ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2023-07-20 11:36
|
||||
package try
|
||||
|
||||
// Try try入口函数
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:37 2023/7/20
|
||||
func Try(fn ILogicFunction, input *LogicFuncInput) ICatchHandler {
|
||||
catchHandler := &DefaultCatchHandler{}
|
||||
if nil == fn {
|
||||
// 逻辑函数空指针
|
||||
catchHandler.errCode = NilLogicFunc
|
||||
return catchHandler
|
||||
}
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
catchHandler.errCode = LogicFuncPanic
|
||||
catchHandler.data = map[string]interface{}{
|
||||
"message": r.(error).Error(),
|
||||
}
|
||||
}
|
||||
}()
|
||||
if nil == input {
|
||||
input = &LogicFuncInput{
|
||||
Parameter: map[string]interface{}{},
|
||||
}
|
||||
}
|
||||
|
||||
result := fn(input)
|
||||
catchHandler.errCode = result.ErrCode
|
||||
catchHandler.data = result.Data
|
||||
|
||||
return catchHandler
|
||||
}
|
Reference in New Issue
Block a user