Core Exceptions

Exceptions

In Angularity, JavaScript runtime errors are divided into two categories: Errors and Exceptions.

  • Exceptions are instances of the Exception class. They are expected to be caught and appropriately handled.
  • Errors are all the errors that are not instances of Exception. They are regarded as critical errors that should never happen and typically breaks the application, such as TypeError. The occurrence of such an error implies something is wrong within the application that awaits fixes.

To create a type of exception, declare a class that extends from the Exception class.

class EntityNotFoundException extends Exception {}

Additional customizations can apply to add contextual properties or generate uniformed error messages:

class EntityNotFoundException extends Exception {
  constructor(
    readonly type: Type<any>,
    readonly id: string,
  ) {
    super(`${type.name} of id ${id} is not found.`);
  }
}

It is worthy to note that both Exceptions and Errors are instances of Error. The way to distinguish them is instanceof Exception, not instanceof Error.

catch (e) {
  const isException = e instanceof Exception;
  const isError = !(e instanceof Exception);
}

A recommended way to compose the catch block is to exhaust all the exceptions that are expected to be caught and re-throw the error if none of the exceptions are matched:

catch (e) {
  if (e instanceof SomeException) {
    doSomething();
  } else if (e instanceof AnotherException) {
    doSomething();
  } else {
    throw e
  }
}

All Angularity libraries follow this convention for error handling, and it is recommended to also follow such convention in applications that uses Angularity.