middleware method

Handler<FutureOr<R>, A> middleware(
  1. Handler<FutureOr<R>, A> next
)

Middleware method that produces the handler that runs these hooks. Adheres to the Middleware signature.

Implementation

Handler<FutureOr<R>, A> middleware(final Handler<FutureOr<R>, A> next) {
  return (final A arg) async {
    final useArg = await onRun(arg);
    try {
      final R response;
      try {
        response = await next(useArg);
      } catch (error, stackTrace) {
        return await onError(error, stackTrace, useArg);
      }
      return await onReturn(useArg, response);
    } finally {
      try {
        await onFinally(useArg);
      } catch (_) {}
    }
  };
}