Skip to main content

externalabstractRuntimeError

Base class for custom errors that may occur during execution of a test scenario.

Defining a custom error

import { RuntimeError } from '@serenity-js/core'

export class CustomError extends RuntimeError {
constructor(message: string, cause?: Error) {
super(CustomError, message, cause);
}
}

Wrapping a sync error

try {
operationThatMightThrowAnError();
} catch(error) {
// catch and re-throw
throw new CustomError('operationThatMightThrowAnError has failed', error);
}

Wrapping an async error

operationThatMightRejectAPromise()
.catch(error => {
// catch and re-throw
throw new CustomError('operationThatMightThrowAnError has failed', error)
})

Registering a custom error with ErrorSerialiser

import { RuntimeError } from '@serenity-js/core'
import { ErrorSerialiser } from '@serenity-js/core/io'

export class CustomError extends RuntimeError {

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

error.stack = serialised.stack as string;

return error;
}

constructor(message: string, cause?: Error) {
super(CustomError, message, cause);
}
}

ErrorSerialiser.registerErrorTypes(CustomError)

Hierarchy

Index

Properties

publicexternaloptionalreadonlycause

cause?: Error

The root cause of this RuntimeError, if any

externalname

name: string

externalmessage

message: string

externaloptionalstack

stack?: string

Methods

staticexternal[hasInstance]

  • [hasInstance](instance: unknown): boolean
  • Custom instanceof check that works across module boundaries. This addresses the dual-package hazard where the same class loaded from both ESM and CJS creates distinct constructor functions.

    The check walks up the prototype chain of the instance and compares constructor names, which remain consistent across module boundaries.


    Parameters

    • externalinstance: unknown

    Returns boolean

externaltoString

  • toString(): string
  • Human-readable description of the error


    Returns string

externaltoJSON

  • toJSON(): object
  • Returns object