/**
 * An error that serializes!
 * It is a copy of the normal definition of an error but allows
 * to serialize the value via the toJSON() function and restore previous values
 * with the `new()` function.
 */
export class SerializableError {
    message;
    code;
    stackValue;
    get stack() {
        return this.stackValue;
    }
    name = 'SerializableError';
    constructor(message, options = {}) {
        if (typeof message === 'string') {
            this.message = message;
        }
        else if (message) {
            this.message = message.message;
            this.stackValue = message.stack;
        }
        else {
            this.message = '';
        }
        if (typeof options === 'string' || typeof options === 'number') {
            this.code = options;
        }
        else if (options.cause && options.cause.stack) {
            this.stackValue = options.cause.stack;
        }
    }
    new(values) {
        if (values.message) {
            this.message = values.message;
        }
        if (values.stack) {
            this.stackValue = values.stack;
        }
        if (values.code || values.code === 0) {
            this.code = values.code;
        }
    }
    toJSON() {
        const { message, stackValue: stack, code } = this;
        const result = {
            message,
        };
        if (stack) {
            result.stack = stack;
        }
        if (code || code === 0) {
            result.code = code;
        }
        return result;
    }
    toString() {
        return this.message;
    }
}
//# sourceMappingURL=SerializableError.js.map