Error handling
When a call fails, the library raises an exception that says what went wrong. You can catch the kind of failure you expect, and handle each one in its own way.
from thrustlab import Client
from thrustlab.exceptions import NotFoundError, RateLimitError, ValidationError
client = Client()
try:
project = client.projects.retrieve("proj_does_not_exist")
except NotFoundError as error:
print("No such project:", error.message)
except ValidationError as error:
print("Bad request:", error.code, error.param, error.message)
except RateLimitError as error:
print("Slow down. Try again in", error.retry_after, "seconds")What an error carries
Every error from the server has these fields:
| Field | What it holds |
|---|---|
message | A sentence that says what went wrong. |
code | A short name for the problem, such as invalid_api_key. |
type | A wider group, such as invalid_request_error. |
http_status | The HTTP status number. |
request_id | The id of the request. Send this to support. |
param | For a bad request, the name of the field at fault. |
A RateLimitError also has retry_after, the seconds to wait. The meaning of each code is in Errors.
The kinds of error
All of these come from thrustlab.exceptions. The first group are answers from the server, and they all inherit from APIError.
| Exception | Status | Usual cause |
|---|---|---|
AuthenticationError | 401 | The key is missing, wrong or revoked. |
ForbiddenError | 403 | The key is fine, but you can't do this. For example, your email isn't verified. |
NotFoundError | 404 | The thing doesn't exist, or belongs to someone else. |
ValidationError | 400, 422 | The request has a bad value. param names the field. |
ConflictError | 409 | The request clashes with the current state, such as canceling a run that is already running. |
RateLimitError | 429 | You sent too many requests. |
APIServerError | 500 and up | A problem on the ThrustLab side. |
APIError | anything else | For example 402, when your plan or your compute units don't cover the request. |
Catch APIError to handle all of them at once.
The rest don't come from a server answer:
NetworkError: no answer arrived, even after retries.ConfigurationError: no API key was found when you created the client.SignatureVerificationError: a webhook message failed its check.RemovedFeatureError: you used a feature that ThrustLab removed, such as FMU export. Thefeaturefield names it, and the message says what to use now. Nothing was sent.AmbiguousComponentError:findmatched more than one part.candidateslists them.
The library can also raise a plain ValueError before it sends anything. It does this when your arguments contradict each other, such as giving both state_of_charge_pct and resting_voltage_v_per_cell.
Plan and balance errors
Two 402 codes are worth knowing. upgrade_required means the feature needs the Pro plan, such as a flight simulation. credits_insufficient means you don't have enough compute units. Both raise a plain APIError, so check http_status:
from thrustlab.exceptions import APIError
# project and flight come from the page Flights
try:
client.flights.create(project_id=project["id"], **flight)
except APIError as error:
if error.http_status == 402:
print("Not covered by your plan:", error.code)
else:
raiseAsk for help
Email [email protected] with the request_id of the failed call. It lets ThrustLab find that exact request.
print(error) shows the message of a server error. repr(error) also shows the code, the status and the request id.