1+ using Microsoft . Extensions . Hosting ;
2+ using Wolverine . ErrorHandling ;
3+ using Xunit ;
4+
5+ namespace CoreTests . Bugs ;
6+
7+ public class Bug_2023_invoke_with_discard_error_handling
8+ {
9+ [ Fact ]
10+ public async Task should_throw_the_exception_from_invoke ( )
11+ {
12+ using var host = await Host . CreateDefaultBuilder ( )
13+ . UseWolverine ( opts =>
14+ {
15+ // By default, this should NOT apply to inline executions (e.g. InvokeAsync),
16+ // and an exception inside an inline execution should simply be surfaced.
17+ // However, as of 3.13.0, this configuration causes the error not to be thrown.
18+ opts . OnAnyException ( )
19+ . Discard ( )
20+ . And ( ( runtime , context , ex ) => {
21+ // Do some application-specific error handling here...
22+ return new ValueTask ( ) ;
23+ } ) ;
24+ } ) . StartAsync ( ) ;
25+
26+ var bus = host . MessageBus ( ) ;
27+
28+ await Should . ThrowAsync < Exception > ( async ( ) =>
29+ {
30+ await bus . InvokeAsync ( new Request ( ) ) ;
31+ } ) ;
32+
33+
34+ }
35+ }
36+
37+ public class RequestHandler {
38+ /*
39+ This is the exception that should appear to the user, but it does not.
40+ This should not be caught by the Wolverine error handling because according
41+ to the documentation,
42+
43+ `When using IMessageBus.InvokeAsync() to execute a message inline,
44+ only the `Retry` and `Retry With Cooldown` error policies are applied
45+ to the execution automatically.`
46+
47+ So, the `Discard/And` error policies should NOT be applied automatically,
48+ and the error should be thrown (which was the case in 3.9.1, and got broken
49+ by a commit in 3.13.0).
50+ */
51+ public string Handle ( Request request )
52+ => throw new Exception ( @"User-facing error message that should appear to the user." ) ;
53+ }
54+
55+ public class Request { }
0 commit comments