# Constructor

## `CommonError()`

Creates an error instance with the [message](/commonerror/accessors/get-message.md) 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>](/commonerror/generic-type-variables.md#wrap-opening)

Optional unique [identification](/getting-started/basic-concepts.md#identification) to the given [`problem`](#problem-string) of generic type variable [`Id`](/commonerror/generic-type-variables.md#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`](/commonerror/properties/static-template.md).
{% 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`](/commonerror/accessors/get-message.md).&#x20;

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://error.angular-package.dev/commonerror/constructor.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
