Skip to content

Commit

Permalink
Add new extension for cases where a required extension is missing ROB…
Browse files Browse the repository at this point in the history
…O-4220 (#343)

* Add new extension for cases where a required extension is missing ROBO-4220

In case of a missing required extensions we throw a ValidationException.
Users might want to handle those cases explicitly and they would need to
know the type of the missing extension.

To achieve this, we added ExtensionRequiredException which inherits
ValidationException and has a single property RequiredExtensionType.

* Move custom activity to test class

* Add test case for registered extensions

* Make RequiredExtensionTypeNotFound an inner exception of ValidationException

* Fix implementation of ISerializable

* Simplify ISerializable implementation
  • Loading branch information
thecristidima authored Nov 8, 2024
1 parent 1b432f4 commit 1f2f884
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
62 changes: 62 additions & 0 deletions src/Test/TestCases.Runtime/MissingRequiredExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// This file is part of Core WF which is licensed under the MIT license.
// See LICENSE file in the project root for full license information.

using Shouldly;
using System.Activities;
using System.Activities.Statements;
using System.Text;
using Xunit;

namespace TestCases.Runtime
{
public class MissingRequiredExtensionTests
{
[Theory]
[InlineData(true)]
[InlineData(false)]
public void RunActivityWithRequiredExtension(bool registerExtension)
{
var sequence = new Sequence
{
Activities = { new ActivityWithRequiredExtension(registerExtension) }
};

var exception = Record.Exception(new WorkflowApplication(sequence).Run);

if (registerExtension)
{
exception.ShouldBeNull();
}
else
{
exception.ShouldBeOfType<ValidationException>();
exception.InnerException.ShouldBeOfType<ExtensionRequiredException>();
var expectedName = typeof(StringBuilder).FullName;
((ExtensionRequiredException)exception.InnerException).RequiredExtensionTypeFullName.ShouldBe(expectedName);
}
}

private class ActivityWithRequiredExtension : NativeActivity
{
private readonly bool _registerExtension;

public ActivityWithRequiredExtension(bool registerExtension)
{
_registerExtension = registerExtension;
}

protected override void CacheMetadata(NativeActivityMetadata metadata)
{
metadata.RequireExtension<StringBuilder>();
if (_registerExtension)
{
metadata.AddDefaultExtensionProvider(() => new StringBuilder());
}
}

protected override void Execute(NativeActivityContext context)
{
}
}
}
}
35 changes: 35 additions & 0 deletions src/UiPath.Workflow.Runtime/ExtensionRequiredException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// This file is part of Core WF which is licensed under the MIT license.
// See LICENSE file in the project root for full license information.

namespace System.Activities;

[Serializable]
public class ExtensionRequiredException : Exception
{
private const string RequiredExtensionTypeName = "requiredExtensionType";

public string RequiredExtensionTypeFullName => Data[RequiredExtensionTypeName] as string;

public ExtensionRequiredException(Type requiredType)
: base()
{
Data.Add(RequiredExtensionTypeName, requiredType.FullName);
}

public ExtensionRequiredException(Type requiredType, string message)
: base(message)
{
Data.Add(RequiredExtensionTypeName, requiredType.FullName);
}

public ExtensionRequiredException(Type requiredType, string message, Exception innerException)
: base(message, innerException)
{
Data.Add(RequiredExtensionTypeName, requiredType.FullName);
}

public ExtensionRequiredException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ internal WorkflowInstanceExtensionCollection(Activity workflowDefinition, Workfl
{
if (!TypeHelper.ContainsCompatibleType(allExtensionTypes, requiredType))
{
throw FxTrace.Exception.AsError(new ValidationException(SR.RequiredExtensionTypeNotFound(requiredType.ToString())));
throw FxTrace.Exception.AsError(new ValidationException(
SR.RequiredExtensionTypeNotFound(requiredType.ToString()),
new ExtensionRequiredException(requiredType)));
}
}
}
Expand Down

0 comments on commit 1f2f884

Please sign in to comment.