Exception Converter Suite
Last updated
Last updated
An ExceptionConverterSuite
allows multiple to be grouped together, so that when a task is executed and an exception is thrown, it can be converted into the appropriate object.
If an Exception
does not have an ideal ExceptionConverter
, then it will be converted to the base Failure
object using the .
This class exposes three methods:
observe
Executes an asynchronous task and returns a Failure
when an Exception
is thrown.
observeSync
Executes a synchronous task and returns a Failure
when an Exception
is thrown.
convert
Directly converts a passed Exception
into a Failure
object.
Using the observe
or observeSync
method of the ExceptionConverterSuite
, we can run a task that returns an monad containing a Left
(failed) or Right
(success) value. If any exception occurs while running the task, then a Left
object will be returned containing the respective Failure
object of the exception.
There are various approaches to converting an Exception
into a Failure
object, each with its own advantages:
Default exception converters can be configured when instantiating a ExceptionConverterSuite
object:
Exceptions converters can also be passed to the observe
, observeSync
and convert
methods when calling them:
In some cases, creating a custom exception converter may be unnecessary, particularly if an exception only occurs in a specific location. In these situations, you can manually convert an exception into a failure instead:
Automatic logging is done when any of the following events occur:
When a Left
value is returned, then a logger.warn
will be called to print the messageLog
:
When a Right
value is returned, then a logger.info
will be called to print the messageLog
:
When an exception is thrown, an ExceptionConverter
can catch that exception and decide whether to print the logs.
A task can automatically be logged by providing a MessageLog
(from the ) to the observe
and observeSync
method calls. This allows you to construct log messages by passing it data and assigning a message which will automatically be displayed at the end of the task's execution.
For more info about the logger, visit the .
Using our , this calls logger.wtf
to print the messageLog
when a SocketException
is thrown: