Constructor

The `CommonError` object constructor

CommonError()

Creates an error instance with the message built from the given problem, its solution, optional type, range, an explicit identification on the supplied or stored template.

common-error.class.ts
constructor(
  problem: string,
  fix: string,
  id?: Id,
  template = CommonError.template,
  additional?: { link?: string; max?: number; min?: number; type?: string }
) {
  super(
    CommonError.defineMessage`${problem}${fix}${id}${template}${additional}`
  );
  this.#fix = fix;
  this.#id = id;
  this.#link = additional?.link;
  this.#problem = problem;
  this.#template = template;
}

Parameters

problem:string

Description of the problem of a string type.

fix:string

A solution to the given problem of a string type.

id?:Id

Optional unique identification to the given problem of generic type variable Id.

template:string=CommonError.template

A template of error message with the replaceable {problem}, {fix} and optional {id}, {link}, {max}, {min} and {type} tags.

By default, the value is equal to the static property template.

An optional object consists of optional link, min, max, and type properties to define the error message.

link - The link to read more about the thrown error replaceable on the given template as {link} tag. max - The maximum number replaceable on the given template as {max} tag. min - The minimum number is replaceable on the given template as {min} tag. type - The type indicates the expected type that isn't throwing an error or the not expected type that is throwing an error replaceable on the given template as the {type} tag.

Example usage

Basic usage

Example with the given required problem and fix.

// Example usage.
import { CommonError } from '@angular-package/error';

// Create `TestError` to extend.
class TestError<Id extends string> extends CommonError<Id> {}

// Uncaught Error: Problem: problem => Fix: fix
throw new TestError(
  'problem',
  'fix'
);

id

Example with the given id.

// Example usage.
import { CommonError } from '@angular-package/error';

// Create `TestError` to extend.
class TestError<Id extends string> extends CommonError<Id> {}

// Uncaught Error: Problem(AE:427): problem => Fix: fix
throw new TestError(
  'problem',
  'fix',
  '(AE:427)' // <--- Parameter `id`.
);

id, template

Example with the given id and template.

// Example usage.
import { CommonError } from '@angular-package/error';

// Create `TestError` to extend.
class TestError<Id extends string> extends CommonError<Id> {}

// Uncaught Error: problem(AE:427). fix
throw new TestError(
  'problem',
  'fix',
  'AE:427', // <--- Parameter `id`
  '{problem}({id}). {fix}' // <--- Parameter `template`
);

id, template, additional{ min }

Example with the given id, template and property min of additional.

// Example usage.
import { CommonError } from '@angular-package/error';

// Create `TestError` to extend.
class TestError<Id extends string> extends CommonError<Id> {}

// Uncaught Error: (AE:427)Age must be above 9. Provide age more than 9
throw new TestError(
  'Age must be above ', // Problem
  'Provide age more than ', // Fix
  'AE:427', // Identification
  '({id}){problem}{min}. {fix}{min}', // Template
  { min: 9 } // Additional
);

id, template, additional{ min, max }

Example with the given id, template and property min and max of additional.

// Example usage.
import { CommonError } from '@angular-package/error';

// Create `TestError` to extend.
class TestError<Id extends string> extends CommonError<Id> {}

// Uncaught Error: (AE:427)The `age` parameter is 45. Provided `age` must be between 9 and 12
throw new TestError(
  'The `age` parameter is 45.', // Problem
  'Provided `age` must be', // Fix
  'AE:427', // Identification
  '({id}){problem} {fix} between {min} and {max}', // Template
  { min: 9, max: 12 } // Additional
);

id, template, additional{ min, max, type }

Example with the given id, template, property min, max and type of additional.

// Example usage.
import { CommonError } from '@angular-package/error';

// Create `TestError` to extend.
class TestError<Id extends string> extends CommonError<Id> {}

// Uncaught Error: (AE:427)The `age` parameter is not a number. Provided `age` must be a  number between 9 and 12.
throw new TestError(
  'The `age` parameter is not a', // Problem
  'Provided `age` must be a ', // Fix
  'AE:427', // Identification
  '({id}){problem} {type}. {fix} {type} between {min} and {max}.', // Template
  { min: 9, max: 12, type: 'number' } // Additional
);

Last updated