Skip to content

Commit dfc83a0

Browse files
committed
feat: Add 'Data' field to 'response' struct
1 parent 8493164 commit dfc83a0

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

handler.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ type respError struct {
6969
Code ErrorCode `json:"code"`
7070
Message string `json:"message"`
7171
Meta json.RawMessage `json:"meta,omitempty"`
72-
Data json.RawMessage `json:"data,omitempty"`
72+
Data interface{} `json:"data,omitempty"`
7373
}
7474

7575
func (e *respError) Error() string {
@@ -105,10 +105,11 @@ func (e *respError) val(errors *Errors) reflect.Value {
105105
}
106106

107107
type response struct {
108-
Jsonrpc string
109-
Result interface{}
110-
ID interface{}
111-
Error *respError
108+
Jsonrpc string `json:"jsonrpc"`
109+
Result interface{} `json:"result,omitempty"`
110+
ID interface{} `json:"id"`
111+
Error *respError `json:"error,omitempty"`
112+
Data interface{} `json:"data,omitempty"` // Add this line
112113
}
113114

114115
func (r response) MarshalJSON() ([]byte, error) {
@@ -350,16 +351,16 @@ func (s *handler) createError(err error) *respError {
350351
}
351352

352353
if m, ok := err.(marshalable); ok {
353-
meta, err := m.MarshalJSON()
354-
if err == nil {
354+
meta, marshalErr := m.MarshalJSON()
355+
if marshalErr == nil {
355356
out.Meta = meta
357+
} else {
358+
log.Warnf("Failed to marshal error metadata: %v", marshalErr)
356359
}
357360
}
358361

359362
if dataErr, ok := err.(interface{ ErrorData() interface{} }); ok {
360-
if data, err := json.Marshal(dataErr.ErrorData()); err == nil {
361-
out.Data = data
362-
}
363+
out.Data = dataErr.ErrorData() // Directly assign the data
363364
}
364365

365366
return out
@@ -517,6 +518,10 @@ func (s *handler) handle(ctx context.Context, req request, w func(func(io.Writer
517518
}
518519
} else {
519520
resp.Result = res
521+
// Add this block to include additional data in the response
522+
if dataProvider, ok := res.(interface{ ResponseData() interface{} }); ok {
523+
resp.Data = dataProvider.ResponseData()
524+
}
520525
}
521526
}
522527
if resp.Error != nil && nonZero {

0 commit comments

Comments
 (0)