Skip to content

Commit

Permalink
Improve README on API error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Rendez committed Jun 2, 2023
1 parent 38419ae commit ec44ced
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions packages/js-client-rest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Run the Qdrant Docker container:
docker run -p 6333:6333 qdrant/qdrant
```

## Instantiate a client
### Instantiate a client

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

## Make requests
### Make requests

Using one of the available facade methods:

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

### Typed Error Handling

A non-ok fetch response throws a generic `ApiError`

But an Openapi document can declare a different response type for each status code, or a default error response type.

These can be accessed via a discriminated union on status, as in code snippet below:

```ts
const findPetsByStatus = fetcher.path('/pet/findByStatus').method('get').create();
const addPet = fetcher.path('/pet').method('post').create();

try {
const collection = await client.getCollection('bom-ada-002');
// ...
} catch (e) {
// check which operation threw the exception
if (e instanceof client.getCollection.Error) {
// get discriminated union error { status, data }
const error = e.getActualType();
// sort case's logic
if (error.status === 400) {
error.data.status.error; // only available for a 4xx responses
} else if (error.status === 500) {
error.data.status.error; // only available for a 500 response
} else {
error.data.result;
// ...
}
}
}
```

## Support

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)).
Expand Down

0 comments on commit ec44ced

Please sign in to comment.