fix(server): never throw in websocket gateways (#5050)

This commit is contained in:
liuyi
2023-11-24 07:26:39 +00:00
parent 42f4045ad6
commit cf65a5cd93
@@ -28,8 +28,46 @@ import {
WorkspaceNotFoundError, WorkspaceNotFoundError,
} from './error'; } from './error';
export const GatewayErrorWrapper = (): MethodDecorator => {
// @ts-expect-error allow
return (
_target,
_key,
desc: TypedPropertyDescriptor<(...args: any[]) => any>
) => {
const originalMethod = desc.value;
if (!originalMethod) {
return desc;
}
desc.value = function (...args: any[]) {
let result: any;
try {
result = originalMethod.apply(this, args);
} catch (e) {
return {
error: new InternalError(e as Error),
};
}
if (result instanceof Promise) {
return result.catch(e => {
return {
error: new InternalError(e),
};
});
} else {
return result;
}
};
return desc;
};
};
const SubscribeMessage = (event: string) => const SubscribeMessage = (event: string) =>
applyDecorators( applyDecorators(
GatewayErrorWrapper(),
CallCounter('socket_io_counter', { event }), CallCounter('socket_io_counter', { event }),
CallTimer('socket_io_timer', { event }), CallTimer('socket_io_timer', { event }),
RawSubscribeMessage(event) RawSubscribeMessage(event)
@@ -233,25 +271,19 @@ export class EventsGateway implements OnGatewayConnection, OnGatewayDisconnect {
}; };
} }
try { const docId = new DocID(guid, workspaceId);
const docId = new DocID(guid, workspaceId); client
client .to(docId.workspace)
.to(docId.workspace) .emit('server-updates', { workspaceId, guid, updates });
.emit('server-updates', { workspaceId, guid, updates });
const buffers = updates.map(update => Buffer.from(update, 'base64')); const buffers = updates.map(update => Buffer.from(update, 'base64'));
await this.docManager.batchPush(docId.workspace, docId.guid, buffers); await this.docManager.batchPush(docId.workspace, docId.guid, buffers);
return { return {
data: { data: {
accepted: true, accepted: true,
}, },
}; };
} catch (e) {
return {
error: new InternalError(e as Error),
};
}
} }
@Auth() @Auth()