2
2
3
3
## Usage
4
4
5
- ``` js
6
- const { HTTPClient } = require ( " @loke/http-client" ) ;
5
+ ``` ts
6
+ import { HTTPClient , HTTPResponseError } from " @loke/http-client" ;
7
7
8
8
class MyClient extends HTTPClient {
9
9
constructor () {
@@ -53,22 +53,31 @@ class MyClient extends HTTPClient {
53
53
case 404 :
54
54
throw new ThingNotFoundError (body .message );
55
55
default :
56
- console .error (
57
- " Unexpected status code" ,
58
- err .status ,
59
- body .message ,
60
- );
56
+ console .error (" Unexpected status code" , err .status , body .message );
61
57
throw err ;
62
58
}
63
59
}
64
60
}
65
61
```
66
62
63
+ ## Error Handling
64
+
65
+ If ` fetch ` throws an error, it will be passed to ` _handlerError ` . This is
66
+ usually a network error or an abort error.
67
+
68
+ If the response is not in the 2xx range, a ` HTTPResponseError ` will be thrown.
69
+ This is a subclass of ` Error ` and has the following properties:
70
+
71
+ - ` message ` : The status text of the response
72
+ - ` status ` : The status code of the response
73
+ - ` response ` : The original response object
74
+
67
75
## Metrics
68
76
69
77
``` js
70
- const { HTTPClient , registerMetrics } = require (" @loke/http-client" );
71
- const promClient = require (" prom-client" );
78
+ import { HTTPClient , registerMetrics } from " @loke/http-client" ;
79
+ import promClient from " prom-client" ;
80
+
72
81
registerMetrics (promClient .register );
73
82
```
74
83
@@ -80,9 +89,12 @@ the native fetch API.
80
89
81
90
- The Response object is now from native ` fetch ` and not ` got `
82
91
- Options are now native ` fetch ` options not ` got ` options
83
- - Errors are now native ` fetch` errors not ` got` errors
92
+ - Errors are now either native ` fetch ` errors or ` HTTPResponseError ` , not ` got `
93
+ errors. Notably ` got ` errors have ` statusCode ` as a property, but the
94
+ ` HTTPResponseError ` has ` status ` as a property. All other properties are now
95
+ found under ` response `
84
96
- default ` _handlerResponse ` now returns the parsed body of the response
85
- - Timout is no longer an option, pass a ` signal` on the fetch options instead
97
+ - Timeout is no longer an option, pass a ` signal ` on the fetch options instead
86
98
- No longer supports the ` retry ` option, wrap your call to ` request ` in a retry
87
99
loop instead
88
100
- No longer supports the ` http_client_request_stage_duration_seconds ` metric
0 commit comments