UNPKG

jsm-exceptions

Version:

A comprehensive TypeScript exception library with HTTP status code support, detailed JSDoc documentation, and backward compatibility. Provides structured error handling for web applications and APIs.

54 lines (53 loc) 2.03 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const base_exception_1 = __importDefault(require("./base.exception")); /** * @fileoverview Value Already Exists exception (HTTP 409) * @author dr. Salmi <reevosolutions@gmail.com> */ /** * Exception thrown when attempting to create a resource that already exists. * Corresponds to HTTP 409 Conflict status code. * * @class ValueAlreadyExistsException * @extends {BaseException} * @example * ```typescript * throw new ValueAlreadyExistsException('Email already exists'); * throw new ValueAlreadyExistsException('Username is taken', { * username: { value: 'john_doe', message: 'This username is already taken' } * }); * ``` */ class ValueAlreadyExistsException extends base_exception_1.default { /** * Creates an instance of ValueAlreadyExistsException. * * @param {string} [message='Value already exists'] - The error message * @param {ErrorFields} [fields] - Field-specific information about conflicting values * @param {Record<string, any>} [context] - Additional context * @memberof ValueAlreadyExistsException */ constructor(message = 'Value already exists', fields, context) { super(message, 409, context); /** * Indicates if this exception was created from a Mongoose validation error * @type {boolean} */ this.isMongoose = false; this.fields = fields; } /** * Returns a JSON representation of the value already exists exception * * @returns {object} JSON representation including field information * @memberof ValueAlreadyExistsException */ toJSON() { return Object.assign(Object.assign({}, super.toJSON()), { name: this.constructor.name, fields: this.fields, isMongoose: this.isMongoose }); } } exports.default = ValueAlreadyExistsException;