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: docs/docs/api/Dispatcher.md
+197Lines changed: 197 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -986,6 +986,203 @@ client.dispatch(
986
986
);
987
987
```
988
988
989
+
##### `Response Error Interceptor`
990
+
991
+
**Introduction**
992
+
993
+
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.
994
+
995
+
**ResponseError Class**
996
+
997
+
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.
998
+
999
+
**Definition**
1000
+
1001
+
```js
1002
+
classResponseErrorextendsUndiciError {
1003
+
constructor (message, code, { headers, data }) {
1004
+
super(message);
1005
+
this.name='ResponseError';
1006
+
this.message= message ||'Response error';
1007
+
this.code='UND_ERR_RESPONSE';
1008
+
this.statusCode= code;
1009
+
this.data= data;
1010
+
this.headers= headers;
1011
+
}
1012
+
}
1013
+
```
1014
+
1015
+
**Interceptor Handler**
1016
+
1017
+
The interceptor's handler class extends `DecoratorHandler` and overrides methods to capture response details and handle errors based on the response status code.
1018
+
1019
+
**Methods**
1020
+
1021
+
-**onConnect**: Initializes response properties.
1022
+
-**onHeaders**: Captures headers and status code. Decodes body if content type is `application/json` or `text/plain`.
1023
+
-**onData**: Appends chunks to the body if status code indicates an error.
1024
+
-**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.
0 commit comments