包装try/catch
This commit is contained in:
		
							
								
								
									
										35
									
								
								try/abstract.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								try/abstract.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,35 @@
 | 
			
		||||
// Package try ...
 | 
			
		||||
//
 | 
			
		||||
// Description : try ...
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 2023-07-20 11:26
 | 
			
		||||
package try
 | 
			
		||||
 | 
			
		||||
// ICatchHandler 异常处理器接口约束
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 11:26 2023/7/20
 | 
			
		||||
type ICatchHandler interface {
 | 
			
		||||
	Catch(errCode string, handler func(errCode string, data map[string]interface{})) ICatchHandler
 | 
			
		||||
	CatchAll(handler func(errCode string, data map[string]interface{})) IFinalHandler
 | 
			
		||||
	IFinalHandler
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// IFinalHandler 所有异常 / 逻辑执行成功之后的逻辑
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 11:27 2023/7/20
 | 
			
		||||
type IFinalHandler interface {
 | 
			
		||||
	Finally(data map[string]interface{}, handlers ...func(data map[string]interface{}))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ILogicFunction 逻辑函数约束
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 11:29 2023/7/20
 | 
			
		||||
type ILogicFunction func(input *LogicFuncInput) LogicFuncOutput
 | 
			
		||||
							
								
								
									
										95
									
								
								try/default.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										95
									
								
								try/default.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,95 @@
 | 
			
		||||
// Package try ...
 | 
			
		||||
//
 | 
			
		||||
// Description : try ...
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 2023-07-20 12:13
 | 
			
		||||
package try
 | 
			
		||||
 | 
			
		||||
// DefaultCatchHandler catch handler默认实现
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 12:14 2023/7/20
 | 
			
		||||
type DefaultCatchHandler struct {
 | 
			
		||||
	hasDeal bool // 异常是否已被处理
 | 
			
		||||
	errCode string
 | 
			
		||||
	data    map[string]interface{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// hasDealError 判断异常是否已经被处理
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 12:46 2023/7/20
 | 
			
		||||
func (d *DefaultCatchHandler) hasDealError() bool {
 | 
			
		||||
	if d.hasDeal {
 | 
			
		||||
		return true
 | 
			
		||||
	}
 | 
			
		||||
	if d.errCode == "" {
 | 
			
		||||
		return true
 | 
			
		||||
	}
 | 
			
		||||
	return false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Catch 处理指定errorCode
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 17:19 2023/7/20
 | 
			
		||||
func (d *DefaultCatchHandler) Catch(errCode string, handler func(errCode string, data map[string]interface{})) ICatchHandler {
 | 
			
		||||
	if d.hasDealError() {
 | 
			
		||||
		return d
 | 
			
		||||
	}
 | 
			
		||||
	if errCode != d.errCode {
 | 
			
		||||
		return d
 | 
			
		||||
	}
 | 
			
		||||
	d.hasDeal = true
 | 
			
		||||
	defer func() {
 | 
			
		||||
		if r := recover(); nil != r {
 | 
			
		||||
			d.hasDeal = false
 | 
			
		||||
			d.data[errCode+"_handler_error"] = r.(error).Error()
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
	handler(d.errCode, d.data)
 | 
			
		||||
	return d
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CatchAll 捕捉任意一场
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 17:47 2023/7/20
 | 
			
		||||
func (d *DefaultCatchHandler) CatchAll(handler func(errCode string, data map[string]interface{})) IFinalHandler {
 | 
			
		||||
	if d.hasDealError() {
 | 
			
		||||
		return d
 | 
			
		||||
	}
 | 
			
		||||
	defer func() {
 | 
			
		||||
		if r := recover(); nil != r {
 | 
			
		||||
			d.data[d.errCode+"_handler_error"] = r.(error).Error()
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
	d.hasDeal = true
 | 
			
		||||
	handler(d.errCode, d.data)
 | 
			
		||||
	return d
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Finally 最终逻辑
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 17:48 2023/7/20
 | 
			
		||||
func (d *DefaultCatchHandler) Finally(data map[string]interface{}, handlers ...func(data map[string]interface{})) {
 | 
			
		||||
	if data == nil {
 | 
			
		||||
		data = map[string]interface{}{}
 | 
			
		||||
	}
 | 
			
		||||
	defer func() {
 | 
			
		||||
		if r := recover(); nil != r {
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
	for _, itemHandler := range handlers {
 | 
			
		||||
		itemHandler(data)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										50
									
								
								try/define.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								try/define.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,50 @@
 | 
			
		||||
// Package try ...
 | 
			
		||||
//
 | 
			
		||||
// Description : try ...
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 2023-07-20 11:29
 | 
			
		||||
package try
 | 
			
		||||
 | 
			
		||||
// LogicFuncInput 逻辑函数输入参数
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 11:30 2023/7/20
 | 
			
		||||
type LogicFuncInput struct {
 | 
			
		||||
	Parameter map[string]interface{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// LogicFuncOutput ...
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 11:30 2023/7/20
 | 
			
		||||
type LogicFuncOutput struct {
 | 
			
		||||
	ErrCode string                 // 错误标识码
 | 
			
		||||
	Data    map[string]interface{} // 错误时返回的数据
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NewLogicFuncOutput 获取逻辑函数输出数据
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 11:33 2023/7/20
 | 
			
		||||
func NewLogicFuncOutput(code string, data map[string]interface{}) LogicFuncOutput {
 | 
			
		||||
	if data == nil {
 | 
			
		||||
		data = map[string]interface{}{}
 | 
			
		||||
	}
 | 
			
		||||
	r := LogicFuncOutput{
 | 
			
		||||
		ErrCode: code,
 | 
			
		||||
		Data:    data,
 | 
			
		||||
	}
 | 
			
		||||
	return r
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	// NilLogicFunc 。。。
 | 
			
		||||
	NilLogicFunc = "NIL_LOGIC_FUNC"
 | 
			
		||||
	// LogicFuncPanic ...
 | 
			
		||||
	LogicFuncPanic = "LOGIC_FUNC_PANIC"
 | 
			
		||||
)
 | 
			
		||||
							
								
								
									
										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