Skip to content

Commit

Permalink
fix(rpc): make sure Error is chained and ValidationError correctly ha…
Browse files Browse the repository at this point in the history
…ndled
  • Loading branch information
marcj committed May 27, 2024
1 parent 3b2c6cc commit 5c49778
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/framework/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ export class RpcServer implements RpcServerInterface {

server.on('connection', (ws, req: HttpRequest) => {
const connection = createRpcConnection({
write(b) {
ws.send(b);
writeBinary(message) {
ws.send(message);
},
close() {
ws.close();
Expand Down
11 changes: 4 additions & 7 deletions packages/rpc/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

import { asyncOperation, ClassType, formatError, sleep } from '@deepkit/core';
import { ReceiveType, resolveReceiveType } from '@deepkit/type';
import { ReceiveType, resolveReceiveType, ValidationError } from '@deepkit/type';
import { BehaviorSubject, Observable, Subject } from 'rxjs';
import {
ControllerDefinition,
Expand Down Expand Up @@ -42,9 +42,6 @@ import {
} from '../transport.js';

export class OfflineError extends Error {
constructor(message: string = 'Offline') {
super(message);
}
}

type PromisifyFn<T extends ((...args: any[]) => any)> = (...args: Parameters<T>) => ReturnType<T> extends Promise<any> ? ReturnType<T> : Promise<ReturnType<T>>;
Expand Down Expand Up @@ -265,7 +262,7 @@ export class RpcClientTransporter {

onError: (error: Error) => {
this.onError(error);
reject(new OfflineError(`Could not connect: ${formatError(error)}`));
reject(new OfflineError(`Could not connect: ${formatError(error)}`, {cause: error}));
},

read: (message: RpcMessage) => {
Expand Down Expand Up @@ -312,7 +309,8 @@ export class RpcClientTransporter {
try {
this.writer(message, this.writerOptions, progress);
} catch (error: any) {
throw new OfflineError(error);
if (error instanceof ValidationError) throw error;
throw new OfflineError(error, {cause: error});
}
}
}
Expand Down Expand Up @@ -446,7 +444,6 @@ export class RpcBaseClient implements WritableClient {
} else {
const callback = this.replies.get(message.id);
if (!callback) {
console.log(message.debug());
throw new Error('No callback for ' + message.id);
}
if (callback) callback(message);
Expand Down
2 changes: 1 addition & 1 deletion packages/rpc/src/server/kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export class RpcMessageBuilder {
if (this.logValidationErrors) {
this.logger.warn(this.errorLabel, error);
}
throw new Error(this.errorLabel + ': ' + error.message);
throw new Error(this.errorLabel + ': ' + error.message, {cause: error});
}
}

Expand Down
5 changes: 5 additions & 0 deletions packages/rpc/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
"outDir": "./dist/cjs",
"declaration": true,
"composite": true,
"lib": [
"es2021",
"es2022.error",
"dom"
],
"types": [
"dot-prop",
"fs-extra",
Expand Down

0 comments on commit 5c49778

Please sign in to comment.