From 036bc03ebf5862d5193094eee0d8c400cc31309b Mon Sep 17 00:00:00 2001 From: jflyfox Date: Wed, 30 Dec 2020 13:27:27 +0800 Subject: [PATCH] improve error handling for package gconv --- util/gconv/gconv.go | 8 ++++++++ util/gconv/gconv_map.go | 16 ++++++++++++---- util/gconv/gconv_struct.go | 8 ++++++-- util/gconv/gconv_structs.go | 8 ++++++-- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/util/gconv/gconv.go b/util/gconv/gconv.go index fc0b5fb82..01b3a64ac 100644 --- a/util/gconv/gconv.go +++ b/util/gconv/gconv.go @@ -21,6 +21,14 @@ import ( "github.com/gogf/gf/encoding/gbinary" ) +type ( + // errorStack is the interface for Stack feature. + errorStack interface { + Error() string + Stack() string + } +) + var ( // Empty strings. emptyStringMap = map[string]struct{}{ diff --git a/util/gconv/gconv_map.go b/util/gconv/gconv_map.go index 8688507b9..c94a5bf49 100644 --- a/util/gconv/gconv_map.go +++ b/util/gconv/gconv_map.go @@ -444,8 +444,12 @@ func doMapToMap(params interface{}, pointer interface{}, mapping ...map[string]s } defer func() { // Catch the panic, especially the reflect operation panics. - if e := recover(); e != nil { - err = gerror.NewSkipf(1, "%v", e) + if exception := recover(); exception != nil { + if e, ok := exception.(errorStack); ok { + err = e + } else { + err = gerror.NewSkipf(1, "%v", exception) + } } }() var ( @@ -544,8 +548,12 @@ func doMapToMaps(params interface{}, pointer interface{}, mapping ...map[string] } defer func() { // Catch the panic, especially the reflect operation panics. - if e := recover(); e != nil { - err = gerror.NewSkipf(1, "%v", e) + if exception := recover(); exception != nil { + if e, ok := exception.(errorStack); ok { + err = e + } else { + err = gerror.NewSkipf(1, "%v", exception) + } } }() var ( diff --git a/util/gconv/gconv_struct.go b/util/gconv/gconv_struct.go index a84226e33..401f136a6 100644 --- a/util/gconv/gconv_struct.go +++ b/util/gconv/gconv_struct.go @@ -53,8 +53,12 @@ func doStruct(params interface{}, pointer interface{}, mapping ...map[string]str defer func() { // Catch the panic, especially the reflect operation panics. - if e := recover(); e != nil { - err = gerror.NewSkipf(1, "%v", e) + if exception := recover(); exception != nil { + if e, ok := exception.(errorStack); ok { + err = e + } else { + err = gerror.NewSkipf(1, "%v", exception) + } } }() diff --git a/util/gconv/gconv_structs.go b/util/gconv/gconv_structs.go index 0372bb86f..a4ce17065 100644 --- a/util/gconv/gconv_structs.go +++ b/util/gconv/gconv_structs.go @@ -45,8 +45,12 @@ func doStructs(params interface{}, pointer interface{}, mapping ...map[string]st defer func() { // Catch the panic, especially the reflect operation panics. - if e := recover(); e != nil { - err = gerror.NewSkipf(1, "%v", e) + if exception := recover(); exception != nil { + if e, ok := exception.(errorStack); ok { + err = e + } else { + err = gerror.NewSkipf(1, "%v", exception) + } } }() // If given is JSON, it then uses json.Unmarshal doing the converting.