observe method
Future<Either<Failure, T> >
observe({
- required FutureOr<
Either< task(Failure, T> >- MessageLog? messageLog
- CodenicLogger? logger,
- MessageLog? messageLog,
Executes and observes the given task
.
A messageLog
can be provided to log the result of the task
.
If no error occurs, then value T
is returned and
logger.info(messageLog)
is called.
If a Failure is returned by the task
, then logger.warn()
is
called.
If an Exception is thrown, then it is handled by the
exceptionConverters
and ExceptionConverterSuite.exceptionConverters.
If the Exception does not have an exception converter, then it will be
converted to Failure by default using the FallbackExceptionConverter
.
NOTE:
- Errors are not caught by this method considering that it is bad practice to catch an Error. See https://groups.google.com/a/dartlang.org/g/misc/c/lx9CXiV3o30/m/s5l_PwpHUGAJ.
Implementation
Future<Either<Failure, T>> observe({
required FutureOr<Either<Failure, T>> Function(MessageLog? messageLog) task,
CodenicLogger? logger,
MessageLog? messageLog,
}) async {
assert(
messageLog == null || logger != null,
'If you want to log the exception, then you must provide a logger.',
);
try {
return await task(messageLog);
} on E catch (exception, stackTrace) {
return Left(
convert(
logger: logger,
messageLog: messageLog,
exception: exception,
stackTrace: stackTrace,
),
);
}
}