| 1 | // Package rpcwire implements the Extension Protocol's transport-neutral |
| 2 | // JSON-RPC 2.0 over NDJSON wire. It intentionally contains no extension |
| 3 | // lifecycle or capability semantics. |
| 4 | package rpcwire |
| 5 | |
| 6 | import ( |
| 7 | "encoding/json" |
| 8 | "fmt" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | // Standard JSON-RPC 2.0 error codes. |
| 13 | const ( |
| 14 | ErrParse = -32700 |
| 15 | ErrInvalidRequest = -32600 |
| 16 | ErrMethodNotFound = -32601 |
| 17 | ErrInvalidParams = -32602 |
| 18 | ErrInternal = -32603 |
| 19 | // ErrServerBusy is a stable transport-local overload response. It is outside |
| 20 | // the JSON-RPC reserved range and intentionally carries no peer data. |
| 21 | ErrServerBusy = -32099 |
| 22 | ) |
| 23 | |
| 24 | // ErrorObject is the JSON-RPC error object carried on the wire. |
| 25 | type ErrorObject struct { |
| 26 | Code int `json:"code"` |
| 27 | Message string `json:"message"` |
| 28 | Data json.RawMessage `json:"data,omitempty"` |
| 29 | } |
| 30 | |
| 31 | // RPCError lets a request handler choose the JSON-RPC error response. Data may |
| 32 | // be any JSON-marshalable value. Callers must keep Message and Data safe for the |
| 33 | // target protocol; rpcwire does not apply product-specific redaction. |
| 34 | type RPCError struct { |
| 35 | Code int |
| 36 | Message string |
| 37 | Data any |
| 38 | } |
| 39 | |
| 40 | func (e *RPCError) Error() string { |
| 41 | if e == nil { |
| 42 | return "" |
| 43 | } |
| 44 | return e.Message |
| 45 | } |
| 46 | |
| 47 | // ResponseError is returned by Conn.Request when the peer responds with a |
| 48 | // JSON-RPC error. Data remains raw so the protocol adapter can decode its own |
| 49 | // structured error type. |
| 50 | type ResponseError struct { |
| 51 | Code int |
| 52 | Message string |
| 53 | Data json.RawMessage |
| 54 | } |
| 55 | |
| 56 | func (e *ResponseError) Error() string { |
| 57 | if e == nil { |
| 58 | return "" |
| 59 | } |
| 60 | return e.Message |
| 61 | } |
| 62 | |
| 63 | // FrameTooLargeError reports a frame that violates the configured NDJSON |
| 64 | // budget. Size and Limit include the trailing newline, matching the bytes sent |
| 65 | // over the transport. |
| 66 | type FrameTooLargeError struct { |
| 67 | Direction string |
| 68 | Size int |
| 69 | Limit int |
| 70 | } |
| 71 | |
| 72 | func (e *FrameTooLargeError) Error() string { |
| 73 | return fmt.Sprintf("rpcwire: %s frame is %d bytes; limit is %d", e.Direction, e.Size, e.Limit) |
| 74 | } |
| 75 | |
| 76 | // OutboundQueueFullError reports that TryNotify could not enqueue a |
| 77 | // best-effort notification without blocking. The caller may drop the event; |
| 78 | // request/response traffic keeps using the bounded blocking write path. |
| 79 | type OutboundQueueFullError struct { |
| 80 | Limit int |
| 81 | } |
| 82 | |
| 83 | func (e *OutboundQueueFullError) Error() string { |
| 84 | return fmt.Sprintf("rpcwire: outbound notification queue is full (limit %d)", e.Limit) |
| 85 | } |
| 86 | |
| 87 | // WriteStallError reports an outbound write that made no progress within the |
| 88 | // configured MaxWriteStall bound: the peer is alive enough to keep the pipe |
| 89 | // open but has stopped reading, so without a bound the write would block the |
| 90 | // caller forever. It is a transport-fatal error — the connection fails so a |
| 91 | // wedged peer is torn down instead of stalling every later call. |
| 92 | type WriteStallError struct { |
| 93 | Direction string |
| 94 | Size int |
| 95 | Stall time.Duration |
| 96 | } |
| 97 | |
| 98 | func (e *WriteStallError) Error() string { |
| 99 | return fmt.Sprintf("rpcwire: %s write of %d bytes made no progress for %s", e.Direction, e.Size, e.Stall) |
| 100 | } |
| 101 |