Skip to content

Commit f135aba

Browse files
committed
Revert "Revert "Change request/error handling""
This reverts commit 8914dab.
1 parent a058524 commit f135aba

File tree

9 files changed

+627
-263
lines changed

9 files changed

+627
-263
lines changed

CHANGELOG.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,59 @@ This driver uses semantic versioning:
1616

1717
## [9.2.0] - 2024-11-27
1818

19+
### Changed
20+
21+
- Errors encountered before a request completes are now wrapped in a
22+
`NetworkError` or a subclass thereof
23+
24+
This should help making it easier to diagnose network issues and distinguish
25+
the relevant error conditions.
26+
27+
The originating error can still be accessed using the `cause` property of the
28+
`NetworkError` error.
29+
30+
- `HttpError` now extends the `NetworkError` class
31+
32+
This allows treating all non-`ArangoError` errors as one category of errors,
33+
even when there is no server response available.
34+
35+
- `db.waitForPropagation` now throws a `PropagationTimeoutError` error when
36+
invoked with a `timeout` option and the timeout duration is exceeded
37+
38+
The method would previously throw the most recent error encountered while
39+
waiting for replication. The originating error can still be accessed using
40+
the `cause` property of the `PropagationTimeoutError` error.
41+
42+
- `db.waitForPropagation` now respects the `timeout` option more strictly
43+
44+
Previously the method would only time out if the timeout duration was
45+
exceeded after the most recent request failed. Now the timeout is
46+
recalculated and passed on to each request, preventing it from exceeding
47+
the specified duration.
48+
49+
If the propagation timed out due to an underlying request exceeding the
50+
timeout duration, the `cause` property of the `PropagationTimeoutError`
51+
error will be a `ResponseTimeoutError` error.
52+
53+
- `config.beforeRequest` and `config.afterResponse` callbacks can now return
54+
promises
55+
56+
If the callback returns a promise, it will be awaited before the request
57+
and response cycle proceeds. If either callback throws an error or returns
58+
a promise that is rejected, that error will be thrown instead.
59+
60+
- `config.afterResponse` callback signature changed
61+
62+
The callback signature previously used the internal `ArangojsResponse` type.
63+
The new signature uses the `Response` type of the Fetch API with an
64+
additional `request` property to more accurately represent the actual value
65+
it receives as the `parsedBody` property will never be present.
66+
67+
- `response` property on `ArangoError` is now optional
68+
69+
This property should always be present but this allows using the error in
70+
situations where a response might not be available.
71+
1972
### Added
2073

2174
- Added `database.availability` method
@@ -26,6 +79,67 @@ This driver uses semantic versioning:
2679

2780
- Added `database.supportInfo` method
2881

82+
- Added `onError` option to `Config` (DE-955)
83+
84+
This option can be used to specify a callback function that will be invoked
85+
whenever a request results in an error. Unlike `afterResponse`, this callback
86+
will be invoked even if the request completed but returned an error status.
87+
In this case the error will be the `HttpError` or `ArangoError` representing
88+
the error response.
89+
90+
If the `onError` callback throws an error or returns a promise that is
91+
rejected, that error will be thrown instead.
92+
93+
- Added `NetworkError` class
94+
95+
This is the common base class for all errors (including `HttpError`) that
96+
occur while making a request. The originating error can be accessed using the
97+
`cause` property. The request object can be accessed using the `request`
98+
property.
99+
100+
Note that `ArangoError` and the new `PropagationTimeoutError` error type
101+
do not extend `NetworkError` but may wrap an underlying error, which can
102+
be accessed using the `cause` property.
103+
104+
- Added `ResponseTimeoutError` class
105+
106+
This error extends `NetworkError` and is thrown when a request deliberately
107+
times out using the `timeout` option.
108+
109+
- Added `RequestAbortedError` class
110+
111+
This error extends `NetworkError` and is thrown when a request is aborted
112+
by using the `db.close` method.
113+
114+
- Added `FetchFailedError` class
115+
116+
This error extends `NetworkError` and is thrown when a request fails because
117+
the underlying `fetch` call fails (usually with a `TypeError`).
118+
119+
In Node.js the root cause of this error (e.g. a network failure) can often be
120+
found in the `cause` property of the originating error, i.e. the `cause`
121+
property of the `cause` property of this error.
122+
123+
In browsers the root cause is usually not exposed directly but can often
124+
be diagnosed by examining the developer console or network tab.
125+
126+
- Added `PropagationTimeoutError` class
127+
128+
This error does not extend `NetworkError` but wraps the most recent error
129+
encountered while waiting for replication, which can be accessed using the
130+
`cause` property. This error is only thrown when `db.waitForPropagation`
131+
is invoked with a `timeout` option and the timeout duration is exceeded.
132+
133+
- Added `ProcessedResponse` type
134+
135+
This type replaces the previously internal `ArangojsResponse` type and
136+
extends the native `Response` type with additional properties.
137+
138+
- Added optional `ArangoError#request` property
139+
140+
This property is always present if the error has a `response` property. In
141+
normal use this should always be the case.
142+
29143
- Added `keepNull` option to `CollectionInsertOptions` type (DE-946)
30144

31145
This option was previously missing from the type.

README.md

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,21 +139,42 @@ and [the `db` object](https://www.arangodb.com/docs/stable/appendix-references-d
139139

140140
## Error responses
141141

142-
If arangojs encounters an API error, it will throw an `ArangoError` with
143-
an `errorNum` property indicating the ArangoDB error code and the `code`
144-
property indicating the HTTP status code from the response body.
142+
If the server returns an ArangoDB error response, arangojs will throw an
143+
`ArangoError` with an `errorNum` property indicating the ArangoDB error code
144+
and expose the response body as the `response` property of the error object.
145145

146-
For any other non-ArangoDB error responses (4xx/5xx status code), it will throw
147-
an `HttpError` error with the status code indicated by the `code` property.
146+
For all other errors during the request/response cycle, arangojs will throw a
147+
`NetworkError` or a more specific subclass thereof and expose the originating
148+
request object as the `request` property of the error object.
148149

149-
If the server response did not indicate an error but the response body could
150-
not be parsed, a regular `SyntaxError` may be thrown instead.
150+
If the server responded with a non-2xx status code, this `NetworkError` will
151+
be an `HttpError` with a `code` property indicating the HTTP status code of the
152+
response and a `response` property containing the response object itself.
151153

152-
In all of these cases the server response object will be exposed as the
153-
`response` property on the error object.
154+
If the error is caused by an exception, the originating exception will be
155+
available as the `cause` property of the error object thrown by arangojs. For
156+
network errors, this will often be a `TypeError`.
154157

155-
If the request failed at a network level or the connection was closed without
156-
receiving a response, the underlying system error will be thrown instead.
158+
### Node.js network errors
159+
160+
In Node.js, network errors caused by a `TypeError` will often have a `cause`
161+
property containing a more detailed exception.
162+
163+
Specifically, these are often either system errors (represented by regular
164+
`Error` objects with additional properties) or errors from the `undici` module
165+
Node.js uses internally for its native `fetch` implementation.
166+
167+
Node.js system error objects provide a `code` property containing the specific
168+
string error code, a `syscall` property identifying the underlying system call
169+
that triggered the error (e.g. `connect`), as well as other helpful properties.
170+
171+
For more details on Node.js system errors, see the Node.js documentation of the
172+
[`SystemError` interface](https://nodejs.org/api/errors.html#class-systemerror)
173+
as well as the section on
174+
[Node.js error codes](https://nodejs.org/api/errors.html#nodejs-error-codes).
175+
176+
For more details on the errors thrown by `undici`, see the
177+
[undici errors documentation](https://undici.nodejs.org/#/docs/api/Errors.md).
157178

158179
## Common issues
159180

@@ -170,6 +191,15 @@ Additionally please ensure that your version of Node.js (or browser) and
170191
ArangoDB are supported by the version of arangojs you are trying to use. See
171192
the [compatibility section](#compatibility) for additional information.
172193

194+
You can install an older version of arangojs using `npm` or `yarn`:
195+
196+
```sh
197+
# for version 8.x.x
198+
yarn add arangojs@8
199+
# - or -
200+
npm install --save arangojs@8
201+
```
202+
173203
### No code intelligence when using require instead of import
174204

175205
If you are using `require` to import the `arangojs` module in JavaScript, the

0 commit comments

Comments
 (0)