source: node_modules/@swagger-api/apidom-error/README.md

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 4.2 KB
Line 
1# @swagger-api/apidom-error
2
3`apidom-error` provides several error classes in support of [Joyent's Best Practices for Error Handling in Node.js](http://web.archive.org/web/20150221074228/http://www.joyent.com/developers/node/design/errors).
4These error classes should be used as a superclass for all **operational errors**.
5Operational errors represent run-time problems experienced by correctly-written programs.
6
7Error classes from `apidom-error` handle complexities of extending native Error class
8and support [error chaining](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause)
9in backward and forward compatible way.
10
11## Base errors
12
13Base errors are meant to directly be thrown by ApiDOM code or to be extended
14to form custom error hierarchies.
15
16### ApiDOMError
17
18Basic error class that can be easily extended and form error chains.
19
20```js
21import { ApiDOMError } from '@swagger-api/apidom-error';
22
23const error1 = new ApiDOMError('error message'); // basic error
24const error2 = new ApiDOMError('error message', { cause: new Error('cause') }); // error chain
25```
26
27### ApiDOMStructuredError
28
29Error class that is based on `ApiDOMError` and allows to assign arbitrary data properties to its instances.
30
31```js
32import { ApiDOMStructuredError } from '@swagger-api/apidom-error';
33
34const error1 = new ApiDOMStructuredError('error message', { cause: new Error('cause') }); // just like ApiDOMError
35
36const error2 = new ApiDOMStructuredError('error message', {
37 prop1: 'value1',
38 prop2: 3,
39})
40error2.prop1; // => 'value1'
41error2.prop2; // => 3
42```
43
44Although structured error class gives convenience by assigning arbitrary properties
45it's highly recommended to extend `ApiDOMStructuredError` class and create public properties
46explicitly.
47
48```typescript
49import { ApiDOMStructuredError, ApiDOMErrorOptions } from '@swagger-api/apidom-error';
50
51interface StructuredErrorOptions extends ApiDOMErrorOptions {
52 readonly prop1: string;
53 readonly prop2: number;
54}
55
56class PropError extends ApiDOMStructuredError {
57 public readonly prop1: string;
58 public readonly prop2: number;
59
60 constructor(message?: string, options?: StructuredErrorOptions) {
61 super(message, options)
62 if (typeof options !== 'undefined') {
63 this.prop1 = options.prop1;
64 this.prop2 = options.prop2;
65 }
66 }
67}
68```
69
70### ApiDOMAggregateError
71
72`ApiDOMAggregateError` can be easily extended and represents instance of an error when
73several errors need to be wrapped in a single error. It is thrown when multiple errors
74need to be reported by an operation. Supports error chaining as well.
75
76```js
77import { ApiDOMAggregateError } from '@swagger-api/apidom-error';
78
79const error1 = new Error('error1');
80const error2 = new Error('error2');
81const cause = new Error('optional cause');
82
83const aggregateError = new ApiDOMAggregateError([error1, error2], 'aggregate error', {
84 cause,
85});
86```
87
88This class is based on native [AggregateError], and due to the fact that JavaScript
89classes don't support multiple inheritance, it cannot also be based on `ApiDOMError`.
90Nevertheless, for convenience, following will work:
91
92```js
93import { ApiDOMAggregateError, ApiDOMError } from '@swagger-api/apidom-error';
94
95const aggregateError = new ApiDOMAggregateError([]);
96aggregateError instanceof ApiDOMError; // => true
97aggregateError instanceof AggregateError; // => true
98```
99
100## Generic custom errors
101
102Generic custom errors represents custom errors that are generic enough
103to be used across ApiDOM monorepo packages and are not specific to any
104particular ApiDOM package.
105
106### UnsupportedOperationError
107
108This error class that is based on `ApiDOMError`. Thrown to indicate that the requested operation is not supported.
109
110```js
111import { UnsupportedOperationError } from '@swagger-api/apidom-error';
112
113const error = new UnsupportedOperationError('error message');
114```
115
116### NotImplementedError
117
118Error class that is based on `UnsupportedOperationError` and. It is thrown to indicate that a block
119of code has not been implemented. This exception provides a more semantically rich description
120of the problem than base `ApiDOMError`.
121
122```js
123import { NotImplementedError } from '@swagger-api/apidom-error';
124
125const error = new NotImplementedError('error message');
126```
127
128
Note: See TracBrowser for help on using the repository browser.