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.

python
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:

What an error carries
FieldWhat it holds
messageA sentence that says what went wrong.
codeA short name for the problem, such as invalid_api_key.
typeA wider group, such as invalid_request_error.
http_statusThe HTTP status number.
request_idThe id of the request. Send this to support.
paramFor 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.

The kinds of error
ExceptionStatusUsual cause
AuthenticationError401The key is missing, wrong or revoked.
ForbiddenError403The key is fine, but you can't do this. For example, your email isn't verified.
NotFoundError404The thing doesn't exist, or belongs to someone else.
ValidationError400, 422The request has a bad value. param names the field.
ConflictError409The request clashes with the current state, such as canceling a run that is already running.
RateLimitError429You sent too many requests.
APIServerError500 and upA problem on the ThrustLab side.
APIErroranything elseFor 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. The feature field names it, and the message says what to use now. Nothing was sent.
  • AmbiguousComponentError: find matched more than one part. candidates lists 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:

python
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:
        raise

Ask 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.