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