@thalorlabs/errors
Version:
Enhanced exception handling system for TypeScript applications with comprehensive error classes and debugging capabilities
45 lines (44 loc) • 1.76 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ValidationError = void 0;
const types_1 = require("@thalorlabs/types");
const CustomError_1 = __importDefault(require("./CustomError"));
/**
* Error for input validation failures with detailed field-level information.
*
* Used when request data fails validation with specific field errors.
* Provides structured error information for API responses with HTTP 422 status.
*
* @example
* throw new ValidationError('Validation failed', [
* { field: 'email', message: 'Invalid email format', value: 'invalid-email' },
* { field: 'password', message: 'Password too short', value: '123' }
* ], 'req-123');
*
* // Add more validation errors dynamically
* validationError.addValidationError('age', 'Must be 18 or older', 16);
*/
class ValidationError extends CustomError_1.default {
constructor(message = 'Validation Error', validationErrors = [], requestId, context) {
super(types_1.EHttpClientErrorResponse.UNPROCESSABLE_ENTITY, message, requestId, context);
this.validationErrors = validationErrors;
}
addValidationError(field, message, value, code) {
this.validationErrors.push({ field, message, value, code });
}
getErrorResponse() {
const response = super.getErrorResponse();
response.errors = this.validationErrors;
return response;
}
toJSON() {
const json = super.toJSON();
json.validationErrors = this.validationErrors;
return json;
}
}
exports.ValidationError = ValidationError;
exports.default = ValidationError;
;