You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: deps/undici/src/docs/docs/api/Dispatcher.md
+197
Original file line number
Diff line number
Diff line change
@@ -984,6 +984,203 @@ client.dispatch(
984
984
);
985
985
```
986
986
987
+
##### `Response Error Interceptor`
988
+
989
+
**Introduction**
990
+
991
+
The Response Error Interceptor is designed to handle HTTP response errors efficiently. It intercepts responses and throws detailed errors for responses with status codes indicating failure (4xx, 5xx). This interceptor enhances error handling by providing structured error information, including response headers, data, and status codes.
992
+
993
+
**ResponseError Class**
994
+
995
+
The `ResponseError` class extends the `UndiciError` class and encapsulates detailed error information. It captures the response status code, headers, and data, providing a structured way to handle errors.
996
+
997
+
**Definition**
998
+
999
+
```js
1000
+
classResponseErrorextendsUndiciError {
1001
+
constructor (message, code, { headers, data }) {
1002
+
super(message);
1003
+
this.name='ResponseError';
1004
+
this.message= message ||'Response error';
1005
+
this.code='UND_ERR_RESPONSE';
1006
+
this.statusCode= code;
1007
+
this.data= data;
1008
+
this.headers= headers;
1009
+
}
1010
+
}
1011
+
```
1012
+
1013
+
**Interceptor Handler**
1014
+
1015
+
The interceptor's handler class extends `DecoratorHandler` and overrides methods to capture response details and handle errors based on the response status code.
1016
+
1017
+
**Methods**
1018
+
1019
+
-**onConnect**: Initializes response properties.
1020
+
-**onHeaders**: Captures headers and status code. Decodes body if content type is `application/json` or `text/plain`.
1021
+
-**onData**: Appends chunks to the body if status code indicates an error.
1022
+
-**onComplete**: Finalizes error handling, constructs a `ResponseError`, and invokes the `onError` method.
The Response Error Interceptor provides a robust mechanism for handling HTTP response errors by capturing detailed error information and propagating it through a structured `ResponseError` class. This enhancement improves error handling and debugging capabilities in applications using the interceptor.
-**retry**`(err: Error, context: RetryContext, callback: (err?: Error | null) => void) => void` (optional) - Function to be called after every retry. It should pass error if no more retries should be performed.
22
+
-**retry**`(err: Error, context: RetryContext, callback: (err?: Error | null) => void) => number | null` (optional) - Function to be called after every retry. It should pass error if no more retries should be performed.
23
23
-**maxRetries**`number` (optional) - Maximum number of retries. Default: `5`
24
24
-**maxTimeout**`number` (optional) - Maximum number of milliseconds to wait before retrying. Default: `30000` (30 seconds)
25
25
-**minTimeout**`number` (optional) - Minimum number of milliseconds to wait before retrying. Default: `500` (half a second)
0 commit comments