Newer
Older
// A value of this type can a JSON-RPC request, notification, successful response or
// error response. Which one it is depends on the fields.
Method string `json:"method,omitempty"`
Params json.RawMessage `json:"params,omitempty"`
Result json.RawMessage `json:"result,omitempty"`
func MakeCall(id int, method string, params []any) *Message {
return &Message{
return msg.hasValidID() && len(msg.Method) == 0 && msg.Params == nil && (msg.Result != nil || msg.Error != nil)
b, _ := json.Marshal(msg)
func (msg *Message) ErrorResponse(err error) *Message {
resp := ErrorMessage(err)
enc, err := jzon.Marshal(result)
Code int `json:"code"`
Message string `json:"message"`
Data any `json:"data,omitempty"`
}
// error message produces json rpc message with error message
Message: err.Error(),
}}
ec, ok := err.(Error)
if ok {
msg.Error.Code = ec.ErrorCode()
}
de, ok := err.(DataError)
if ok {
msg.Error.Data = de.ErrorData()
}
return msg
}
// isBatch returns true when the first non-whitespace characters is '['
for _, c := range raw {
// skip insignificant whitespace (http://www.ietf.org/rfc/rfc4627.txt)
switch c {
case 0x20, 0x09, 0x0a, 0x0d:
continue
}
// parseMessage parses raw bytes as a (batch of) JSON-RPC message(s). There are no error
// checks in this function because the raw message has already been syntax-checked when it
// is called. Any non-JSON-RPC messages in the input return the zero value of
// Message.
func ParseMessage(raw json.RawMessage) ([]*Message, bool) {
if !IsBatchMessage(raw) {
msgs := []*Message{{}}
jzon.Unmarshal(raw, &msgs[0])
// TODO:
// for some reason other json decoders are incompatible with our test suite
// pretty sure its how we handle EOFs and stuff