From 1dd7ccf02678dd513a5f68040f6ed934bfe85f01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 29 Nov 2023 11:59:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8C=85=E8=A3=85try/catch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- try/abstract.go | 35 ++++++++++++++++++ try/default.go | 95 +++++++++++++++++++++++++++++++++++++++++++++++++ try/define.go | 50 ++++++++++++++++++++++++++ try/try.go | 41 +++++++++++++++++++++ 4 files changed, 221 insertions(+) create mode 100644 try/abstract.go create mode 100644 try/default.go create mode 100644 try/define.go create mode 100644 try/try.go diff --git a/try/abstract.go b/try/abstract.go new file mode 100644 index 0000000..8a11f1e --- /dev/null +++ b/try/abstract.go @@ -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 diff --git a/try/default.go b/try/default.go new file mode 100644 index 0000000..151a571 --- /dev/null +++ b/try/default.go @@ -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) + } +} diff --git a/try/define.go b/try/define.go new file mode 100644 index 0000000..f3b5d85 --- /dev/null +++ b/try/define.go @@ -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" +) diff --git a/try/try.go b/try/try.go new file mode 100644 index 0000000..656cc4f --- /dev/null +++ b/try/try.go @@ -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 +}