Skip to content

Commit

Permalink
add message and data in process response
Browse files Browse the repository at this point in the history
  • Loading branch information
akaladarshi committed Oct 17, 2024
1 parent 4e33c9f commit 9592989
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ func (e *respError) ErrorData() interface{} {
return e.Data
}

var marshalableRT = reflect.TypeOf(new(marshalable)).Elem()
var (
marshalableRT = reflect.TypeOf(new(marshalable)).Elem()
errorsRT = reflect.TypeOf(new(ErrorWithData)).Elem()
)

func (e *respError) val(errors *Errors) reflect.Value {
if errors != nil {
Expand All @@ -96,9 +99,23 @@ func (e *respError) val(errors *Errors) reflect.Value {
} else {
v = reflect.New(t)
}

if len(e.Meta) > 0 && v.Type().Implements(marshalableRT) {
_ = v.Interface().(marshalable).UnmarshalJSON(e.Meta)
} else {
msgField := v.Elem().FieldByName("Message")
if msgField.IsValid() && msgField.CanSet() && msgField.Kind() == reflect.String {
msgField.SetString(e.Message)
}

if v.Type().Implements(errorsRT) {
dataField := v.Elem().FieldByName("Data")
if dataField.IsValid() && dataField.CanSet() {
dataField.Set(reflect.ValueOf(e.Data))
}
}
}

if t.Kind() != reflect.Ptr {
v = v.Elem()
}
Expand Down

0 comments on commit 9592989

Please sign in to comment.