import type { JSONObject } from 'tiny-types';

import { ErrorSerialiser } from '../ErrorSerialiser.js';
import { RuntimeError } from './RuntimeError.js';

/**
 * Thrown to indicate that an assertion has failed.
 *
 * @group Errors
 */
export class AssertionError extends RuntimeError {

    static fromJSON(serialised: JSONObject): AssertionError {
        const error = new AssertionError(
            serialised.message as string,
            ErrorSerialiser.deserialise(serialised.cause as string | undefined),
        );

        error.stack = serialised.stack as string;

        return error;
    }

    /**
     * @param message - Human-readable description of the error and the difference between the expected and actual values
     * @param [cause] - The root cause of this [`RuntimeError`](https://serenity-js.org/api/core/class/RuntimeError/), if any
     */
    constructor(message: string, cause?: Error) {
        super(AssertionError, message, cause);
    }
}
