From 34349a0b063f0c578461ad2a05fc1b09651891c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 24 Jul 2021 23:54:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0try-cache?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- try/catch.go | 90 ++++++++++++++++++++++++++++++++ try/iTry.go | 130 +++++++++++++++++++++++++++++++++++++++++++++++ try/iTry_test.go | 31 +++++++++++ 3 files changed, 251 insertions(+) create mode 100644 try/catch.go create mode 100644 try/iTry.go create mode 100644 try/iTry_test.go diff --git a/try/catch.go b/try/catch.go new file mode 100644 index 0000000..d272f55 --- /dev/null +++ b/try/catch.go @@ -0,0 +1,90 @@ +// Package try... +// +// Description : catch 的实现 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2021-07-24 10:10 下午 +package try + +import "reflect" + +// CatchHandler catch的处理 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 9:55 下午 2021/7/24 +type CatchHandler struct { + err error // 异常 + hasDeal bool // 是否已处理 +} + +// NeedDeal 是否需要处理 : 存在异常切异常没有处理过 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 9:57 下午 2021/7/24 +func (ch *CatchHandler) NeedDeal() bool { + return nil != ch.err && !ch.hasDeal +} + +// HasError 判断是否有异常 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:00 下午 2021/7/24 +func (ch *CatchHandler) HasError() bool { + return ch.err != nil +} + +// ErrorHasDeal 异常已经处理 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:01 下午 2021/7/24 +func (ch *CatchHandler) ErrorHasDeal() bool { + return ch.hasDeal +} + +// Catch ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:29 下午 2021/7/24 +func (ch *CatchHandler) Catch(err error, handler func(err2 error)) ICatchHandler { + if !ch.NeedDeal() { + return ch + } + //如果传入的error类型和发生异常的类型一致,则执行异常处理器,并将hasCatch修改为true代表已捕捉异常 + if reflect.TypeOf(err) == reflect.TypeOf(ch.err) { + handler(ch.err) + ch.hasDeal = true + } + return ch +} + +// CatchAll ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:29 下午 2021/7/24 +func (ch *CatchHandler) CatchAll(handler func(err error)) FinalHandler { + if !ch.NeedDeal() { + return ch + } + handler(ch.err) + ch.hasDeal = true + return ch +} + +func (ch *CatchHandler) Finally(handlers ...func()) { + // 遍历处理器,并在Finally函数执行完毕之后执行 + for _, handler := range handlers { + defer handler() + } + err := ch.err + //<7>如果异常不为空,且未捕捉异常,则抛出异常 + if err != nil && !ch.hasDeal { + panic(err) + } +} diff --git a/try/iTry.go b/try/iTry.go new file mode 100644 index 0000000..f6fc7c3 --- /dev/null +++ b/try/iTry.go @@ -0,0 +1,130 @@ +// Package try... +// +// Description : 各种接口定义 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2021-07-24 9:47 下午 +package try + +import ( + "encoding/json" + "fmt" + "github.com/gin-gonic/gin" + "github.com/go-developer/gopkg/easymap" + "github.com/pkg/errors" + "runtime" +) + +// ICatchHandler catch处理 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:12 下午 2021/7/24 +type ICatchHandler interface { + Catch(e error, handler func(err error)) ICatchHandler + CatchAll(handler func(err error)) FinalHandler + FinalHandler +} + +// SystemError 系统异常 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 11:35 下午 2021/7/24 +type SystemError struct { + error +} + +// FinalHandler final处理 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:12 下午 2021/7/24 +type FinalHandler interface { + Finally(handlers ...func()) +} + +// WithNormal 入口try函数, 普通的逻辑 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:43 下午 2021/7/24 +func WithNormal(f func()) ICatchHandler { + t := &CatchHandler{} + defer func() { + defer func() { + r := recover() + if r != nil { + switch r.(type) { + case runtime.Error: + t.err = r.(error) + case error: + t.err = r.(error) + case string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64: + t.err = SystemError{SystemError{error: errors.New(fmt.Sprintf("%v", r))}} + default: + byteData, _ := json.Marshal(r) + t.err = SystemError{error: errors.New(string(byteData))} + } + } + }() + f() + }() + return t +} + +// WithGin 带 gin context 的 try, 可集成gin框架 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:50 下午 2021/7/24 +func WithGin(ctx *gin.Context, f func(ctx *gin.Context)) ICatchHandler { + t := &CatchHandler{} + defer func() { + defer func() { + r := recover() + t.err = r.(error) + }() + f(ctx) + }() + return t +} + +// WithData 带数据的try +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:52 下午 2021/7/24 +func WithData(data easymap.EasyMap, f func(data easymap.EasyMap)) ICatchHandler { + t := &CatchHandler{} + defer func() { + defer func() { + r := recover() + if r != nil { + t.err = r.(error) + } + }() + f(data) + }() + return t +} + +// WithInterface 传入任意数据类型 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:53 下午 2021/7/24 +func WithInterface(data interface{}, f func(data interface{})) ICatchHandler { + t := &CatchHandler{} + defer func() { + defer func() { + r := recover() + if r != nil { + t.err = r.(error) + } + }() + f(data) + }() + return t +} \ No newline at end of file diff --git a/try/iTry_test.go b/try/iTry_test.go new file mode 100644 index 0000000..1c15e0f --- /dev/null +++ b/try/iTry_test.go @@ -0,0 +1,31 @@ +// Package try... +// +// Description : try... +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-07-24 11:15 下午 +package try + +import ( + "fmt" + "net/url" + "testing" +) + +// TestWithNormal ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 11:16 下午 2021/7/24 +func TestWithNormal(t *testing.T) { + f := func() { + var m *url.URL + fmt.Println(m.RawQuery) + } + WithNormal(f).CatchAll(func(err error) { + fmt.Println("捕获异常 :" + err.Error()) + }).Finally(func() { + fmt.Println("这里是final") + }) +}