# Constructor

## `CommonError()`

Creates an error instance with the [message](https://docs.angular-package.dev/error/commonerror/accessors/get-message) built from the given [problem](#problem-string), its [solution](#fix-string), optional [type](#additional-link-string-min-number-max-number-type-string), [range](#additional-link-string-min-number-max-number-type-string), an explicit [identification](#id-id) on the supplied or stored [template](#template-string-commonerror.template).

{% code title="common-error.class.ts" %}

```typescript
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;
}
```

{% endcode %}

### Parameters

#### `problem:`[<mark style="color:green;">`string`</mark>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)

Description of the problem of a [`string`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) type.

#### `fix:`[<mark style="color:green;">`string`</mark>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)

A solution to the given [`problem`](#problem-string) of a [`string`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) type.

#### `id?:`[<mark style="color:green;">`Id`</mark>](https://docs.angular-package.dev/error/generic-type-variables#wrap-opening)

Optional unique [identification](https://docs.angular-package.dev/error/getting-started/basic-concepts#identification) to the given [`problem`](#problem-string) of generic type variable [`Id`](https://docs.angular-package.dev/error/generic-type-variables#commonerror-less-than-id-greater-than).

#### `template:`[<mark style="color:green;">`string`</mark>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)`=`<mark style="color:green;">`CommonError`</mark>`.template`

A template of error message with the replaceable [`{problem}`](#problem), [`{fix}`](#fix) and optional [`{id}`](#id), `{link}`, [`{max}`](#max), [`{min}`](#min) and [`{type}`](#type) tags.

{% hint style="info" %}
By default, the value is equal to the static property [`template`](https://docs.angular-package.dev/error/commonerror/properties/static-template).
{% endhint %}

#### `additional: {link?:`[<mark style="color:green;">`string`</mark>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)`; min?:`[<mark style="color:green;">`number`</mark>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)`; max?:`[<mark style="color:green;">`number`</mark>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)`; type?:`[<mark style="color:green;">`string`</mark>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)`}`

An optional [`object`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) consists of optional `link`, `min`, `max`, and `type` properties to define the error [`message`](https://docs.angular-package.dev/error/commonerror/accessors/get-message).&#x20;

`link` - The link to read more about the thrown error replaceable on the given [`template`](#template-string-commonerror.template) as [`{link}`](https://docs.angular-package.dev/error/properties/static-template#link) tag.\
`max`   - The maximum number replaceable on the given [`template`](#template-string-commonerror.template) as [`{max}`](https://docs.angular-package.dev/error/properties/static-template#max) tag.\
`min`   - The minimum number is replaceable on the given [`template`](#template-string-commonerror.template) as [`{min}`](https://docs.angular-package.dev/error/properties/static-template#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`](#template-string-commonerror.template) as the [`{type}`](https://docs.angular-package.dev/error/properties/static-template#type) tag.

## Example usage

### Basic usage

Example with the given required `problem` and `fix`.

```typescript
// 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`.

```typescript
// 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`.

```typescript
// 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`.

```typescript
// 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 }`&#x20;

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

```typescript
// 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 }`&#x20;

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

```typescript
// 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
);
```
