Skip to content

Commit 00415ce

Browse files
committed
More README
1 parent d714978 commit 00415ce

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

README.md

+23-11
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
## Usage
44

5-
```js
6-
const { HTTPClient } = require("@loke/http-client");
5+
```ts
6+
import { HTTPClient, HTTPResponseError } from "@loke/http-client";
77

88
class MyClient extends HTTPClient {
99
constructor() {
@@ -53,22 +53,31 @@ class MyClient extends HTTPClient {
5353
case 404:
5454
throw new ThingNotFoundError(body.message);
5555
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);
6157
throw err;
6258
}
6359
}
6460
}
6561
```
6662

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+
6775
## Metrics
6876

6977
```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+
7281
registerMetrics(promClient.register);
7382
```
7483

@@ -80,9 +89,12 @@ the native fetch API.
8089

8190
- The Response object is now from native `fetch` and not `got`
8291
- 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`
8496
- 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
8698
- No longer supports the `retry` option, wrap your call to `request` in a retry
8799
loop instead
88100
- No longer supports the `http_client_request_stage_duration_seconds` metric

0 commit comments

Comments
 (0)