Skip to content

Commit

Permalink
UnexpectedStateException dry'ing & refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
stidsborg committed Aug 7, 2024
1 parent 9074e40 commit bd04901
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected async Task ExistingActionCanBeDeletedFromControlPanel(Task<IFunctionSt
var controlPanel = await rAction.ControlPanel(flowInstance).ShouldNotBeNullAsync();
await controlPanel.Delete();

await Should.ThrowAsync<UnexpectedFunctionState>(controlPanel.Refresh());
await Should.ThrowAsync<UnexpectedStateException>(controlPanel.Refresh());

await store.GetFunction(functionId).ShouldBeNullAsync();

Expand Down Expand Up @@ -106,7 +106,7 @@ async Task<string>(string _, Workflow workflow) =>
var controlPanel = await rFunc.ControlPanel(flowInstance).ShouldNotBeNullAsync();
await controlPanel.Delete();

await Should.ThrowAsync<UnexpectedFunctionState>(controlPanel.Refresh());
await Should.ThrowAsync<UnexpectedStateException>(controlPanel.Refresh());

await store.GetFunction(functionId).ShouldBeNullAsync();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ protected async Task ReInvocationFailsWhenTheFunctionDoesNotExist(Task<IFunction
var controlPanel2 = await rAction.ControlPanel(flowInstance).ShouldNotBeNullAsync();
await controlPanel1.Delete();

await Should.ThrowAsync<UnexpectedFunctionState>(() => controlPanel2.Restart());
await Should.ThrowAsync<UnexpectedStateException>(() => controlPanel2.Restart());

unhandledExceptionCatcher.ShouldNotHaveExceptions();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public async Task<PreparedReInvocation> PrepareForReInvocation(FlowId flowId, Re
runningFunction.Dispose();
sf = await _functionStore.GetFunction(flowId);
if (sf == null)
throw new UnexpectedFunctionState(flowId, $"Function '{flowId}' was not found");
throw UnexpectedStateException.NotFound(flowId);

await _functionStore.FailFunction(
flowId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ private async Task<PreparedReInvocation> PrepareForReInvocation(FlowId flowId, i
{
var restartedFunction = await _invocationHelper.RestartFunction(flowId, expectedEpoch);
if (restartedFunction == null)
throw new UnexpectedFunctionState(flowId, $"Function '{flowId}' did not have expected epoch '{expectedEpoch}' on re-invocation");
throw UnexpectedStateException.EpochMismatch(flowId);

return await PrepareForReInvocation(flowId, restartedFunction);
}
Expand Down Expand Up @@ -289,7 +289,7 @@ private async Task PersistResultAndEnsureSuccess(FlowId flowId, Result<TReturn>
try
{
await ScheduleRestart(flowId.Instance.Value, expectedEpoch);
} catch (UnexpectedFunctionState) {} //allow this exception - the invocation has been surpassed by other execution
} catch (UnexpectedStateException) {} //allow this exception - the invocation has been surpassed by other execution
InvocationHelper<TParam, TReturn>.EnsureSuccess(flowId, result, allowPostponedOrSuspended);
break;
}
Expand Down
9 changes: 2 additions & 7 deletions Core/Cleipnir.ResilientFunctions/CoreRuntime/LeaseUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,7 @@ private async Task Start()
if (!success)
{
_disposed = true;
_unhandledExceptionHandler.Invoke(
new UnexpectedFunctionState(
_flowId,
$"{nameof(LeaseUpdater)} failed to update lease for executing function: '{_flowId}'"
)
);
_unhandledExceptionHandler.Invoke(UnexpectedStateException.LeaseUpdateFailed(_flowId));
}
}
catch (Exception e)
Expand All @@ -82,7 +77,7 @@ private async Task Start()
_unhandledExceptionHandler.Invoke(
new FrameworkException(
_flowId.Type,
$"{nameof(LeaseUpdater)} failed while executing function: '{_flowId}'",
$"{nameof(LeaseUpdater)} failed for '{_flowId}'",
e
)
);
Expand Down
2 changes: 1 addition & 1 deletion Core/Cleipnir.ResilientFunctions/Domain/ControlPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public async Task Refresh()
{
var sf = await _invocationHelper.GetFunction(FlowId);
if (sf == null)
throw new UnexpectedFunctionState(FlowId, $"Function '{FlowId}' not found");
throw UnexpectedStateException.NotFound(FlowId);

Status = sf.Status;
Epoch = sf.Epoch;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using Cleipnir.ResilientFunctions.CoreRuntime;

namespace Cleipnir.ResilientFunctions.Domain.Exceptions;

public sealed class UnexpectedStateException : FlowTypeException
{
public FlowId FlowId { get; }

public UnexpectedStateException(FlowId flowId, string message)
: base(flowId.Type, message) => FlowId = flowId;

public UnexpectedStateException(FlowId flowId, string message, Exception innerException)
: base(flowId.Type, message, innerException) => FlowId = flowId;

public static UnexpectedStateException NotFound(FlowId flowId) => new(flowId, $"{flowId} was not found");
public static UnexpectedStateException EpochMismatch(FlowId flowId) => new(flowId, $"{flowId} did not have expected epoch at restart");
public static UnexpectedStateException LeaseUpdateFailed(FlowId flowId) => new(flowId, $"{nameof(LeaseUpdater)} failed to update lease for '{flowId}'");
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public async Task<Finding> AppendMessage<TMessage>(TMessage message, string? ide
{
await _scheduleReInvocation(_flowId.Instance.Value, expectedEpoch: epoch);
}
catch (UnexpectedFunctionState) { }
catch (UnexpectedStateException) { }

return Finding.Found;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public async Task<InterruptCount> PullEvents(TimeSpan maxSinceLastSynced)

var interruptCount = await _functionStore.GetInterruptCount(_flowId);
if (interruptCount == null)
throw new UnexpectedFunctionState(_flowId, "Function was not found when fetching interrupt count");
throw UnexpectedStateException.NotFound(_flowId);

lock (_sync)
_interruptCount = interruptCount.Value;
Expand Down

0 comments on commit bd04901

Please sign in to comment.