@energica-city/shared-amplify-utils
Version:
Shared utilities for AWS Amplify projects
94 lines • 3.38 kB
TypeScript
import { type ErrorContext } from './error';
/**
* WebSocket-specific error codes
*/
export declare const WebSocketErrorCodes: {
readonly VALIDATION_ERROR: "VALIDATION_ERROR";
readonly AUTHENTICATION_ERROR: "AUTHENTICATION_ERROR";
readonly AUTHORIZATION_ERROR: "AUTHORIZATION_ERROR";
readonly CONNECTION_ERROR: "CONNECTION_ERROR";
readonly MESSAGE_TOO_LARGE: "MESSAGE_TOO_LARGE";
readonly RATE_LIMIT_EXCEEDED: "RATE_LIMIT_EXCEEDED";
readonly INTERNAL_SERVER_ERROR: "INTERNAL_SERVER_ERROR";
readonly BAD_REQUEST: "BAD_REQUEST";
readonly SERVICE_UNAVAILABLE: "SERVICE_UNAVAILABLE";
};
/**
* WebSocket error context with connection-specific information
*/
export interface WebSocketErrorContext extends ErrorContext {
connectionId?: string;
routeKey?: string;
eventType?: 'CONNECT' | 'DISCONNECT' | 'MESSAGE';
messageId?: string;
}
/**
* Enhanced WebSocket error with status code and error code
*/
export interface WebSocketError extends Error {
statusCode: number;
code: string;
context?: WebSocketErrorContext;
originalError?: unknown;
}
/**
* Creates a WebSocket-specific error with proper status code and error code
*/
export declare function throwWebSocketError(statusCode: number, code: keyof typeof WebSocketErrorCodes, message: string, context?: WebSocketErrorContext & {
originalError?: unknown;
}): never;
/**
* Convenience functions for common WebSocket error scenarios
*/
export declare const WebSocketErrors: {
/**
* Throws a validation error (400)
*/
validation(message: string, context?: WebSocketErrorContext, originalError?: unknown): never;
/**
* Throws an authentication error (401)
*/
authentication(message: string, context?: WebSocketErrorContext, originalError?: unknown): never;
/**
* Throws an authorization error (403)
*/
authorization(message: string, context?: WebSocketErrorContext, originalError?: unknown): never;
/**
* Throws a connection error (400)
*/
connection(message: string, context?: WebSocketErrorContext, originalError?: unknown): never;
/**
* Throws a message too large error (413)
*/
messageTooLarge(message: string, context?: WebSocketErrorContext, originalError?: unknown): never;
/**
* Throws a rate limit error (429)
*/
rateLimitExceeded(message: string, context?: WebSocketErrorContext, originalError?: unknown): never;
/**
* Throws a bad request error (400)
*/
badRequest(message: string, context?: WebSocketErrorContext, originalError?: unknown): never;
/**
* Throws a service unavailable error (503)
*/
serviceUnavailable(message: string, context?: WebSocketErrorContext, originalError?: unknown): never;
/**
* Throws an internal server error (500)
*/
internal(message: string, context?: WebSocketErrorContext, originalError?: unknown): never;
};
/**
* Checks if an error is a WebSocket error with status code
*/
export declare function isWebSocketError(error: unknown): error is WebSocketError;
/**
* Extracts WebSocket error information for response formatting
*/
export declare function extractWebSocketErrorInfo(error: WebSocketError): {
statusCode: number;
code: string;
message: string;
context?: WebSocketErrorContext;
};
//# sourceMappingURL=websocket.d.ts.map