diff --git a/packages/config/lib/src/command/command_middleware.dart b/packages/config/lib/src/command/command_middleware.dart index ac9cae7d..81bf3f27 100644 --- a/packages/config/lib/src/command/command_middleware.dart +++ b/packages/config/lib/src/command/command_middleware.dart @@ -23,6 +23,7 @@ class CommandMiddlewareComposer super.onRun, super.onReturn, super.onError, + super.onFinally, }); } @@ -33,11 +34,13 @@ abstract class Middlewares { final OnRun>? onRun, final OnReturn>? onReturn, final OnError>? onError, + final OnFinally>? onFinally, }) { return CommandMiddlewareComposer( onRun: onRun, onReturn: onReturn, onError: onError, + onFinally: onFinally, ).middleware; } @@ -81,4 +84,13 @@ abstract class Middlewares { ) { return withHooks(onError: onError); } + + /// Creates a [CommandMiddleware] with the given [onFinally] hook. + /// + /// See [MiddlewareHooks.onFinally]. + static CommandMiddleware onFinally( + final OnFinally> onFinally, + ) { + return withHooks(onFinally: onFinally); + } } diff --git a/packages/config/lib/src/middleware/middleware_hooks.dart b/packages/config/lib/src/middleware/middleware_hooks.dart index c7024037..03d3a6bb 100644 --- a/packages/config/lib/src/middleware/middleware_hooks.dart +++ b/packages/config/lib/src/middleware/middleware_hooks.dart @@ -16,13 +16,19 @@ class MiddlewareHooks { Handler, A> middleware(final Handler, A> next) { return (final A arg) async { final useArg = await onRun(arg); - final R response; try { - response = await next(useArg); - } catch (error, stackTrace) { - return await onError(error, stackTrace, useArg); + 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 (_) {} } - return await onReturn(useArg, response); }; } @@ -49,10 +55,8 @@ class MiddlewareHooks { /// Hook method that is called if the next handler throws. /// - /// Returns the same type as the next handler's return type. - /// - /// If it throws, the call chain is aborted - /// and the exception propagates to the caller. + /// If it returns a value, this handler completes normally with that value. + /// Otherwise the exception propagates to the caller. FutureOr onError( final Object error, final StackTrace stackTrace, @@ -60,12 +64,19 @@ class MiddlewareHooks { ) { Error.throwWithStackTrace(error, stackTrace); } + + /// Hook method that is called after the next handler returns or throws, + /// after the [onReturn] / [onError] hook has run. + /// + /// Errors thrown from this method are ignored. + FutureOr onFinally(final A arg) {} } typedef OnRun = FutureOr Function(A arg); typedef OnReturn = FutureOr Function(A arg, R response); typedef OnError = FutureOr Function(Object error, StackTrace stackTrace, A arg); +typedef OnFinally = FutureOr Function(A arg); /// Helper class for easy composition with hook functions. /// @@ -82,28 +93,29 @@ class MiddlewareComposer extends MiddlewareHooks { final OnRun? _onRun; final OnReturn? _onReturn; final OnError? _onError; + final OnFinally? _onFinally; /// Compose a middleware with one or more hook functions. const MiddlewareComposer({ final OnRun? onRun, final OnReturn? onReturn, final OnError? onError, + final OnFinally? onFinally, }) : _onRun = onRun, _onReturn = onReturn, - _onError = onError; + _onError = onError, + _onFinally = onFinally; @override FutureOr onRun(final A arg) { - final onRun = _onRun; - if (onRun == null) return arg; - return onRun(arg); + if (_onRun == null) return arg; + return _onRun(arg); } @override FutureOr onReturn(final A arg, final R response) { - final onReturn = _onReturn; - if (onReturn == null) return response; - return onReturn(arg, response); + if (_onReturn == null) return response; + return _onReturn(arg, response); } @override @@ -117,4 +129,11 @@ class MiddlewareComposer extends MiddlewareHooks { } Error.throwWithStackTrace(error, stackTrace); } + + @override + FutureOr onFinally(final A arg) { + if (_onFinally != null) { + return _onFinally(arg); + } + } } diff --git a/packages/config/test/command/command_middleware_test.dart b/packages/config/test/command/command_middleware_test.dart index 64786341..4a7b8606 100644 --- a/packages/config/test/command/command_middleware_test.dart +++ b/packages/config/test/command/command_middleware_test.dart @@ -1105,6 +1105,378 @@ void main() { }); }); + group('Given Middlewares.onFinally with a synchronous callback', () { + late List trace; + late String? result; + + setUp(() async { + trace = []; + final command = RecordingCommand( + name: 'build', + description: 'Build the app', + trace: trace, + middlewares: [ + Middlewares.onFinally((final _) { + trace.add('onFinally'); + }), + ], + ); + final runner = CommandRunner( + 'tool', + 'Tools', + subcommands: [command], + ); + result = await runner.run(['build']); + }); + + test( + 'when the command runs then the callback runs after the command', + () { + expect(trace, ['build', 'onFinally']); + }, + ); + + test('when the command runs then the command result is returned', () { + expect(result, 'build'); + }); + }); + + group('Given Middlewares.onFinally with an asynchronous callback', () { + late List trace; + late String? result; + + setUp(() async { + trace = []; + final command = RecordingCommand( + name: 'build', + description: 'Build the app', + trace: trace, + middlewares: [ + Middlewares.onFinally((final _) async { + trace.add('onFinally-start'); + await Future.delayed(Duration.zero); + trace.add('onFinally-end'); + }), + ], + ); + final runner = CommandRunner( + 'tool', + 'Tools', + subcommands: [command], + ); + result = await runner.run(['build']); + }); + + test( + 'when the command runs then the callback finishes after the command', + () { + expect(trace, ['build', 'onFinally-start', 'onFinally-end']); + }, + ); + + test('when the command runs then the command result is returned', () { + expect(result, 'build'); + }); + }); + + group( + 'Given Middlewares.onFinally with a synchronous callback ' + 'on a failing command', + () { + late List trace; + late Object? error; + + setUp(() async { + trace = []; + error = null; + final command = RecordingCommand( + name: 'build', + description: 'Build the app', + trace: trace, + body: (final _) => throw StateError('boom'), + middlewares: [ + Middlewares.onFinally((final _) { + trace.add('onFinally'); + }), + ], + ); + final runner = CommandRunner( + 'tool', + 'Tools', + subcommands: [command], + ); + try { + await runner.run(['build']); + } on StateError catch (thrown) { + error = thrown; + } + }); + + test( + 'when the command fails then the callback runs after the command', + () { + expect(trace, ['build', 'onFinally']); + }, + ); + + test('when the command fails then the exception propagates', () { + expect( + error, + isA().having( + (final error) => error.message, + 'message', + 'boom', + ), + ); + }); + }, + ); + + group( + 'Given Middlewares.onFinally with an asynchronous callback ' + 'on a failing command', + () { + late List trace; + late Object? error; + + setUp(() async { + trace = []; + error = null; + final command = RecordingCommand( + name: 'build', + description: 'Build the app', + trace: trace, + body: (final _) => throw StateError('boom'), + middlewares: [ + Middlewares.onFinally((final _) async { + trace.add('onFinally-start'); + await Future.delayed(Duration.zero); + trace.add('onFinally-end'); + }), + ], + ); + final runner = CommandRunner( + 'tool', + 'Tools', + subcommands: [command], + ); + try { + await runner.run(['build']); + } on StateError catch (thrown) { + error = thrown; + } + }); + + test( + 'when the command fails then the callback finishes after the command', + () { + expect(trace, ['build', 'onFinally-start', 'onFinally-end']); + }, + ); + + test('when the command fails then the exception propagates', () { + expect( + error, + isA().having( + (final error) => error.message, + 'message', + 'boom', + ), + ); + }); + }, + ); + + group( + 'Given Middlewares.onFinally with a synchronous callback that throws', + () { + late List trace; + late String? result; + + setUp(() async { + trace = []; + final command = RecordingCommand( + name: 'build', + description: 'Build the app', + trace: trace, + middlewares: [ + Middlewares.onFinally((final _) { + trace.add('onFinally'); + throw StateError('onFinally'); + }), + ], + ); + final runner = CommandRunner( + 'tool', + 'Tools', + subcommands: [command], + ); + result = await runner.run(['build']); + }); + + test( + 'when the command runs then the callback runs after the command', + () { + expect(trace, ['build', 'onFinally']); + }, + ); + + test('when the command runs then the command result is returned', () { + expect(result, 'build'); + }); + }, + ); + + group( + 'Given Middlewares.onFinally with an asynchronous callback that throws', + () { + late List trace; + late String? result; + + setUp(() async { + trace = []; + final command = RecordingCommand( + name: 'build', + description: 'Build the app', + trace: trace, + middlewares: [ + Middlewares.onFinally((final _) async { + trace.add('onFinally-start'); + await Future.delayed(Duration.zero); + throw StateError('onFinally'); + }), + ], + ); + final runner = CommandRunner( + 'tool', + 'Tools', + subcommands: [command], + ); + result = await runner.run(['build']); + }); + + test( + 'when the command runs then the callback runs after the command', + () { + expect(trace, ['build', 'onFinally-start']); + }, + ); + + test('when the command runs then the command result is returned', () { + expect(result, 'build'); + }); + }, + ); + + group( + 'Given Middlewares.onFinally with a synchronous callback that throws ' + 'on a failing command', + () { + late List trace; + late Object? error; + + setUp(() async { + trace = []; + error = null; + final command = RecordingCommand( + name: 'build', + description: 'Build the app', + trace: trace, + body: (final _) => throw StateError('boom'), + middlewares: [ + Middlewares.onFinally((final _) { + trace.add('onFinally'); + throw StateError('onFinally'); + }), + ], + ); + final runner = CommandRunner( + 'tool', + 'Tools', + subcommands: [command], + ); + try { + await runner.run(['build']); + } on StateError catch (thrown) { + error = thrown; + } + }); + + test( + 'when the command fails then the callback runs after the command', + () { + expect(trace, ['build', 'onFinally']); + }, + ); + + test('when the command fails then the command exception propagates', () { + expect( + error, + isA().having( + (final error) => error.message, + 'message', + 'boom', + ), + ); + }); + }, + ); + + group( + 'Given Middlewares.onFinally with an asynchronous callback that throws ' + 'on a failing command', + () { + late List trace; + late Object? error; + + setUp(() async { + trace = []; + error = null; + final command = RecordingCommand( + name: 'build', + description: 'Build the app', + trace: trace, + body: (final _) => throw StateError('boom'), + middlewares: [ + Middlewares.onFinally((final _) async { + trace.add('onFinally-start'); + await Future.delayed(Duration.zero); + throw StateError('onFinally'); + }), + ], + ); + final runner = CommandRunner( + 'tool', + 'Tools', + subcommands: [command], + ); + try { + await runner.run(['build']); + } on StateError catch (thrown) { + error = thrown; + } + }); + + test( + 'when the command fails then the callback runs after the command', + () { + expect(trace, ['build', 'onFinally-start']); + }, + ); + + test('when the command fails then the command exception propagates', () { + expect( + error, + isA().having( + (final error) => error.message, + 'message', + 'boom', + ), + ); + }); + }, + ); + group('Given Middlewares.withHooks with synchronous callbacks', () { const marker = StringOption(argName: 'marker'); @@ -1325,6 +1697,210 @@ void main() { }, ); + group( + 'Given Middlewares.withHooks with a synchronous onFinally callback', + () { + late List trace; + late String? result; + + setUp(() async { + trace = []; + final command = RecordingCommand( + name: 'build', + description: 'Build the app', + trace: trace, + middlewares: [ + Middlewares.withHooks( + onReturn: (final _, final response) { + trace.add('onReturn'); + return 'hook:$response'; + }, + onFinally: (final _) { + trace.add('onFinally'); + }, + ), + ], + ); + final runner = CommandRunner( + 'tool', + 'Tools', + subcommands: [command], + ); + result = await runner.run(['build']); + }); + + test( + 'when the command runs then onFinally runs after onReturn', + () { + expect(trace, ['build', 'onReturn', 'onFinally']); + }, + ); + + test('when the command runs then the onReturn value is returned', () { + expect(result, 'hook:build'); + }); + }, + ); + + group( + 'Given Middlewares.withHooks with an asynchronous onFinally callback', + () { + late List trace; + late String? result; + + setUp(() async { + trace = []; + final command = RecordingCommand( + name: 'build', + description: 'Build the app', + trace: trace, + middlewares: [ + Middlewares.withHooks( + onReturn: (final _, final response) async { + trace.add('onReturn-start'); + await Future.delayed(Duration.zero); + trace.add('onReturn-end'); + return 'hook:$response'; + }, + onFinally: (final _) async { + trace.add('onFinally-start'); + await Future.delayed(Duration.zero); + trace.add('onFinally-end'); + }, + ), + ], + ); + final runner = CommandRunner( + 'tool', + 'Tools', + subcommands: [command], + ); + result = await runner.run(['build']); + }); + + test( + 'when the command runs then onFinally finishes after onReturn', + () { + expect(trace, [ + 'build', + 'onReturn-start', + 'onReturn-end', + 'onFinally-start', + 'onFinally-end', + ]); + }, + ); + + test('when the command runs then the onReturn value is returned', () { + expect(result, 'hook:build'); + }); + }, + ); + + group( + 'Given Middlewares.withHooks with synchronous onError and onFinally ' + 'callbacks', + () { + late List trace; + late String? result; + + setUp(() async { + trace = []; + final command = RecordingCommand( + name: 'build', + description: 'Build the app', + trace: trace, + body: (final _) => throw StateError('boom'), + middlewares: [ + Middlewares.withHooks( + onError: (final error, final _, final _) { + trace.add('onError'); + return 'recovered:${(error as StateError).message}'; + }, + onFinally: (final _) { + trace.add('onFinally'); + }, + ), + ], + ); + final runner = CommandRunner( + 'tool', + 'Tools', + subcommands: [command], + ); + result = await runner.run(['build']); + }); + + test( + 'when the command fails then onFinally runs after onError', + () { + expect(trace, ['build', 'onError', 'onFinally']); + }, + ); + + test('when the command fails then the onError value is returned', () { + expect(result, 'recovered:boom'); + }); + }, + ); + + group( + 'Given Middlewares.withHooks with asynchronous onError and onFinally ' + 'callbacks', + () { + late List trace; + late String? result; + + setUp(() async { + trace = []; + final command = RecordingCommand( + name: 'build', + description: 'Build the app', + trace: trace, + body: (final _) => throw StateError('boom'), + middlewares: [ + Middlewares.withHooks( + onError: (final error, final _, final _) async { + trace.add('onError-start'); + await Future.delayed(Duration.zero); + trace.add('onError-end'); + return 'recovered:${(error as StateError).message}'; + }, + onFinally: (final _) async { + trace.add('onFinally-start'); + await Future.delayed(Duration.zero); + trace.add('onFinally-end'); + }, + ), + ], + ); + final runner = CommandRunner( + 'tool', + 'Tools', + subcommands: [command], + ); + result = await runner.run(['build']); + }); + + test( + 'when the command fails then onFinally finishes after onError', + () { + expect(trace, [ + 'build', + 'onError-start', + 'onError-end', + 'onFinally-start', + 'onFinally-end', + ]); + }, + ); + + test('when the command fails then the onError value is returned', () { + expect(result, 'recovered:boom'); + }); + }, + ); + group('Given an action option defined on the runner', () { late List trace; late CommandRunner runner; diff --git a/packages/config/test/middleware/middleware_hooks_test.dart b/packages/config/test/middleware/middleware_hooks_test.dart index e81e9e65..1385929c 100644 --- a/packages/config/test/middleware/middleware_hooks_test.dart +++ b/packages/config/test/middleware/middleware_hooks_test.dart @@ -342,6 +342,759 @@ void main() { }, ); }); + + group( + 'Given a MiddlewareComposer with synchronous onReturn and onFinally', + () { + late List trace; + late String result; + + setUp(() async { + trace = []; + final composer = MiddlewareComposer( + onReturn: (final arg, final response) { + trace.add('onReturn'); + return 'returned:$response'; + }, + onFinally: (final arg) { + trace.add('onFinally'); + }, + ); + final handler = composer.middleware((final arg) { + trace.add('next'); + return 'from-next'; + }); + result = await handler('original'); + }); + + test('when the handler runs then onFinally runs after onReturn', () { + expect(trace, ['next', 'onReturn', 'onFinally']); + }); + + test('when the handler runs then the onReturn value is returned', () { + expect(result, 'returned:from-next'); + }); + }, + ); + + group( + 'Given a MiddlewareComposer with a synchronous onReturn ' + 'and an asynchronous onFinally', + () { + late List trace; + late String result; + + setUp(() async { + trace = []; + final composer = MiddlewareComposer( + onReturn: (final arg, final response) { + trace.add('onReturn'); + return 'returned:$response'; + }, + onFinally: (final arg) async { + trace.add('onFinally-start'); + await Future.delayed(Duration.zero); + trace.add('onFinally-end'); + }, + ); + final handler = composer.middleware((final arg) { + trace.add('next'); + return 'from-next'; + }); + result = await handler('original'); + }); + + test( + 'when the handler runs ' + 'then onFinally runs after onReturn has completed', + () { + expect(trace, [ + 'next', + 'onReturn', + 'onFinally-start', + 'onFinally-end', + ]); + }, + ); + + test('when the handler runs then the onReturn value is returned', () { + expect(result, 'returned:from-next'); + }); + }, + ); + + group( + 'Given a MiddlewareComposer with an asynchronous onReturn ' + 'and a synchronous onFinally', + () { + late List trace; + late String result; + + setUp(() async { + trace = []; + final composer = MiddlewareComposer( + onReturn: (final arg, final response) async { + trace.add('onReturn-start'); + await Future.delayed(Duration.zero); + trace.add('onReturn-end'); + return 'returned:$response'; + }, + onFinally: (final arg) { + trace.add('onFinally'); + }, + ); + final handler = composer.middleware((final arg) { + trace.add('next'); + return 'from-next'; + }); + result = await handler('original'); + }); + + test( + 'when the handler runs ' + 'then onFinally runs after onReturn has completed', + () { + expect(trace, [ + 'next', + 'onReturn-start', + 'onReturn-end', + 'onFinally', + ]); + }, + ); + + test('when the handler runs then the onReturn value is returned', () { + expect(result, 'returned:from-next'); + }); + }, + ); + + group( + 'Given a MiddlewareComposer with asynchronous onReturn and onFinally', + () { + late List trace; + late String result; + + setUp(() async { + trace = []; + final composer = MiddlewareComposer( + onReturn: (final arg, final response) async { + trace.add('onReturn-start'); + await Future.delayed(Duration.zero); + trace.add('onReturn-end'); + return 'returned:$response'; + }, + onFinally: (final arg) async { + trace.add('onFinally-start'); + await Future.delayed(Duration.zero); + trace.add('onFinally-end'); + }, + ); + final handler = composer.middleware((final arg) { + trace.add('next'); + return 'from-next'; + }); + result = await handler('original'); + }); + + test( + 'when the handler runs ' + 'then onFinally runs after onReturn has completed', + () { + expect(trace, [ + 'next', + 'onReturn-start', + 'onReturn-end', + 'onFinally-start', + 'onFinally-end', + ]); + }, + ); + + test('when the handler runs then the onReturn value is returned', () { + expect(result, 'returned:from-next'); + }); + }, + ); + + group( + 'Given a MiddlewareComposer with synchronous onError and onFinally', + () { + late List trace; + late String result; + + setUp(() async { + trace = []; + final composer = MiddlewareComposer( + onError: (final error, final stackTrace, final arg) { + trace.add('onError'); + return 'recovered:${(error as StateError).message}'; + }, + onFinally: (final arg) { + trace.add('onFinally'); + }, + ); + final handler = composer.middleware((final arg) { + trace.add('next'); + throw StateError('boom'); + }); + result = await handler('original'); + }); + + test( + 'when the next handler throws then onFinally runs after onError', + () { + expect(trace, ['next', 'onError', 'onFinally']); + }, + ); + + test( + 'when the next handler throws then the onError value is returned', + () { + expect(result, 'recovered:boom'); + }, + ); + }, + ); + + group( + 'Given a MiddlewareComposer with a synchronous onError ' + 'and an asynchronous onFinally', + () { + late List trace; + late String result; + + setUp(() async { + trace = []; + final composer = MiddlewareComposer( + onError: (final error, final stackTrace, final arg) { + trace.add('onError'); + return 'recovered:${(error as StateError).message}'; + }, + onFinally: (final arg) async { + trace.add('onFinally-start'); + await Future.delayed(Duration.zero); + trace.add('onFinally-end'); + }, + ); + final handler = composer.middleware((final arg) { + trace.add('next'); + throw StateError('boom'); + }); + result = await handler('original'); + }); + + test( + 'when the next handler throws ' + 'then onFinally runs after onError has completed', + () { + expect(trace, [ + 'next', + 'onError', + 'onFinally-start', + 'onFinally-end', + ]); + }, + ); + + test( + 'when the next handler throws then the onError value is returned', + () { + expect(result, 'recovered:boom'); + }, + ); + }, + ); + + group( + 'Given a MiddlewareComposer with an asynchronous onError ' + 'and a synchronous onFinally', + () { + late List trace; + late String result; + + setUp(() async { + trace = []; + final composer = MiddlewareComposer( + onError: (final error, final stackTrace, final arg) async { + trace.add('onError-start'); + await Future.delayed(Duration.zero); + trace.add('onError-end'); + return 'recovered:${(error as StateError).message}'; + }, + onFinally: (final arg) { + trace.add('onFinally'); + }, + ); + final handler = composer.middleware((final arg) { + trace.add('next'); + throw StateError('boom'); + }); + result = await handler('original'); + }); + + test( + 'when the next handler throws ' + 'then onFinally runs after onError has completed', + () { + expect(trace, [ + 'next', + 'onError-start', + 'onError-end', + 'onFinally', + ]); + }, + ); + + test( + 'when the next handler throws then the onError value is returned', + () { + expect(result, 'recovered:boom'); + }, + ); + }, + ); + + group( + 'Given a MiddlewareComposer with asynchronous onError and onFinally', + () { + late List trace; + late String result; + + setUp(() async { + trace = []; + final composer = MiddlewareComposer( + onError: (final error, final stackTrace, final arg) async { + trace.add('onError-start'); + await Future.delayed(Duration.zero); + trace.add('onError-end'); + return 'recovered:${(error as StateError).message}'; + }, + onFinally: (final arg) async { + trace.add('onFinally-start'); + await Future.delayed(Duration.zero); + trace.add('onFinally-end'); + }, + ); + final handler = composer.middleware((final arg) { + trace.add('next'); + throw StateError('boom'); + }); + result = await handler('original'); + }); + + test( + 'when the next handler throws ' + 'then onFinally runs after onError has completed', + () { + expect(trace, [ + 'next', + 'onError-start', + 'onError-end', + 'onFinally-start', + 'onFinally-end', + ]); + }, + ); + + test( + 'when the next handler throws then the onError value is returned', + () { + expect(result, 'recovered:boom'); + }, + ); + }, + ); + + group( + 'Given a MiddlewareComposer whose synchronous onFinally throws ' + 'after onReturn', + () { + late List trace; + late String result; + + setUp(() async { + trace = []; + final composer = MiddlewareComposer( + onReturn: (final arg, final response) { + trace.add('onReturn'); + return 'returned:$response'; + }, + onFinally: (final arg) { + trace.add('onFinally'); + throw StateError('onFinally'); + }, + ); + final handler = composer.middleware((final arg) { + trace.add('next'); + return 'from-next'; + }); + result = await handler('original'); + }); + + test('when the handler runs then onFinally runs after onReturn', () { + expect(trace, ['next', 'onReturn', 'onFinally']); + }); + + test('when the handler runs then the onReturn value is returned', () { + expect(result, 'returned:from-next'); + }); + }, + ); + + group( + 'Given a MiddlewareComposer whose asynchronous onFinally throws ' + 'after onReturn', + () { + late List trace; + late String result; + + setUp(() async { + trace = []; + final composer = MiddlewareComposer( + onReturn: (final arg, final response) async { + trace.add('onReturn-start'); + await Future.delayed(Duration.zero); + trace.add('onReturn-end'); + return 'returned:$response'; + }, + onFinally: (final arg) async { + trace.add('onFinally-start'); + await Future.delayed(Duration.zero); + throw StateError('onFinally'); + }, + ); + final handler = composer.middleware((final arg) { + trace.add('next'); + return 'from-next'; + }); + result = await handler('original'); + }); + + test( + 'when the handler runs ' + 'then onFinally runs after onReturn has completed', + () { + expect(trace, [ + 'next', + 'onReturn-start', + 'onReturn-end', + 'onFinally-start', + ]); + }, + ); + + test('when the handler runs then the onReturn value is returned', () { + expect(result, 'returned:from-next'); + }); + }, + ); + + group( + 'Given a MiddlewareComposer whose synchronous onFinally throws ' + 'after onError recovers', + () { + late List trace; + late String result; + + setUp(() async { + trace = []; + final composer = MiddlewareComposer( + onError: (final error, final stackTrace, final arg) { + trace.add('onError'); + return 'recovered:${(error as StateError).message}'; + }, + onFinally: (final arg) { + trace.add('onFinally'); + throw StateError('onFinally'); + }, + ); + final handler = composer.middleware((final arg) { + trace.add('next'); + throw StateError('boom'); + }); + result = await handler('original'); + }); + + test( + 'when the next handler throws then onFinally runs after onError', + () { + expect(trace, ['next', 'onError', 'onFinally']); + }, + ); + + test( + 'when the next handler throws then the onError value is returned', + () { + expect(result, 'recovered:boom'); + }, + ); + }, + ); + + group( + 'Given a MiddlewareComposer whose asynchronous onFinally throws ' + 'after onError recovers', + () { + late List trace; + late String result; + + setUp(() async { + trace = []; + final composer = MiddlewareComposer( + onError: (final error, final stackTrace, final arg) async { + trace.add('onError-start'); + await Future.delayed(Duration.zero); + trace.add('onError-end'); + return 'recovered:${(error as StateError).message}'; + }, + onFinally: (final arg) async { + trace.add('onFinally-start'); + await Future.delayed(Duration.zero); + throw StateError('onFinally'); + }, + ); + final handler = composer.middleware((final arg) { + trace.add('next'); + throw StateError('boom'); + }); + result = await handler('original'); + }); + + test( + 'when the next handler throws ' + 'then onFinally runs after onError has completed', + () { + expect(trace, [ + 'next', + 'onError-start', + 'onError-end', + 'onFinally-start', + ]); + }, + ); + + test( + 'when the next handler throws then the onError value is returned', + () { + expect(result, 'recovered:boom'); + }, + ); + }, + ); + + group( + 'Given a MiddlewareComposer whose synchronous onFinally throws ' + 'after onError rethrows', + () { + late List trace; + late Object? error; + + setUp(() async { + trace = []; + final composer = MiddlewareComposer( + onError: (final error, final stackTrace, final arg) { + trace.add('onError'); + Error.throwWithStackTrace(error, stackTrace); + }, + onFinally: (final arg) { + trace.add('onFinally'); + throw StateError('onFinally'); + }, + ); + final handler = composer.middleware((final arg) { + trace.add('next'); + throw StateError('boom'); + }); + try { + await handler('original'); + error = null; + } catch (thrown) { + error = thrown; + } + }); + + test( + 'when the next handler throws then onFinally runs after onError', + () { + expect(trace, ['next', 'onError', 'onFinally']); + }, + ); + + test( + 'when the next handler throws ' + 'then the next handler exception propagates', + () { + expect(error, _stateError('boom')); + }, + ); + }, + ); + + group( + 'Given a MiddlewareComposer whose asynchronous onFinally throws ' + 'after onError rethrows', + () { + late List trace; + late Object? error; + + setUp(() async { + trace = []; + final composer = MiddlewareComposer( + onError: (final error, final stackTrace, final arg) async { + trace.add('onError-start'); + await Future.delayed(Duration.zero); + trace.add('onError-end'); + Error.throwWithStackTrace(error, stackTrace); + }, + onFinally: (final arg) async { + trace.add('onFinally-start'); + await Future.delayed(Duration.zero); + throw StateError('onFinally'); + }, + ); + final handler = composer.middleware((final arg) { + trace.add('next'); + throw StateError('boom'); + }); + try { + await handler('original'); + error = null; + } catch (thrown) { + error = thrown; + } + }); + + test( + 'when the next handler throws ' + 'then onFinally runs after onError has completed', + () { + expect(trace, [ + 'next', + 'onError-start', + 'onError-end', + 'onFinally-start', + ]); + }, + ); + + test( + 'when the next handler throws ' + 'then the next handler exception propagates', + () { + expect(error, _stateError('boom')); + }, + ); + }, + ); + + group( + 'Given a MiddlewareComposer whose synchronous onFinally throws ' + 'after onReturn throws', + () { + late List trace; + late Object? error; + + setUp(() async { + trace = []; + final composer = MiddlewareComposer( + onReturn: (final arg, final response) { + trace.add('onReturn'); + throw StateError('onReturn'); + }, + onFinally: (final arg) { + trace.add('onFinally'); + throw StateError('onFinally'); + }, + ); + final handler = composer.middleware((final arg) { + trace.add('next'); + return 'from-next'; + }); + try { + await handler('original'); + error = null; + } catch (thrown) { + error = thrown; + } + }); + + test('when the handler runs then onFinally runs after onReturn', () { + expect(trace, ['next', 'onReturn', 'onFinally']); + }); + + test( + 'when the handler runs then the onReturn exception propagates', + () { + expect(error, _stateError('onReturn')); + }, + ); + }, + ); + + group( + 'Given a MiddlewareComposer whose asynchronous onFinally throws ' + 'after onReturn throws', + () { + late List trace; + late Object? error; + + setUp(() async { + trace = []; + final composer = MiddlewareComposer( + onReturn: (final arg, final response) async { + trace.add('onReturn-start'); + await Future.delayed(Duration.zero); + trace.add('onReturn-end'); + throw StateError('onReturn'); + }, + onFinally: (final arg) async { + trace.add('onFinally-start'); + await Future.delayed(Duration.zero); + throw StateError('onFinally'); + }, + ); + final handler = composer.middleware((final arg) { + trace.add('next'); + return 'from-next'; + }); + try { + await handler('original'); + error = null; + } catch (thrown) { + error = thrown; + } + }); + + test( + 'when the handler runs ' + 'then onFinally runs after onReturn has completed', + () { + expect(trace, [ + 'next', + 'onReturn-start', + 'onReturn-end', + 'onFinally-start', + ]); + }, + ); + + test( + 'when the handler runs then the onReturn exception propagates', + () { + expect(error, _stateError('onReturn')); + }, + ); + }, + ); +} + +Matcher _stateError(final String message) { + return isA().having( + (final StateError error) => error.message, + 'message', + message, + ); } class _NullOnRun extends MiddlewareHooks {