Closed brianmcgee closed 1 year ago
It seems like an invalid JSON input is the only case where this is needed, and in that case there will be no ID to wrangle anyway. The ParseRequests function already returns a *jrpc2.Error
in case of failure (and that already marshals to a JSON-RPC error object), so could the proxy construct the reply message directly?
msg, jerr := json.Marshal(err)
if jerr != nil {
panic(jerr) // probably do something better with this
}
resp := fmt.Sprintf(`{"jsonrpc":"2.0","id":null,"error":%s}`, string(msg))
or words to that effect.
Example: https://go.dev/play/p/9gzRLq6YPbM
You make a fair point, it is quite a niche edge case. Rather than building a whole Response just to help with JSON marshalling your suggestion is much simpler.
I'm building a proxy and have hit an edge case where I need to construct and return a
jrpc2.Response
with an error:Since all the fields of
jrpc2.Response
are package private I've added a small utility function.