-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from DavidCai1993/issue/handle-panic
feat: handle panic in Mask()
- Loading branch information
Showing
1 changed file
with
22 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,35 @@ | ||
package jsonmask | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
// Mask selects the specific parts of an object, according to the "mask". | ||
func Mask(obj interface{}, mask string) (interface{}, error) { | ||
func Mask(obj interface{}, mask string) (res interface{}, err error) { | ||
defer func() { | ||
if r := recover(); err != nil { | ||
res = obj | ||
|
||
switch r.(type) { | ||
case error: | ||
err = r.(error) | ||
case string: | ||
err = errors.New(r.(string)) | ||
default: | ||
err = errors.New("json mask panic") | ||
} | ||
} | ||
}() | ||
|
||
compiledMask, err := compile(mask) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
filteredObj, err := filter(obj, compiledMask) | ||
res, err = filter(obj, compiledMask) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return filteredObj, nil | ||
return res, nil | ||
} |