Handle errors and retries
Catch each Smritea error type, know which ones the SDK retries, and decide what your code does for the rest.
The SDK retries a rate limit for you and raises a typed error for every failure. Your code decides what to do with each type. Catch the specific types first, then the base error class.
What gets retried
The SDK retries only HTTP 429, the rate limit error. It never retries any other status.
The table describes what the SDK does on its own when a call gets a 429. You do not write this logic yourself.
| Behavior | What the SDK does |
|---|---|
| Number of retries | It retries up to 2 times. Set max_retries to change this number, or set it to 0 to turn retries off. |
| Wait before a retry | When the server sends a Retry-After header, the SDK waits 90 percent of that time. Otherwise it waits 1 second before the first retry and doubles the wait for each later retry. It adds or removes up to 25 percent of the wait at random. |
| Longest wait | The SDK never waits more than 30 seconds. |
| After the last retry | The SDK raises SmriteaTooManyRequestsError. |
Choose an action for each error
The table below is advice for your code. For each error type it says what your code should do after the SDK raises that error.
| Error | HTTP status | What to do |
|---|---|---|
SmriteaBadRequestError | 400 | Fix the request. Retrying the same request fails again. |
SmriteaUnauthorizedError | 401 | Check the API key and the host for your region. |
SmriteaPaymentRequiredError | 402 | Your plan quota has run out. Wait for the next period or change your plan. Do not retry. |
SmriteaForbiddenError | 403 | Check that the app ID matches the API key's app. |
SmriteaNotFoundError | 404 | Check the memory ID. |
SmriteaConflictError | 409 | Read the message and adjust the request. |
SmriteaUnprocessableError | 422 | Remove the unsupported part, such as $contains. |
SmriteaTooManyRequestsError | 429 | You hit a rate limit and the SDK retries ran out. Slow down, then try again. |
SmriteaError | 5xx, other | The failure is on the server side. Retry later with your own backoff. |
A rate limit (429) and a quota (402) are different. A rate limit clears in seconds. A quota clears when the period ends or you change plans. See Limits, quotas and credits.
Catch the errors
import time
from smritea import (
SmriteaError,
SmriteaPaymentRequiredError,
SmriteaTooManyRequestsError,
)
def search_safely(query, scope):
try:
return client.search(query, scope=scope)
except SmriteaPaymentRequiredError:
print("Quota used up. Not retrying.")
except SmriteaTooManyRequestsError as e:
time.sleep(e.retry_after or 5)
return client.search(query, scope=scope)
except SmriteaError as e:
print(e.http_status, e.code, e.request_id)
return []In Go, a typed error does not match *smritea.SmriteaError in errors.As. Check the typed errors first, as the code does.
Names differ by SDK. Python and TypeScript use max_retries and maxRetries. Go uses MaxRetries, a pointer set with smritea.Int(n). Java takes maxRetries as a constructor argument. C# uses the maxRetries parameter. In C#, the error classes end in Exception and the base class is SmriteaException. C# wraps only HTTP errors, so a network failure surfaces as a .NET exception.
Report a failure
Every error carries a request_id. Include it when you contact support, so the team can find the failed call.
Related pages
Last updated on



