/**
 * Create a custom error class with the ability to define the error
 * code, status, and the help text.
 *
 * ```js
 * export class FileNotFoundException extends Exception {
 *   static status = 500
 *   static code = 'E_FILE_NOT_FOUND'
 * }
 *
 * throw new FileNotFoundException(
 *  `Unable to find file from ${filePath} location`
 * )
 * ```
 */
export class Exception extends Error {
    /**
     * Name of the class that raised the exception.
     */
    name;
    /**
     * A status code for the error. Usually helpful when converting errors
     * to HTTP responses.
     */
    status;
    constructor(message, options) {
        super(message, options);
        const ErrorConstructor = this.constructor;
        this.name = ErrorConstructor.name;
        this.message = message || ErrorConstructor.message || '';
        this.status = options?.status || ErrorConstructor.status || 500;
        const code = options?.code || ErrorConstructor.code;
        if (code !== undefined) {
            this.code = code;
        }
        const help = ErrorConstructor.help;
        if (help !== undefined) {
            this.help = help;
        }
        Error.captureStackTrace(this, ErrorConstructor);
    }
    setHelp(help) {
        this.help = help;
        return this;
    }
    setCode(code) {
        this.code = code;
        return this;
    }
    setStatus(status) {
        this.status = status;
        return this;
    }
    get [Symbol.toStringTag]() {
        return this.constructor.name;
    }
    toString() {
        if (this.code) {
            return `${this.name} [${this.code}]: ${this.message}`;
        }
        return `${this.name}: ${this.message}`;
    }
    toJSON() {
        const result = {
            name: this.name,
            message: this.message,
        };
        if (this.status) {
            result.status = this.status;
        }
        if (this.code) {
            result.code = this.code;
        }
        if (this.help) {
            result.help = this.help;
        }
        return result;
    }
}
//# sourceMappingURL=exception.js.map