Skip to content

Commit 667dcaf

Browse files
authored
Inroduce OnActorMethodFailedAsync virtual method for overriding (#1014)
Signed-off-by: Vlad Rudenko <[email protected]>
1 parent f4e02df commit 667dcaf

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

src/Dapr.Actors/Runtime/Actor.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ------------------------------------------------------------------------
1+
// ------------------------------------------------------------------------
22
// Copyright 2021 The Dapr Authors
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -101,10 +101,11 @@ internal async Task OnPostActorMethodAsyncInternal(ActorMethodContext actorMetho
101101
await this.SaveStateAsync();
102102
}
103103

104-
internal Task OnInvokeFailedAsync()
104+
internal async Task OnActorMethodFailedInternalAsync(ActorMethodContext actorMethodContext, Exception e)
105105
{
106+
await this.OnActorMethodFailedAsync(actorMethodContext, e);
106107
// Exception has been thrown by user code, reset the state in state manager.
107-
return this.ResetStateAsync();
108+
await this.ResetStateAsync();
108109
}
109110

110111
internal Task ResetStateAsync()
@@ -190,6 +191,30 @@ protected virtual Task OnPostActorMethodAsync(ActorMethodContext actorMethodCont
190191
return Task.CompletedTask;
191192
}
192193

194+
/// <summary>
195+
/// Override this method for performing any action when invoking actor method has thrown an exception.
196+
/// This method is invoked by actor runtime when invoking actor method has thrown an exception.
197+
/// </summary>
198+
/// <param name="actorMethodContext">
199+
/// An <see cref="ActorMethodContext"/> describing the method that was invoked by actor runtime prior to this method.
200+
/// </param>
201+
/// <param name="e">Exception thrown by either actor method or by OnPreActorMethodAsync/OnPostActorMethodAsync overriden methods.</param>
202+
/// <returns>
203+
/// Returns a <see cref="Task">Task</see> representing post-actor-method operation.
204+
/// </returns>
205+
/// /// <remarks>
206+
/// This method is invoked by actor runtime prior to:
207+
/// <list type="bullet">
208+
/// <item><description>Invoking an actor interface method when a client request comes.</description></item>
209+
/// <item><description>Invoking a method when a reminder fires.</description></item>
210+
/// <item><description>Invoking a timer callback when timer fires.</description></item>
211+
/// </list>
212+
/// </remarks>
213+
protected virtual Task OnActorMethodFailedAsync(ActorMethodContext actorMethodContext, Exception e)
214+
{
215+
return Task.CompletedTask;
216+
}
217+
193218
/// <summary>
194219
/// Registers a reminder with the actor.
195220
/// </summary>

src/Dapr.Actors/Runtime/ActorManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ------------------------------------------------------------------------
1+
// ------------------------------------------------------------------------
22
// Copyright 2021 The Dapr Authors
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -355,9 +355,9 @@ private async Task<T> DispatchInternalAsync<T>(ActorId actorId, ActorMethodConte
355355
// PostActivate will save the state, its not invoked when actorFunc invocation throws.
356356
await actor.OnPostActorMethodAsyncInternal(actorMethodContext);
357357
}
358-
catch (Exception)
358+
catch (Exception e)
359359
{
360-
await actor.OnInvokeFailedAsync();
360+
await actor.OnActorMethodFailedInternalAsync(actorMethodContext, e);
361361
throw;
362362
}
363363
finally

0 commit comments

Comments
 (0)