Wrap errors

This commit is contained in:
Ian Gulliver
2023-04-24 04:10:01 +00:00
parent 29e8a1fe5e
commit 58a2df2604

View File

@@ -3,6 +3,8 @@ package path
import ( import (
"encoding/json" "encoding/json"
"reflect" "reflect"
"github.com/gopatchy/jsrest"
) )
func Merge(to, from any) { func Merge(to, from any) {
@@ -33,8 +35,7 @@ func MergeValue(to, from reflect.Value) {
func MergeMap(to any, from map[string]any) error { func MergeMap(to any, from map[string]any) error {
m, err := ToMap(to) m, err := ToMap(to)
if err != nil { if err != nil {
// TODO: Wrap error return jsrest.Errorf(jsrest.ErrInternalServerError, "converting to map failed (%w)", err)
return err
} }
MergeMaps(m, from) MergeMaps(m, from)
@@ -61,16 +62,14 @@ func MergeMaps(to map[string]any, from map[string]any) {
func ToMap(from any) (map[string]any, error) { func ToMap(from any) (map[string]any, error) {
js, err := json.Marshal(from) js, err := json.Marshal(from)
if err != nil { if err != nil {
// TODO: Wrap error return nil, jsrest.Errorf(jsrest.ErrInternalServerError, "json marshal failed (%w)", err)
return nil, err
} }
ret := map[string]any{} ret := map[string]any{}
err = json.Unmarshal(js, &ret) err = json.Unmarshal(js, &ret)
if err != nil { if err != nil {
// TODO: Wrap error return nil, jsrest.Errorf(jsrest.ErrInternalServerError, "json unmarshal failed (%w)", err)
return nil, err
} }
return ret, nil return ret, nil
@@ -79,10 +78,13 @@ func ToMap(from any) (map[string]any, error) {
func FromMap(to any, from map[string]any) error { func FromMap(to any, from map[string]any) error {
js, err := json.Marshal(from) js, err := json.Marshal(from)
if err != nil { if err != nil {
// TODO: Wrap error return jsrest.Errorf(jsrest.ErrInternalServerError, "json marshal failed (%w)", err)
return err
} }
// TODO: Wrap error err = json.Unmarshal(js, to)
return json.Unmarshal(js, to) if err != nil {
return jsrest.Errorf(jsrest.ErrInternalServerError, "json unmarshal failed (%w)", err)
}
return nil
} }