Description
Summary
The client-side implementation of duplex streams currently doesn't support the transmission of error messages to the server. This functionality is crucial for both the transport and application layers to handle errors effectively and maintain a symmetrical communication model with the server. The issue outlines the rationale behind enabling client-side error transmission and presents an architectural overview.
Background and Current State
Transport Layer: Stream Error Codes
At the transport layer, we manage what we term as "transport-level stream errors." These errors are captured and transformed using codeToReason
and reasonToCode
functions. Both the server and client use these functions to translate a stream error code into a reason
of any
type. This reason
is then accepted by readable.cancel(reason)
and writable.abort(reason)
.
Application Layer: JSON RPC Errors
On the application layer, we use JSON RPC errors that are encoded as per the JSON RPC specification. A typical JSON RPC error message might look like:
{"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "1"}
Error Handling: toError
and fromError
Both on the client and server sides, toError
and fromError
functions are used to serialize and deserialize errors, respectively. For example, on the server side:
handler() {
throw new SpecialError('oh no');
}
In this case, fromError
would convert SpecialError
into a JSON RPC compatible error message. The client, upon receiving this, would use toError
to convert it back into an exception object.
Issue: Asymmetry in Error Handling
While the server has the capability to transmit errors back to the client, the client is currently unable to reciprocate, even when using duplex streams. This creates an asymmetry in error handling that can cause issues in various scenarios, especially when the client needs to notify the server of specific conditions or errors that should halt or modify server behavior.
Proposed Solution
Duplex Streams: Error Transmission
Duplex streams should be enhanced to allow the client to send JSON RPC error messages back to the server. Stream handlers should be prepared to handle errors thrown into the async iterator/generator on both ends.
Implementation Steps:
- Modify the client's duplex stream setup to allow the transmission of JSON RPC error messages.
- Incorporate
toError
andfromError
into the client's duplex stream setup. - Ensure that these changes do not interfere with existing
codeToReason
andreasonToCode
functionalities for handling transport-level errors.