@stack.thefennec.dev/telegram-export-parser
Version:
TypeScript library for parsing Telegram Desktop's data export with full type safety
108 lines • 3.43 kB
JavaScript
;
/**
* @fileoverview Error handling for Telegram export parsing operations.
*
* Provides enhanced error types with context preservation and error chaining
* for debugging complex parsing failures in large Telegram exports.
*
* @example
* ```typescript
* import { TelegramExportParseError } from './errors'
*
* try {
* const parsed = parseMessage(rawData)
* } catch (error) {
* if (error instanceof TelegramExportParseError) {
* console.log('Context:', error.context)
* console.log('Root cause:', error.cause)
* }
* }
* ```
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.TelegramExportParseError = void 0;
/**
* Enhanced error class for Telegram export parsing failures.
*
* Extends native Error with context preservation and error chaining capabilities.
* Essential for debugging parsing issues in large exports where failures need
* detailed context about the failed data and underlying causes.
*
* **Features:**
* - **Context preservation** - Stores raw data that caused the failure
* - **Error chaining** - Links to underlying errors for root cause analysis
* - **Stack trace capture** - Proper stack traces in Node.js environments
* - **Type safety** - Works seamlessly with TypeScript error handling
*
* @example Basic usage
* ```typescript
* throw new TelegramExportParseError('Invalid message format')
* ```
*
* @example With context for debugging
* ```typescript
* throw new TelegramExportParseError(
* 'Failed to parse message',
* rawMessageData // Preserve failing data
* )
* ```
*
* @example Error chaining
* ```typescript
* try {
* parseDate(rawDate)
* } catch (dateError) {
* throw new TelegramExportParseError(
* 'Message contains invalid date',
* rawMessage,
* dateError // Chain the original error
* )
* }
* ```
*/
class TelegramExportParseError extends Error {
/** Raw data that caused the parsing failure (for debugging) */
context;
/** Underlying error that triggered this parsing failure (for error chaining) */
cause;
/**
* Creates a new parsing error with optional context and cause chaining.
*
* @param message - Human-readable error description
* @param context - Raw data that caused the failure (preserved for debugging)
* @param cause - Underlying error that triggered this failure (for error chaining)
*
* @example Parser error with context
* ```typescript
* const rawMessage = { id: 123, invalid: 'data' }
* throw new TelegramExportParseError(
* 'Message missing required "type" field',
* rawMessage // Preserve for debugging
* )
* ```
*
* @example Error chaining
* ```typescript
* try {
* JSON.parse(malformedJson)
* } catch (jsonError) {
* throw new TelegramExportParseError(
* 'Invalid JSON in message data',
* rawData,
* jsonError // Chain the JSON parsing error
* )
* }
* ```
*/
constructor(message, context, cause) {
super(message);
this.name = 'TelegramExportParseError';
this.context = context;
this.cause = cause;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, TelegramExportParseError);
}
}
}
exports.TelegramExportParseError = TelegramExportParseError;
//# sourceMappingURL=errors.js.map