Dealing with errors

Note

This chapter describe common error that may happen at runtime. Some exception can happen during the registration phase, using the function blacksmith.scan() which are not par not runtime desigated runtime errors here.

Timeout

If a service is too slow, a blacksmith.HTTPTimeoutError exception will be raised to avoid a process to be locked. The default timeout is at 30 seconds but it can be configured on the client factory, and can be overriden on every http call. The default connect timeout is at 15 seconds.

from blacksmith import AsyncClientFactory, AsyncStaticDiscovery, HTTPTimeout

sd = AsyncStaticDiscovery({})

# read timeout at 5 seconds
# and connect timeout at 5 seconds
cli = AsyncClientFactory(sd, timeout=(10.0, 5.0))
# Or
cli = AsyncClientFactory(sd, timeout=HTTPTimeout(10.0, 5.0))

# All timeout at 10 seconds
cli = AsyncClientFactory(sd, timeout=10.0)
# Or
cli = AsyncClientFactory(sd, timeout=HTTPTimeout(10.0))


async def main():
    api = await cli("api")

    # user the default timeout
    resources = await api.resource.collection_get()  # noqa

    # force the timeout
    resources = await api.resource.collection_get(timeout=42.0)  # noqa

    # Or even with a connect timeout using the HTTPTimeout class
    resources = await api.resource.collection_get(  # noqa
        timeout=HTTPTimeout(42.0, 7.0)
    )

HTTP Errors

Blacksmith does not declare schema for errors.

It raised exceptions instead.

The exception raised is blacksmith.HTTPError and get the status_code of the error.

Note

Usually, a set of API share the same format for all the errors, but sometime, errors may also be html, so it is not possible to have a schema for errors.

The error is supposed to be a json document, under attribute json. If it is not the case, the content of the document will be in plain text under the key detail.

Opened Circuit Breaker

While using the Circuit Breaker Middleware, the OpenedState exception is raised from the Circuit Breaker library, when a service is detected down, and then, that circuit has been opened.