observe method

Future<Either<Failure, T>> observe({
  1. required FutureOr<Either<Failure, T>> task(
    1. MessageLog? messageLog
    ),
  2. CodenicLogger? logger,
  3. 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:

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,
      ),
    );
  }
}