barcode-exception.js

/**
 * @class
 * @extends {Error}
 */
class BarcodeException extends Error {
    /**
     * Constructor of BarcodeException.
     * @param {Error|string} exc - Exception or error message
     */
    constructor(exc) {
        let message = '';

        if (exc instanceof Error) {
            message = exc.message;
        } else if (typeof exc === 'string') {
            message = exc;
        } else {
            message = 'An unknown error occurred.';
        }

        super(message);
        this.name = 'BarcodeException';
    }

    /**
     * Gets the exception details.
     * @param {Error|string} exc - The exception or error message.
     * @returns {string} The exception details.
     */
    getDetails(exc) {
        let details = '';

        if (exc instanceof Error) {
            details += `Exception type: ${exc.constructor.name}\n`;
            details += `Message: ${exc.message}\n`;

            if (typeof exc.stack === 'string') {
                details += `Stack trace:\n${exc.stack}`;
            }
        } else if (typeof exc === 'string') {
            details = exc;
        } else {
            details = 'No additional details available.';
        }

        return details;
    }
}

module.exports = BarcodeException;