Skip to content

Commit 535884c

Browse files
committed
docs: document error handling in the README
Explain how to read the message, HTTP status and body from a raised Typesense::Error, which class each status code maps to, and that network errors are re-raised as-is once retries run out.
1 parent 58003b0 commit 535884c

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@ Here are some examples with inline comments that walk you through how to use the
2929

3030
Tests are also a good place to know how the the library works internally: [spec](spec)
3131

32+
### Error handling
33+
34+
Error responses from Typesense raise a subclass of `Typesense::Error`. The exception's `message` is the `message` from the API response, and the underlying Faraday response is available as `e.data[:response]` if you need the HTTP status, headers, or raw body:
35+
36+
```ruby
37+
begin
38+
client.collections['unknown'].retrieve
39+
rescue Typesense::Error::ObjectNotFound => e
40+
e.message # => "Not Found"
41+
e.data[:response].status # => 404
42+
e.data[:response].body # => raw response body
43+
end
44+
```
45+
46+
Status codes map to these classes: `RequestMalformed` (400), `RequestUnauthorized` (401), `ObjectNotFound` (404), `ObjectAlreadyExists` (409), `ObjectUnprocessable` (422), `ServerError` (5xx), `HTTPStatus0Error` (status 0), and `HTTPError` (any other non-2xx status).
47+
48+
5xx responses and network errors are retried first. Once retries run out, the last error is re-raised, so a network failure surfaces as the original Faraday or `Errno` exception rather than a `Typesense::Error`.
49+
3250
### Keep-alive connections
3351

3452
By default, the client opens a fresh HTTP connection (and TLS handshake) for every request. For high-traffic applications this can dominate request latency. Setting `keep_alive_connections: true` enables persistent connections via the `:net_http_persistent` Faraday adapter:

0 commit comments

Comments
 (0)