improve error handling for package gconv

This commit is contained in:
jflyfox
2020-12-30 13:27:27 +08:00
parent 86e70ad55c
commit 036bc03ebf
4 changed files with 32 additions and 8 deletions

View File

@ -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{}{

View File

@ -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 (

View File

@ -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)
}
}
}()

View File

@ -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 <params> is JSON, it then uses json.Unmarshal doing the converting.