@thalorlabs/errors
Version:
Enhanced exception handling system for TypeScript applications with comprehensive error classes and debugging capabilities
88 lines (87 loc) • 3.1 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DatabaseError = exports.DatabaseErrorType = void 0;
const types_1 = require("@thalorlabs/types");
const CustomError_1 = __importDefault(require("./CustomError"));
/**
* Enum for database error types.
*
* Used to classify different types of database failures for better error handling.
*/
var DatabaseErrorType;
(function (DatabaseErrorType) {
DatabaseErrorType["CONNECTION_FAILED"] = "CONNECTION_FAILED";
DatabaseErrorType["QUERY_FAILED"] = "QUERY_FAILED";
DatabaseErrorType["TRANSACTION_FAILED"] = "TRANSACTION_FAILED";
DatabaseErrorType["CONSTRAINT_VIOLATION"] = "CONSTRAINT_VIOLATION";
DatabaseErrorType["TIMEOUT"] = "TIMEOUT";
DatabaseErrorType["DEADLOCK"] = "DEADLOCK";
DatabaseErrorType["UNKNOWN"] = "UNKNOWN";
})(DatabaseErrorType || (exports.DatabaseErrorType = DatabaseErrorType = {}));
/**
* Error for database operation failures with detailed context.
*
* Used when database operations fail with specific error types and context.
* Includes database type, operation, and error classification with HTTP 400 status.
*
* @example
* throw new DatabaseError(
* 'Connection timeout',
* 'MongoDB',
* 'find',
* DatabaseErrorType.CONNECTION_FAILED,
* originalError,
* 'req-123',
* { collection: 'users', query: { _id: '123' } }
* );
*
* throw new DatabaseError(
* 'Query failed',
* 'PostgreSQL',
* 'insert',
* DatabaseErrorType.QUERY_FAILED,
* undefined,
* 'req-456'
* );
*/
class DatabaseError extends CustomError_1.default {
constructor(message, databaseType, operation, errorType = DatabaseErrorType.UNKNOWN, originalError, requestId, context) {
super(types_1.EHttpClientErrorResponse.BAD_REQUEST, message, requestId, context);
this.databaseType = databaseType;
this.operation = operation;
this.errorType = errorType;
this.originalError = originalError;
}
getErrorResponse() {
const response = super.getErrorResponse();
response.databaseType = this.databaseType;
response.operation = this.operation;
response.errorType = this.errorType;
if (this.originalError) {
response.originalError = {
message: this.originalError.message,
name: this.originalError.name,
};
}
return response;
}
toJSON() {
const json = super.toJSON();
json.databaseType = this.databaseType;
json.operation = this.operation;
json.errorType = this.errorType;
json.originalError = this.originalError
? {
message: this.originalError.message,
name: this.originalError.name,
stack: this.originalError.stack,
}
: undefined;
return json;
}
}
exports.DatabaseError = DatabaseError;
exports.default = DatabaseError;