diff --git a/src/Dapr.Actors/Runtime/Actor.cs b/src/Dapr.Actors/Runtime/Actor.cs index d7291cb58..ddfc266e9 100644 --- a/src/Dapr.Actors/Runtime/Actor.cs +++ b/src/Dapr.Actors/Runtime/Actor.cs @@ -1,4 +1,4 @@ -// ------------------------------------------------------------------------ +// ------------------------------------------------------------------------ // Copyright 2021 The Dapr Authors // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -101,10 +101,11 @@ internal async Task OnPostActorMethodAsyncInternal(ActorMethodContext actorMetho await this.SaveStateAsync(); } - internal Task OnInvokeFailedAsync() + internal async Task OnActorMethodFailedInternalAsync(ActorMethodContext actorMethodContext, Exception e) { + await this.OnActorMethodFailedAsync(actorMethodContext, e); // Exception has been thrown by user code, reset the state in state manager. - return this.ResetStateAsync(); + await this.ResetStateAsync(); } internal Task ResetStateAsync() @@ -190,6 +191,30 @@ protected virtual Task OnPostActorMethodAsync(ActorMethodContext actorMethodCont return Task.CompletedTask; } + /// + /// Override this method for performing any action when invoking actor method has thrown an exception. + /// This method is invoked by actor runtime when invoking actor method has thrown an exception. + /// + /// + /// An describing the method that was invoked by actor runtime prior to this method. + /// + /// Exception thrown by either actor method or by OnPreActorMethodAsync/OnPostActorMethodAsync overriden methods. + /// + /// Returns a Task representing post-actor-method operation. + /// + /// /// + /// This method is invoked by actor runtime prior to: + /// + /// Invoking an actor interface method when a client request comes. + /// Invoking a method when a reminder fires. + /// Invoking a timer callback when timer fires. + /// + /// + protected virtual Task OnActorMethodFailedAsync(ActorMethodContext actorMethodContext, Exception e) + { + return Task.CompletedTask; + } + /// /// Registers a reminder with the actor. /// diff --git a/src/Dapr.Actors/Runtime/ActorManager.cs b/src/Dapr.Actors/Runtime/ActorManager.cs index e7f29f5cc..6278b0ded 100644 --- a/src/Dapr.Actors/Runtime/ActorManager.cs +++ b/src/Dapr.Actors/Runtime/ActorManager.cs @@ -1,4 +1,4 @@ -// ------------------------------------------------------------------------ +// ------------------------------------------------------------------------ // Copyright 2021 The Dapr Authors // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -355,9 +355,9 @@ private async Task DispatchInternalAsync(ActorId actorId, ActorMethodConte // PostActivate will save the state, its not invoked when actorFunc invocation throws. await actor.OnPostActorMethodAsyncInternal(actorMethodContext); } - catch (Exception) + catch (Exception e) { - await actor.OnInvokeFailedAsync(); + await actor.OnActorMethodFailedInternalAsync(actorMethodContext, e); throw; } finally