Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
akaladarshi committed Oct 23, 2024
1 parent e8dc77a commit 90c8605
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ func (e *JSONRPCError) Error() string {
}

var (
_ error = (*JSONRPCError)(nil)
marshalableRT = reflect.TypeOf(new(marshalable)).Elem()
unmarshalableRT = reflect.TypeOf(new(RPCErrorCodec)).Elem()
_ error = (*JSONRPCError)(nil)
marshalableRT = reflect.TypeOf(new(marshalable)).Elem()
errorCodecRT = reflect.TypeOf(new(RPCErrorCodec)).Elem()
)

func (e *JSONRPCError) val(errors *Errors) reflect.Value {
Expand All @@ -66,12 +66,18 @@ func (e *JSONRPCError) val(errors *Errors) reflect.Value {
v = reflect.New(t)
}

if v.Type().Implements(unmarshalableRT) {
_ = v.Interface().(RPCErrorCodec).FromJSONRPCError(*e)
if v.Type().Implements(errorCodecRT) {
if err := v.Interface().(RPCErrorCodec).FromJSONRPCError(*e); err != nil {
log.Errorf("Error converting JSONRPCError (code %d) to custom error type '%s': %w", e.Code, t.String(), err)
return reflect.ValueOf(e)
}
}

if len(e.Meta) > 0 && v.Type().Implements(marshalableRT) {
_ = v.Interface().(marshalable).UnmarshalJSON(e.Meta)
if err := v.Interface().(marshalable).UnmarshalJSON(e.Meta); err != nil {
log.Errorf("Error unmarshaling metadata for error type '%s': %w", t.String(), err)
return reflect.ValueOf(e)
}
}

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

0 comments on commit 90c8605

Please sign in to comment.