whatsapp-crm-common
Version:
Componentes compartidos para servicios de WhatsApp CRM - Common utilities and types for WhatsApp CRM system
74 lines • 2.83 kB
JavaScript
"use strict";
/**
* Custom error classes for repository operations
* These errors provide better categorization and handling for database operations
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChatRepositoryError = exports.MessageRepositoryError = exports.RepositoryError = void 0;
exports.createRepositoryError = createRepositoryError;
class RepositoryError extends Error {
constructor(message, context, originalError, isRetryable = true) {
super(message);
this.name = 'RepositoryError';
this.context = context;
this.originalError = originalError;
this.isRetryable = isRetryable;
// Capture stack trace
if (Error.captureStackTrace) {
Error.captureStackTrace(this, RepositoryError);
}
}
/**
* Creates a detailed error message with context
*/
getDetailedMessage() {
const contextStr = JSON.stringify(this.context, null, 2);
const originalStack = this.originalError?.stack || 'No original stack';
return `${this.message}\nContext: ${contextStr}\nOriginal Error: ${this.originalError?.message || 'None'}\nOriginal Stack: ${originalStack}`;
}
/**
* Gets context information for logging
*/
getLogContext() {
return {
errorType: this.name,
message: this.message,
isRetryable: this.isRetryable,
tenantId: this.context.tenantId,
agentId: this.context.agentId,
operation: this.context.operation,
details: this.context.details,
originalError: this.originalError?.message,
stack: this.stack,
originalStack: this.originalError?.stack
};
}
}
exports.RepositoryError = RepositoryError;
/**
* Error for message-specific operations
*/
class MessageRepositoryError extends RepositoryError {
constructor(context, originalError, isRetryable = true) {
super('Message repository operation failed', context, originalError, isRetryable);
this.name = 'MessageRepositoryError';
}
}
exports.MessageRepositoryError = MessageRepositoryError;
/**
* Error for chat-specific operations
*/
class ChatRepositoryError extends RepositoryError {
constructor(context, originalError, isRetryable = true) {
super('Chat repository operation failed', context, originalError, isRetryable);
this.name = 'ChatRepositoryError';
}
}
exports.ChatRepositoryError = ChatRepositoryError;
/**
* Factory function to create appropriate error based on original error
*/
function createRepositoryError(operation, context, originalError) {
return new RepositoryError(`Repository operation failed: ${operation}`, { ...context, operation }, originalError);
}
//# sourceMappingURL=repository-errors.js.map