@@ -27,7 +27,9 @@ use std::path::PathBuf;
27
27
const INDENT : & str = " " ;
28
28
29
29
const REDUCER_EVENTS : & str = r#"
30
- public interface IRemoteDbContext : IDbContext<RemoteTables, RemoteReducers, SetReducerFlags, SubscriptionBuilder> { }
30
+ public interface IRemoteDbContext : IDbContext<RemoteTables, RemoteReducers, SetReducerFlags, SubscriptionBuilder> {
31
+ public event Action<ReducerEventContext, Exception>? OnUnhandledReducerError;
32
+ }
31
33
32
34
public sealed class EventContext : IEventContext, IRemoteDbContext
33
35
{
@@ -88,6 +90,13 @@ const REDUCER_EVENTS: &str = r#"
88
90
/// Get this connection's <c>ConnectionId</c>.
89
91
/// </summary>
90
92
public ConnectionId ConnectionId => conn.ConnectionId;
93
+ /// <summary>
94
+ /// Register a callback to be called when a reducer with no handler returns an error.
95
+ /// </summary>
96
+ public event Action<ReducerEventContext, Exception>? OnUnhandledReducerError {
97
+ add => Reducers.InternalOnUnhandledReducerError += value;
98
+ remove => Reducers.InternalOnUnhandledReducerError -= value;
99
+ }
91
100
92
101
internal EventContext(DbConnection conn, Event<Reducer> Event)
93
102
{
@@ -154,6 +163,13 @@ const REDUCER_EVENTS: &str = r#"
154
163
/// Get this connection's <c>ConnectionId</c>.
155
164
/// </summary>
156
165
public ConnectionId ConnectionId => conn.ConnectionId;
166
+ /// <summary>
167
+ /// Register a callback to be called when a reducer with no handler returns an error.
168
+ /// </summary>
169
+ public event Action<ReducerEventContext, Exception>? OnUnhandledReducerError {
170
+ add => Reducers.InternalOnUnhandledReducerError += value;
171
+ remove => Reducers.InternalOnUnhandledReducerError -= value;
172
+ }
157
173
158
174
internal ReducerEventContext(DbConnection conn, ReducerEvent<Reducer> reducerEvent)
159
175
{
@@ -225,6 +241,13 @@ const REDUCER_EVENTS: &str = r#"
225
241
/// Get this connection's <c>ConnectionId</c>.
226
242
/// </summary>
227
243
public ConnectionId ConnectionId => conn.ConnectionId;
244
+ /// <summary>
245
+ /// Register a callback to be called when a reducer with no handler returns an error.
246
+ /// </summary>
247
+ public event Action<ReducerEventContext, Exception>? OnUnhandledReducerError {
248
+ add => Reducers.InternalOnUnhandledReducerError += value;
249
+ remove => Reducers.InternalOnUnhandledReducerError -= value;
250
+ }
228
251
229
252
internal ErrorContext(DbConnection conn, Exception error)
230
253
{
@@ -287,6 +310,13 @@ const REDUCER_EVENTS: &str = r#"
287
310
/// Get this connection's <c>ConnectionId</c>.
288
311
/// </summary>
289
312
public ConnectionId ConnectionId => conn.ConnectionId;
313
+ /// <summary>
314
+ /// Register a callback to be called when a reducer with no handler returns an error.
315
+ /// </summary>
316
+ public event Action<ReducerEventContext, Exception>? OnUnhandledReducerError {
317
+ add => Reducers.InternalOnUnhandledReducerError += value;
318
+ remove => Reducers.InternalOnUnhandledReducerError -= value;
319
+ }
290
320
291
321
internal SubscriptionEventContext(DbConnection conn)
292
322
{
@@ -639,7 +669,19 @@ impl Lang for Csharp<'_> {
639
669
"public bool Invoke{func_name_pascal_case}(ReducerEventContext ctx, Reducer.{func_name_pascal_case} args)"
640
670
) ;
641
671
indented_block ( output, |output| {
642
- writeln ! ( output, "if (On{func_name_pascal_case} == null) return false;" ) ;
672
+ writeln ! ( output, "if (On{func_name_pascal_case} == null)" ) ;
673
+ indented_block ( output, |output| {
674
+ writeln ! ( output, "if (InternalOnUnhandledReducerError != null)" ) ;
675
+ indented_block ( output, |output| {
676
+ writeln ! ( output, "switch(ctx.Event.Status)" ) ;
677
+ indented_block ( output, |output| {
678
+ writeln ! ( output, "case Status.Failed(var reason): InternalOnUnhandledReducerError(ctx, new Exception(reason)); break;" ) ;
679
+ writeln ! ( output, "case Status.OutOfEnergy(var _): InternalOnUnhandledReducerError(ctx, new Exception(\" out of energy\" )); break;" ) ;
680
+ } ) ;
681
+ } ) ;
682
+ writeln ! ( output, "return false;" ) ;
683
+ } ) ;
684
+
643
685
writeln ! ( output, "On{func_name_pascal_case}(" ) ;
644
686
// Write out arguments one per line
645
687
{
@@ -706,6 +748,10 @@ impl Lang for Csharp<'_> {
706
748
"internal RemoteReducers(DbConnection conn, SetReducerFlags flags) : base(conn) => SetCallReducerFlags = flags;"
707
749
) ;
708
750
writeln ! ( output, "internal readonly SetReducerFlags SetCallReducerFlags;" ) ;
751
+ writeln ! (
752
+ output,
753
+ "internal event Action<ReducerEventContext, Exception>? InternalOnUnhandledReducerError;"
754
+ )
709
755
} ) ;
710
756
writeln ! ( output) ;
711
757
@@ -828,6 +874,14 @@ impl Lang for Csharp<'_> {
828
874
writeln ! ( output) ;
829
875
830
876
writeln ! ( output, "public SubscriptionBuilder SubscriptionBuilder() => new(this);" ) ;
877
+ writeln ! (
878
+ output,
879
+ "public event Action<ReducerEventContext, Exception> OnUnhandledReducerError"
880
+ ) ;
881
+ indented_block ( output, |output| {
882
+ writeln ! ( output, "add => Reducers.InternalOnUnhandledReducerError += value;" ) ;
883
+ writeln ! ( output, "remove => Reducers.InternalOnUnhandledReducerError -= value;" ) ;
884
+ } ) ;
831
885
} ) ;
832
886
833
887
vec ! [ ( "SpacetimeDBClient.g.cs" . to_owned( ) , output. into_inner( ) ) ]
0 commit comments