UNPKG

@nestjs/websockets

Version:

Nest - modern, fast, powerful node.js web framework (@websockets)

73 lines (72 loc) 2.77 kB
import { type ArgumentsHost, Logger, type WsExceptionFilter } from '@nestjs/common'; export interface ErrorPayload<Cause = { pattern: string; data: unknown; }> { /** * Error message identifier. */ status: 'error'; /** * Error message. */ message: string; /** * Message that caused the exception. */ cause?: Cause; } interface BaseWsExceptionFilterOptions { /** * When true, the data that caused the exception will be included in the response. * This is useful when you want to provide additional context to the client, or * when you need to associate the error with a specific request. * @default true */ includeCause?: boolean; /** * A factory function that can be used to control the shape of the "cause" object. * This is useful when you need a custom structure for the cause object. * @default (pattern, data) => ({ pattern, data }) */ causeFactory?: (pattern: string, data: unknown) => Record<string, any>; } /** * @publicApi */ export declare class BaseWsExceptionFilter<TError = any> implements WsExceptionFilter<TError> { protected readonly options: BaseWsExceptionFilterOptions; protected static readonly logger: Logger; constructor(options?: BaseWsExceptionFilterOptions); catch(exception: TError, host: ArgumentsHost): void; handleError<TClient extends { emit?: Function; send?: Function; }>(client: TClient, exception: TError, cause: ErrorPayload['cause']): void; handleUnknownError<TClient extends { emit?: Function; send?: Function; }>(exception: TError, client: TClient, data: ErrorPayload['cause']): void; isExceptionObject(err: any): err is Error; /** * Sends an error message to the client. Supports both Socket.IO clients * (which use `emit`) and native WebSocket clients (which use `send`). * * Native WebSocket clients (e.g. from the `ws` package) inherit from * EventEmitter and therefore also have an `emit` method, but that method * only dispatches events locally. To distinguish native WebSocket clients * from Socket.IO clients, we check for a numeric `readyState` property * (part of the WebSocket specification) before falling back to `emit`. */ protected emitMessage<TClient extends { emit?: Function; send?: Function; }>(client: TClient, event: string, payload: unknown): void; /** * Determines whether the given client is a native WebSocket (e.g. from the * `ws` package) as opposed to a Socket.IO socket. Native WebSocket objects * expose a numeric `readyState` property per the WebSocket specification. */ private isNativeWebSocket; } export {};