Skip to content

Commit ec44ced

Browse files
committed
Improve README on API error handling
1 parent 38419ae commit ec44ced

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

packages/js-client-rest/README.md

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Run the Qdrant Docker container:
2020
docker run -p 6333:6333 qdrant/qdrant
2121
```
2222

23-
## Instantiate a client
23+
### Instantiate a client
2424

2525
```ts
2626
import {QdrantClient} from '@qdrant/js-client-rest';
@@ -30,7 +30,7 @@ const client = new QdrantClient({host: '127.0.0.1', port: 6333});
3030
const client = new QdrantClient({url: 'http://127.0.0.1:6333'});
3131
```
3232

33-
## Make requests
33+
### Make requests
3434

3535
Using one of the available facade methods:
3636

@@ -49,6 +49,39 @@ Or directly using an endpoint from the API:
4949
await client.api('collections').getCollections();
5050
```
5151

52+
### Typed Error Handling
53+
54+
A non-ok fetch response throws a generic `ApiError`
55+
56+
But an Openapi document can declare a different response type for each status code, or a default error response type.
57+
58+
These can be accessed via a discriminated union on status, as in code snippet below:
59+
60+
```ts
61+
const findPetsByStatus = fetcher.path('/pet/findByStatus').method('get').create();
62+
const addPet = fetcher.path('/pet').method('post').create();
63+
64+
try {
65+
const collection = await client.getCollection('bom-ada-002');
66+
// ...
67+
} catch (e) {
68+
// check which operation threw the exception
69+
if (e instanceof client.getCollection.Error) {
70+
// get discriminated union error { status, data }
71+
const error = e.getActualType();
72+
// sort case's logic
73+
if (error.status === 400) {
74+
error.data.status.error; // only available for a 4xx responses
75+
} else if (error.status === 500) {
76+
error.data.status.error; // only available for a 500 response
77+
} else {
78+
error.data.result;
79+
// ...
80+
}
81+
}
82+
}
83+
```
84+
5285
## Support
5386

5487
The REST implementation relies on the native [fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API), which is available in Deno and Node.js (starting on v18.0.0 without experimental flag). The Deno implementation [supports HTTP/2](https://deno.com/blog/every-web-api-in-deno#fetch-request-response-and-headers) whereas Node.js is still lagging on the spec and provide only HTTP 1.1 support (this is due to the fact that under the hood Node.js still relies on [undici](https://github.com/nodejs/undici)).

0 commit comments

Comments
 (0)