Skip to main content

externalRaiseErrors

An Ability that enables an Actor to create a Serenity/JS RuntimeError from within a custom Interaction.

The stack trace of an error created this way includes the filesystem location pointing to where the interaction was invoked, which makes debugging any failures easier.

Pro tip

The ability to RaiseErrors is given to all Serenity/JS actors by default, so you don't need to configure it explicitly.

Raising an error

import { Interaction, LogicError, RaiseErrors, the } from '@serenity-js/core'
import isPathInside from 'is-path-inside'
import { unlink } from 'fs/promises'

const RemoveFile = (pathToFile: string) =>
Interaction.where(the`#actor removes a file at ${ pathToFile }`, async actor => {

if (! isPathInside(pathToFile, process.cwd())) {

throw RaiseErrors.as(actor).create(LogicError, {
message: `Removing files outside the current working directory is not allowed`
diff: {
expected: process.cwd(),
actual: pathToFile,
}
})
}

await unlink(pathToFile);
})

Learn more

Hierarchy

Index

Constructors

externalconstructor

Methods

externalcreate

  • create<RE>(errorType: new (...args: any[]) => RE, options: ErrorOptions): RE
  • Type parameters

    Parameters

    • externalerrorType: new (...args: any[]) => RE
      • externaloptions: ErrorOptions

      Returns RE

    externaltoJSON

    • Returns a JSON representation of the ability and its current state, if available. The purpose of this method is to enable reporting the state of the ability in a human-readable format, rather than to serialise and deserialise the ability itself.


      Returns SerialisedAbility

    externalabilityType

    • Returns the most abstract type of this Ability instance, specifically the first class in the inheritance hierarchy that directly extends the Ability class.

      import { Ability } from '@serenity-js/core';

      class MyAbility extends Ability {}
      class MySpecialisedAbility extends MyAbility {}

      new MyAbility().abilityType(); // returns MyAbility
      new MySpecialisedAbility().abilityType(); // returns MyAbility

      Returns AbilityType<Ability>