Skip to content

Commit e6ae428

Browse files
docs(README.md): add description
1 parent 5cf36f1 commit e6ae428

File tree

1 file changed

+175
-0
lines changed

1 file changed

+175
-0
lines changed

README.md

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Manages an [`Error`][js-error].
4242
* [Installation](#installation)
4343
* [Api](#api)
4444
* [`ValidationError`](#validationerror)
45+
* [Interface](#interface)
4546
* [Git](#git)
4647
* [Commit](#commit)
4748
* [Versioning](#versioning)
@@ -104,13 +105,187 @@ npm i --save @angular-package/error
104105
import {
105106
// Class.
106107
ValidationError,
108+
// Interface.
109+
ErrorMessage,
107110
} from '@angular-package/error';
108111
```
109112

110113
<br>
111114

112115
## `ValidationError`
113116

117+
Manages an [`Error`][js-error] of the validation.
118+
119+
**Static methods:**
120+
121+
| Methods | Description |
122+
| :----------------------------------------------------------------- | :---------- |
123+
| [`ValidationError.defineMessage()`](#validationerrordefinemessage) | Defines the error message of a [`string`][js-string] type from the provided `message` of an [`object`][js-object] |
124+
125+
**Constructor:**
126+
127+
| Constructor | Description |
128+
| :-------------------------------------------------- | :---------- |
129+
| [`ValidationError()`](#validationerror-constructor) | Creates a new instance with the message. If the provided `message` is an [`object`][js-object], then its properties are assigned to the instance |
130+
131+
<br>
132+
133+
## `ValidationError` static properties
134+
135+
### `ValidationError.template`
136+
137+
Template of the error message with the replaceable `[problem]` and `[fix]`.
138+
139+
```typescript
140+
static template = `Problem: [problem] => Fix: [fix]`;
141+
```
142+
143+
<br>
144+
145+
## `ValidationError` instance public properties
146+
147+
### `ValidationError.prototype.fix`
148+
149+
A possible solution to the described problem of a [`string`][js-string] type. By default, it's an empty [`string`][js-string].
150+
151+
```typescript
152+
public fix = '';
153+
```
154+
155+
<br>
156+
157+
### `ValidationError.prototype.name`
158+
159+
Error name of a string type that is being thrown. By default, it's `ValidationError`.
160+
161+
```typescript
162+
public name = ValidationError.name;
163+
```
164+
165+
<br>
166+
167+
### `ValidationError.prototype.problem`
168+
169+
The validation problem of a [`string`][js-string] type. By default, it's an empty string.
170+
171+
```typescript
172+
public problem = '';
173+
```
174+
175+
<br>
176+
177+
## `ValidationError` static methods
178+
179+
### `ValidationError.defineMessage()`
180+
181+
Defines the validation error message of a [`string`][js-string] type from the provided `message` of the [`ErrorMessage`](#errormessage) interface.
182+
183+
```typescript
184+
static defineMessage(message: ErrorMessage): string {
185+
if (is.objectKey(message, ['fix', 'problem'])) {
186+
return `Problem: ${message.problem}. ${
187+
is.string(message.fix) ? `Fix: ${message.fix}` : ''
188+
}`;
189+
}
190+
return '';
191+
}
192+
```
193+
194+
**Parameters:**
195+
196+
| Name: type | Description |
197+
| :---------------------- | :---------- |
198+
| `message: ErrorMessage` | An [`object`][js-object] of the [`ErrorMessage`](#errormessage) interface to build a message of a [`string`][js-string] type. The value is checked against the proper [`object`][js-object] |
199+
200+
**Returns:**
201+
202+
The **return value** is a message of a `string` type created from the provided `message` of [`ErrorMessage`](#errormessage) interface, or it's an empty `string` if the provided message object isn't proper.
203+
204+
**Usage:**
205+
206+
```typescript
207+
// Example usage.
208+
import { ValidationError } from '@angular-package/core';
209+
210+
const fix = 'There is no solution to the described problem.';
211+
const problem = 'The problem has no solution.';
212+
/**
213+
* Returns
214+
* --------
215+
* Problem: The problem has no solution. => Fix: There is no solution to the described problem.
216+
*/
217+
const errorMessage = ValidationError.defineMessage({ fix, problem });
218+
```
219+
220+
<br>
221+
222+
## `ValidationError` constructor
223+
224+
### `ValidationError()`
225+
226+
Creates a new instance with the message. If the provided `message` is an [`object`][js-object], then its properties are assigned to the instance.
227+
228+
```typescript
229+
new ValidationError(message: string | ErrorMessage) {
230+
super(is.string(message) ? message : ValidationError.defineMessage(message));
231+
if (is.object(message)) {
232+
Object.assign(this, getProperties(message, ['fix', 'problem']));
233+
}
234+
}
235+
```
236+
237+
**Parameters:**
238+
239+
| Name: type | Description |
240+
| :-------------------------------- | :---------- |
241+
| `message: string \| ErrorMessage` | The message of a `string` type or of an [`ErrorMessage`](#errormessage) interface that is used to throw with an [`error`][js-error] |
242+
243+
**Returns:**
244+
245+
The **return value** is an instance of [`ValidationError`](#validationerror).
246+
247+
**Usage:**
248+
249+
```typescript
250+
// Example usage.
251+
import { ValidationError } from '@angular-package/core';
252+
253+
const fix = 'There is no solution to the described problem.';
254+
const problem = 'The problem has no solution.';
255+
const validationError = new ValidationError({ fix, problem });
256+
```
257+
258+
<br>
259+
260+
## Interface
261+
262+
### ErrorMessage
263+
264+
The shape of an [`object`][js-object] for an [`error`][js-error] message that contains a possible solution to the described problem.
265+
266+
```typescript
267+
interface ErrorMessage {
268+
/**
269+
* Possible solution to the described problem of a `string` type.
270+
*/
271+
fix: string;
272+
/**
273+
* Error problem of a `string` type.
274+
*/
275+
problem: string;
276+
}
277+
```
278+
279+
### ResultHandler
280+
281+
Function to handle the result of the [`ResultCallback`][package-type-resultcallback] [`function`][js-function] before its result returns.
282+
283+
```typescript
284+
type ResultHandler = (result: boolean, value: any) => void;
285+
```
286+
287+
<br>
288+
114289
## GIT
115290

116291
### Commit

0 commit comments

Comments
 (0)