Skip to content

Commit 80a0a90

Browse files
committed
feat: add better error messages / linked error with cause for new rpc v2 error cases
1 parent 0eb011e commit 80a0a90

3 files changed

Lines changed: 12 additions & 4 deletions

File tree

src/room/rpc/client/RpcClientManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ export default class RpcClientManager extends (EventEmitter as new () => TypedEm
197197
this.log.warn(`Error reading RPC response payload: ${e}`);
198198
this.handleIncomingRpcResponseFailure(
199199
associatedRequestId,
200-
RpcError.builtIn('APPLICATION_ERROR'),
200+
RpcError.builtIn('APPLICATION_ERROR', 'Error reading RPC response payload', { cause: e }),
201201
);
202202
return;
203203
}

src/room/rpc/server/RpcServerManager.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export default class RpcServerManager extends (EventEmitter as new () => TypedEm
105105
responseError = RpcError.builtIn(
106106
'APPLICATION_ERROR',
107107
`Uncaught error: ${(error as Error)?.message ?? error}`,
108+
{ cause: error },
108109
);
109110
}
110111

@@ -141,7 +142,7 @@ export default class RpcServerManager extends (EventEmitter as new () => TypedEm
141142
callerIdentity,
142143
requestId,
143144
null,
144-
RpcError.builtIn('APPLICATION_ERROR'),
145+
RpcError.builtIn('APPLICATION_ERROR', 'RPC data stream malformed'),
145146
);
146147
return;
147148
}
@@ -167,7 +168,7 @@ export default class RpcServerManager extends (EventEmitter as new () => TypedEm
167168
callerIdentity,
168169
requestId,
169170
null,
170-
RpcError.builtIn('APPLICATION_ERROR'),
171+
RpcError.builtIn('APPLICATION_ERROR', 'Error reading RPC request payload', { cause: e }),
171172
);
172173
return;
173174
}

src/room/rpc/utils.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,25 @@ export class RpcError extends Error {
6060

6161
data?: string;
6262

63+
// More info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
64+
cause?: unknown;
65+
6366
/**
6467
* Creates an error object with the given code and message, plus an optional data payload.
6568
*
6669
* If thrown in an RPC method handler, the error will be sent back to the caller.
6770
*
6871
* Error codes 1001-1999 are reserved for built-in errors (see RpcError.ErrorCode for their meanings).
6972
*/
70-
constructor(code: number, message: string, data?: string) {
73+
constructor(code: number, message: string, data?: string, options?: { cause?: unknown }) {
7174
super(message);
7275
this.code = code;
7376
this.message = truncateBytes(message, RpcError.MAX_MESSAGE_BYTES);
7477
this.data = data ? truncateBytes(data, RpcError.MAX_DATA_BYTES) : undefined;
78+
79+
if (typeof options?.cause !== 'undefined') {
80+
this.cause = options?.cause;
81+
}
7582
}
7683

7784
/**

0 commit comments

Comments
 (0)