Skip to content

Commit

Permalink
0.13.x fixes
Browse files Browse the repository at this point in the history
- overwrite on IExecutionContext registering
- exception on not found embedded activity
- definition and runtime exceptions
  • Loading branch information
mikolaj-milewski committed Sep 11, 2024
1 parent 408460a commit 6b119e4
Show file tree
Hide file tree
Showing 49 changed files with 385 additions and 465 deletions.
2 changes: 1 addition & 1 deletion Core/Stateflows.Common/Activities/Classes/ActivityId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public ActivityId(BehaviorId id)
{
if (id.Type != BehaviorType.Activity)
{
throw new StateflowsException("BehaviorId doesn't represent Activity");
throw new StateflowsDefinitionException("BehaviorId doesn't represent Activity");
}

Name = id.Name;
Expand Down
2 changes: 1 addition & 1 deletion Core/Stateflows.Common/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static IServiceCollection AddStateflowsClient(this IServiceCollection ser
{
buildAction.ThrowIfNull(nameof(buildAction));

if (services.IsServiceRegistered<BehaviorLocator>()) throw new StateflowsException("Stateflows client already registered");
if (services.IsServiceRegistered<BehaviorLocator>()) throw new StateflowsDefinitionException("Stateflows client already registered");

var builder = new StateflowsClientBuilder(services);

Expand Down
2 changes: 1 addition & 1 deletion Core/Stateflows.Common/Events/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public void Respond(TResponse response)
{
if (Response != null)
{
throw new StateflowsException($"Already responded to request '{Name}'");
throw new StateflowsRuntimeException($"Already responded to request '{Name}'");
}

Response = response;
Expand Down
21 changes: 21 additions & 0 deletions Core/Stateflows.Common/Exceptions/BehaviorDefinitionException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace Stateflows.Common.Exceptions
{
#pragma warning disable S3925 // "ISerializable" should be implemented correctly
public class BehaviorDefinitionException : StateflowsDefinitionException
{
public BehaviorClass BehaviorClass { get; }

public BehaviorDefinitionException(string message, BehaviorClass behaviorClass) : base(message)
{
BehaviorClass = behaviorClass;
}

public BehaviorDefinitionException(string message, BehaviorClass behaviorClass, Exception innerException) : base(message, innerException)
{
BehaviorClass = behaviorClass;
}
}
#pragma warning restore S3925 // "ISerializable" should be implemented correctly
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Stateflows.Common.Exceptions
{
#pragma warning disable S3925 // "ISerializable" should be implemented correctly
public class BehaviorInstanceException : BehaviorException
public class BehaviorInstanceException : BehaviorRuntimeException
{
public BehaviorId BehaviorId { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
namespace Stateflows.Common.Exceptions
{
#pragma warning disable S3925 // "ISerializable" should be implemented correctly
public class BehaviorException : StateflowsException
public class BehaviorRuntimeException : StateflowsRuntimeException
{
public BehaviorClass BehaviorClass { get; }

public BehaviorException(string message, BehaviorClass behaviorClass) : base(message)
public BehaviorRuntimeException(string message, BehaviorClass behaviorClass) : base(message)
{
BehaviorClass = behaviorClass;
}

public BehaviorException(string message, BehaviorClass behaviorClass, Exception innerException) : base(message, innerException)
public BehaviorRuntimeException(string message, BehaviorClass behaviorClass, Exception innerException) : base(message, innerException)
{
BehaviorClass = behaviorClass;
}
Expand Down
2 changes: 1 addition & 1 deletion Core/Stateflows.Common/Exceptions/ExecutionException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Stateflows.Common.Exceptions
{
#pragma warning disable S3925 // "ISerializable" should be implemented correctly
public class ExecutionException : StateflowsException
public class ExecutionException : StateflowsDefinitionException
{
public ExecutionException(Exception innerException) : base(string.Empty, innerException) { }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Stateflows.Common.Exceptions
{
#pragma warning disable S3925 // "ISerializable" should be implemented correctly
public class SerializationException : StateflowsException
public class SerializationException : StateflowsDefinitionException
{
public SerializationException(string message) : base(message) { }
public SerializationException(string message, Exception innerException) : base(message, innerException) { }
Expand Down
12 changes: 12 additions & 0 deletions Core/Stateflows.Common/Exceptions/StateflowsDefinitionException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace Stateflows.Common.Exceptions
{
#pragma warning disable S3925 // "ISerializable" should be implemented correctly
public class StateflowsDefinitionException : StateflowsException
{
public StateflowsDefinitionException(string message) : base(message) { }
public StateflowsDefinitionException(string message, Exception innerException) : base(message, innerException) { }
}
#pragma warning restore S3925 // "ISerializable" should be implemented correctly
}
12 changes: 12 additions & 0 deletions Core/Stateflows.Common/Exceptions/StateflowsRuntimeException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace Stateflows.Common.Exceptions
{
#pragma warning disable S3925 // "ISerializable" should be implemented correctly
public class StateflowsRuntimeException : StateflowsException
{
public StateflowsRuntimeException(string message) : base(message) { }
public StateflowsRuntimeException(string message, Exception innerException) : base(message, innerException) { }
}
#pragma warning restore S3925 // "ISerializable" should be implemented correctly
}
2 changes: 1 addition & 1 deletion Core/Stateflows.Common/Exceptions/TransportException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Stateflows.Common.Exceptions
{
#pragma warning disable S3925 // "ISerializable" should be implemented correctly
public class TransportException : StateflowsException
public class TransportException : StateflowsDefinitionException
{
public TransportException(string message) : base(message) { }
public TransportException(string message, Exception innerException) : base(message, innerException) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public StateMachineId(BehaviorId id)
{
if (id.Type != StateMachineClass.Type)
{
throw new StateflowsException("BehaviorId doesn't represent State Machine");
throw new StateflowsDefinitionException("BehaviorId doesn't represent State Machine");
}

Name = id.Name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void ValidateWith(IExecutionSequenceBuilder sequenceBuilder)

if (!found)
{
throw new StateflowsException($"Expected execution step \"{entry}\" not found");
throw new StateflowsDefinitionException($"Expected execution step \"{entry}\" not found");
}
}
}
Expand Down
15 changes: 5 additions & 10 deletions Core/Stateflows/Activities/ActivitiesDependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
using System.Diagnostics;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using Stateflows.Common;
using Stateflows.Common.Interfaces;
using Stateflows.Common.Initializer;
using Stateflows.Common.Registration.Interfaces;
using Stateflows.Activities.Engine;
using Stateflows.Activities.Context;
using Stateflows.Activities.Registration;
using Stateflows.Activities.EventHandlers;
using Stateflows.Activities.Registration.Builders;
using Stateflows.Activities.Registration.Interfaces;
using Stateflows.Common.Initializer;

namespace Stateflows.Activities
{
Expand Down Expand Up @@ -56,23 +55,19 @@ private static ActivitiesRegister EnsureActivitiesServices(this IStateflowsBuild
.AddSingleton<IActivityEventHandler, NotificationsHandler>()
.AddSingleton<IActivityEventHandler, SetGlobalValuesHandler>()
.AddTransient(provider =>
ContextHolder.ActivityContext.Value ??
ActivitiesContextHolder.ActivityContext.Value ??
throw new InvalidOperationException($"No service for type '{typeof(IActivityContext).FullName}' is available in this context.")
)
.AddTransient(provider =>
ContextHolder.NodeContext.Value ??
ActivitiesContextHolder.NodeContext.Value ??
throw new InvalidOperationException($"No service for type '{typeof(INodeContext).FullName}' is available in this context.")
)
.AddTransient(provider =>
ContextHolder.FlowContext.Value ??
ActivitiesContextHolder.FlowContext.Value ??
throw new InvalidOperationException($"No service for type '{typeof(IFlowContext).FullName}' is available in this context.")
)
.AddTransient(provider =>
ContextHolder.ExecutionContext.Value ??
throw new InvalidOperationException($"No service for type '{typeof(IExecutionContext).FullName}' is available in this context.")
)
.AddTransient(provider =>
ContextHolder.ExceptionContext.Value ??
ActivitiesContextHolder.ExceptionContext.Value ??
throw new InvalidOperationException($"No service for type '{typeof(IExceptionContext).FullName}' is available in this context.")
)
;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System.Threading;
using Stateflows.Common;
using Stateflows.Common.Context;

namespace Stateflows.Activities.Context
{
public static class ContextHolder
public static class ActivitiesContextHolder
{
public static readonly AsyncLocal<IActivityContext> ActivityContext = new AsyncLocal<IActivityContext>();
public static readonly AsyncLocal<INodeContext> NodeContext = new AsyncLocal<INodeContext>();
public static readonly AsyncLocal<IFlowContext> FlowContext = new AsyncLocal<IFlowContext>();
public static readonly AsyncLocal<IExecutionContext> ExecutionContext = new AsyncLocal<IExecutionContext>();
public static AsyncLocal<IExecutionContext> ExecutionContext => CommonContextHolder.ExecutionContext;
public static readonly AsyncLocal<IExceptionContext> ExceptionContext = new AsyncLocal<IExceptionContext>();
}
}
2 changes: 1 addition & 1 deletion Core/Stateflows/Activities/Engine/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ public async Task<InitializationStatus> DoInitializeActivityAsync(ActivityInitia
}
catch (Exception e)
{
if (e is StateflowsException)
if (e is StateflowsDefinitionException)
{
throw;
}
Expand Down
100 changes: 50 additions & 50 deletions Core/Stateflows/Activities/Engine/NodeScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ public TDefaultInitializer GetDefaultInitializer<TDefaultInitializer>(IActivityI
ContextValues.SourceStateValuesHolder.Value = null;
ContextValues.TargetStateValuesHolder.Value = null;

ContextHolder.NodeContext.Value = null;
ContextHolder.FlowContext.Value = null;
ContextHolder.ActivityContext.Value = context.Activity;
ContextHolder.ExecutionContext.Value = context;
ContextHolder.ExceptionContext.Value = null;
ActivitiesContextHolder.NodeContext.Value = null;
ActivitiesContextHolder.FlowContext.Value = null;
ActivitiesContextHolder.ActivityContext.Value = context.Activity;
ActivitiesContextHolder.ExecutionContext.Value = context;
ActivitiesContextHolder.ExceptionContext.Value = null;

var initializer = ServiceProvider.GetService<TDefaultInitializer>();

Expand All @@ -101,11 +101,11 @@ public TInitializer GetInitializer<TInitializer, TInitializationEvent>(IActivity
ContextValues.SourceStateValuesHolder.Value = null;
ContextValues.TargetStateValuesHolder.Value = null;

ContextHolder.NodeContext.Value = null;
ContextHolder.FlowContext.Value = null;
ContextHolder.ActivityContext.Value = context.Activity;
ContextHolder.ExecutionContext.Value = context;
ContextHolder.ExceptionContext.Value = null;
ActivitiesContextHolder.NodeContext.Value = null;
ActivitiesContextHolder.FlowContext.Value = null;
ActivitiesContextHolder.ActivityContext.Value = context.Activity;
ActivitiesContextHolder.ExecutionContext.Value = context;
ActivitiesContextHolder.ExceptionContext.Value = null;

var initializer = ServiceProvider.GetService<TInitializer>();

Expand All @@ -120,11 +120,11 @@ public TFinalizer GetFinalizer<TFinalizer>(IActivityActionContext context)
ContextValues.SourceStateValuesHolder.Value = null;
ContextValues.TargetStateValuesHolder.Value = null;

ContextHolder.NodeContext.Value = null;
ContextHolder.FlowContext.Value = null;
ContextHolder.ActivityContext.Value = context.Activity;
ContextHolder.ExecutionContext.Value = context;
ContextHolder.ExceptionContext.Value = null;
ActivitiesContextHolder.NodeContext.Value = null;
ActivitiesContextHolder.FlowContext.Value = null;
ActivitiesContextHolder.ActivityContext.Value = context.Activity;
ActivitiesContextHolder.ExecutionContext.Value = context;
ActivitiesContextHolder.ExceptionContext.Value = null;

var initializer = ServiceProvider.GetService<TFinalizer>();

Expand All @@ -139,11 +139,11 @@ public TAction GetAction<TAction>(IActionContext context)
ContextValues.SourceStateValuesHolder.Value = null;
ContextValues.TargetStateValuesHolder.Value = null;

ContextHolder.NodeContext.Value = context.CurrentNode;
ContextHolder.FlowContext.Value = null;
ContextHolder.ActivityContext.Value = context.Activity;
ContextHolder.ExecutionContext.Value = context;
ContextHolder.ExceptionContext.Value = null;
ActivitiesContextHolder.NodeContext.Value = context.CurrentNode;
ActivitiesContextHolder.FlowContext.Value = null;
ActivitiesContextHolder.ActivityContext.Value = context.Activity;
ActivitiesContextHolder.ExecutionContext.Value = context;
ActivitiesContextHolder.ExceptionContext.Value = null;

return ServiceProvider.GetService<TAction>();
}
Expand All @@ -157,11 +157,11 @@ public TAcceptEventAction GetAcceptEventAction<TEvent, TAcceptEventAction>(IAcce
ContextValues.SourceStateValuesHolder.Value = null;
ContextValues.TargetStateValuesHolder.Value = null;

ContextHolder.NodeContext.Value = context.CurrentNode;
ContextHolder.FlowContext.Value = null;
ContextHolder.ActivityContext.Value = context.Activity;
ContextHolder.ExecutionContext.Value = context;
ContextHolder.ExceptionContext.Value = null;
ActivitiesContextHolder.NodeContext.Value = context.CurrentNode;
ActivitiesContextHolder.FlowContext.Value = null;
ActivitiesContextHolder.ActivityContext.Value = context.Activity;
ActivitiesContextHolder.ExecutionContext.Value = context;
ActivitiesContextHolder.ExceptionContext.Value = null;

return ServiceProvider.GetService<TAcceptEventAction>();
}
Expand All @@ -174,11 +174,11 @@ public TTimeEventAction GetTimeEventAction<TTimeEventAction>(IActionContext cont
ContextValues.SourceStateValuesHolder.Value = null;
ContextValues.TargetStateValuesHolder.Value = null;

ContextHolder.NodeContext.Value = context.CurrentNode;
ContextHolder.FlowContext.Value = null;
ContextHolder.ActivityContext.Value = context.Activity;
ContextHolder.ExecutionContext.Value = context;
ContextHolder.ExceptionContext.Value = null;
ActivitiesContextHolder.NodeContext.Value = context.CurrentNode;
ActivitiesContextHolder.FlowContext.Value = null;
ActivitiesContextHolder.ActivityContext.Value = context.Activity;
ActivitiesContextHolder.ExecutionContext.Value = context;
ActivitiesContextHolder.ExceptionContext.Value = null;

return ServiceProvider.GetService<TTimeEventAction>();
}
Expand All @@ -192,11 +192,11 @@ public TSendEventAction GetSendEventAction<TEvent, TSendEventAction>(IActionCont
ContextValues.SourceStateValuesHolder.Value = null;
ContextValues.TargetStateValuesHolder.Value = null;

ContextHolder.NodeContext.Value = context.CurrentNode;
ContextHolder.FlowContext.Value = null;
ContextHolder.ActivityContext.Value = context.Activity;
ContextHolder.ExecutionContext.Value = context;
ContextHolder.ExceptionContext.Value = null;
ActivitiesContextHolder.NodeContext.Value = context.CurrentNode;
ActivitiesContextHolder.FlowContext.Value = null;
ActivitiesContextHolder.ActivityContext.Value = context.Activity;
ActivitiesContextHolder.ExecutionContext.Value = context;
ActivitiesContextHolder.ExceptionContext.Value = null;

return ServiceProvider.GetService<TSendEventAction>();
}
Expand All @@ -209,11 +209,11 @@ public TStructuredActivity GetStructuredActivity<TStructuredActivity>(IActionCon
ContextValues.SourceStateValuesHolder.Value = null;
ContextValues.TargetStateValuesHolder.Value = null;

ContextHolder.NodeContext.Value = context.CurrentNode;
ContextHolder.FlowContext.Value = null;
ContextHolder.ActivityContext.Value = context.Activity;
ContextHolder.ExecutionContext.Value = context;
ContextHolder.ExceptionContext.Value = null;
ActivitiesContextHolder.NodeContext.Value = context.CurrentNode;
ActivitiesContextHolder.FlowContext.Value = null;
ActivitiesContextHolder.ActivityContext.Value = context.Activity;
ActivitiesContextHolder.ExecutionContext.Value = context;
ActivitiesContextHolder.ExceptionContext.Value = null;

return ServiceProvider.GetService<TStructuredActivity>();
}
Expand All @@ -227,11 +227,11 @@ public TExceptionHandler GetExceptionHandler<TException, TExceptionHandler>(IExc
ContextValues.SourceStateValuesHolder.Value = null;
ContextValues.TargetStateValuesHolder.Value = null;

ContextHolder.NodeContext.Value = context.CurrentNode;
ContextHolder.FlowContext.Value = null;
ContextHolder.ActivityContext.Value = context.Activity;
ContextHolder.ExecutionContext.Value = context;
ContextHolder.ExceptionContext.Value = context;
ActivitiesContextHolder.NodeContext.Value = context.CurrentNode;
ActivitiesContextHolder.FlowContext.Value = null;
ActivitiesContextHolder.ActivityContext.Value = context.Activity;
ActivitiesContextHolder.ExecutionContext.Value = context;
ActivitiesContextHolder.ExceptionContext.Value = context;

return ServiceProvider.GetService<TExceptionHandler>();
}
Expand All @@ -244,11 +244,11 @@ public TFlow GetFlow<TFlow>(IActivityFlowContext context)
ContextValues.SourceStateValuesHolder.Value = null;
ContextValues.TargetStateValuesHolder.Value = null;

ContextHolder.NodeContext.Value = null;
ContextHolder.FlowContext.Value = context;
ContextHolder.ActivityContext.Value = context.Activity;
ContextHolder.ExecutionContext.Value = context;
ContextHolder.ExceptionContext.Value = null;
ActivitiesContextHolder.NodeContext.Value = null;
ActivitiesContextHolder.FlowContext.Value = context;
ActivitiesContextHolder.ActivityContext.Value = context.Activity;
ActivitiesContextHolder.ExecutionContext.Value = context;
ActivitiesContextHolder.ExceptionContext.Value = null;

return ServiceProvider.GetService<TFlow>();
}
Expand Down
Loading

0 comments on commit 6b119e4

Please sign in to comment.