Skip to content

Commit 9592989

Browse files
author
Aryan Tikarya
committed
add message and data in process response
1 parent 4e33c9f commit 9592989

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

handler.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ func (e *respError) ErrorData() interface{} {
8484
return e.Data
8585
}
8686

87-
var marshalableRT = reflect.TypeOf(new(marshalable)).Elem()
87+
var (
88+
marshalableRT = reflect.TypeOf(new(marshalable)).Elem()
89+
errorsRT = reflect.TypeOf(new(ErrorWithData)).Elem()
90+
)
8891

8992
func (e *respError) val(errors *Errors) reflect.Value {
9093
if errors != nil {
@@ -96,9 +99,23 @@ func (e *respError) val(errors *Errors) reflect.Value {
9699
} else {
97100
v = reflect.New(t)
98101
}
102+
99103
if len(e.Meta) > 0 && v.Type().Implements(marshalableRT) {
100104
_ = v.Interface().(marshalable).UnmarshalJSON(e.Meta)
105+
} else {
106+
msgField := v.Elem().FieldByName("Message")
107+
if msgField.IsValid() && msgField.CanSet() && msgField.Kind() == reflect.String {
108+
msgField.SetString(e.Message)
109+
}
110+
111+
if v.Type().Implements(errorsRT) {
112+
dataField := v.Elem().FieldByName("Data")
113+
if dataField.IsValid() && dataField.CanSet() {
114+
dataField.Set(reflect.ValueOf(e.Data))
115+
}
116+
}
101117
}
118+
102119
if t.Kind() != reflect.Ptr {
103120
v = v.Elem()
104121
}

0 commit comments

Comments
 (0)