Skip to content

Latest commit

 

History

History
105 lines (65 loc) · 2.72 KB

errors.rst

File metadata and controls

105 lines (65 loc) · 2.72 KB

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.

HTTP Errors

2.0

the HTTP Errors are not throws anymore.

When a resource is consuming a get, post, put, patch, delete, collection_get, and so on, the function return a Result object that have method to unwrap the resource or the associated error.

An error is a class blacksmith.HTTPError and get the status_code of the errors with a JSON payload.

Async example

errors_01.py

Sync example

errors_02.py

Note

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.

HTTP Errors Parser

To get better error handling, a parser can be passed to the Client Factory to replace the raw HTTPError received by a parsed version.

Usually, API have a consistent way to represent error in the set of route.

Async example

errors_03.py

Sync example

errors_04.py

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.

errors_timeout.py

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.

Note

While writing your own Middleware, Exceptions, such as the HTTPError must be raise to let the Circuit Breaker track them. The Result[T, E] exposed on client resources is a layer that consume the whole middleware stack response, and is not used internaly in middlewares.

Runtime Errors

During the development, blacksmith may raises different RuntimeError or TypeError while consuming unregistered resources, or typo in consumers. Those exception are sanity check with an explicit message to get the source of the error.