Skip to content

Commit 2924019

Browse files
committed
fixs for filters
1 parent aaccacc commit 2924019

13 files changed

Lines changed: 261 additions & 106 deletions

File tree

Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
<RepositoryUrl>https://github.com/managedcode/Communication</RepositoryUrl>
2727
<PackageProjectUrl>https://github.com/managedcode/Communication</PackageProjectUrl>
2828
<Product>Managed Code - Communication</Product>
29-
<Version>10.0.1</Version>
30-
<PackageVersion>10.0.1</PackageVersion>
29+
<Version>10.0.2</Version>
30+
<PackageVersion>10.0.2</PackageVersion>
3131

3232
</PropertyGroup>
3333
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">

ManagedCode.Communication.Benchmark/ManagedCode.Communication.Benchmark.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="BenchmarkDotNet" Version="0.15.6" />
11+
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
1212
</ItemGroup>
1313

1414
<ItemGroup>

ManagedCode.Communication.Extensions/ManagedCode.Communication.Extensions.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<ItemGroup>
1717
<FrameworkReference Include="Microsoft.AspNetCore.App" />
1818
<ProjectReference Include="..\ManagedCode.Communication.AspNetCore\ManagedCode.Communication.AspNetCore.csproj" />
19-
<PackageReference Include="Polly" Version="8.6.4" />
19+
<PackageReference Include="Polly" Version="8.6.6" />
2020
</ItemGroup>
2121

2222
</Project>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Net;
3+
using System.Reflection;
4+
using System.Threading.Tasks;
5+
using ManagedCode.Communication.Helpers;
6+
using ManagedCode.Communication.Results;
7+
using Orleans;
8+
9+
namespace ManagedCode.Communication.Filters;
10+
11+
internal static class CommunicationGrainCallResultFactory
12+
{
13+
private static readonly MethodInfo CreateFailureGenericMethod =
14+
typeof(CommunicationGrainCallResultFactory).GetMethod(
15+
nameof(CreateFailureGeneric),
16+
BindingFlags.NonPublic | BindingFlags.Static)
17+
?? throw new MissingMethodException(nameof(CommunicationGrainCallResultFactory), nameof(CreateFailureGeneric));
18+
19+
public static bool TrySetFailure(IGrainCallContext context, Exception exception)
20+
{
21+
var resultType = GetCommunicationResultType(context.InterfaceMethod.ReturnType);
22+
if (resultType is null)
23+
{
24+
return false;
25+
}
26+
27+
var statusCode = OrleansHttpStatusCodeHelper.GetStatusCodeForException(exception);
28+
context.Result = CreateFailure(resultType, exception, statusCode);
29+
return true;
30+
}
31+
32+
private static Type? GetCommunicationResultType(Type returnType)
33+
{
34+
if (!returnType.IsGenericType)
35+
{
36+
return null;
37+
}
38+
39+
var genericDefinition = returnType.GetGenericTypeDefinition();
40+
if (genericDefinition != typeof(Task<>) && genericDefinition != typeof(ValueTask<>))
41+
{
42+
return null;
43+
}
44+
45+
var resultType = returnType.GenericTypeArguments[0];
46+
if (!typeof(IResult).IsAssignableFrom(resultType))
47+
{
48+
return null;
49+
}
50+
51+
var resultFactoryType = typeof(IResultFactory<>).MakeGenericType(resultType);
52+
return resultFactoryType.IsAssignableFrom(resultType) ? resultType : null;
53+
}
54+
55+
private static object CreateFailure(Type resultType, Exception exception, HttpStatusCode statusCode)
56+
{
57+
var genericMethod = CreateFailureGenericMethod.MakeGenericMethod(resultType);
58+
return genericMethod.Invoke(obj: null, [exception, statusCode])
59+
?? throw new InvalidOperationException(resultType.FullName);
60+
}
61+
62+
private static TSelf CreateFailureGeneric<TSelf>(Exception exception, HttpStatusCode statusCode)
63+
where TSelf : struct, IResultFactory<TSelf>
64+
{
65+
return TSelf.Fail(exception, statusCode);
66+
}
67+
}
Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
using System;
2-
using System.Globalization;
3-
using System.Net;
4-
using System.Reflection;
52
using System.Threading.Tasks;
6-
using ManagedCode.Communication.Helpers;
73
using Orleans;
84

95
namespace ManagedCode.Communication.Filters;
@@ -18,42 +14,12 @@ public async Task Invoke(IIncomingGrainCallContext context)
1814
}
1915
catch (Exception exception)
2016
{
21-
var returnType = context.InterfaceMethod.ReturnType;
22-
23-
if (returnType.IsGenericType)
17+
if (CommunicationGrainCallResultFactory.TrySetFailure(context, exception))
2418
{
25-
var genericDef = returnType.GetGenericTypeDefinition();
26-
if (genericDef == typeof(Task<>) || genericDef == typeof(ValueTask<>))
27-
{
28-
var taskResultType = returnType.GenericTypeArguments[0];
29-
30-
if (typeof(IResult).IsAssignableFrom(taskResultType))
31-
{
32-
var statusCode = GetOrleansStatusCode(exception);
33-
34-
if (taskResultType == typeof(Result))
35-
{
36-
context.Result = Result.Fail(exception, statusCode);
37-
}
38-
else
39-
{
40-
// Result<T> - use Activator with an internal constructor
41-
var resultInstance = Activator.CreateInstance(taskResultType, BindingFlags.NonPublic | BindingFlags.Instance, null,
42-
[exception], CultureInfo.CurrentCulture);
43-
context.Result = resultInstance;
44-
}
45-
46-
return;
47-
}
48-
}
19+
return;
4920
}
5021

5122
throw;
5223
}
5324
}
54-
55-
private static HttpStatusCode GetOrleansStatusCode(Exception exception)
56-
{
57-
return OrleansHttpStatusCodeHelper.GetStatusCodeForException(exception);
58-
}
59-
}
25+
}
Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
using System;
2-
using System.Globalization;
3-
using System.Net;
4-
using System.Reflection;
52
using System.Threading.Tasks;
6-
using ManagedCode.Communication.Helpers;
73
using Orleans;
84

95
namespace ManagedCode.Communication.Filters;
@@ -18,42 +14,12 @@ public async Task Invoke(IOutgoingGrainCallContext context)
1814
}
1915
catch (Exception exception)
2016
{
21-
var returnType = context.InterfaceMethod.ReturnType;
22-
23-
if (returnType.IsGenericType)
17+
if (CommunicationGrainCallResultFactory.TrySetFailure(context, exception))
2418
{
25-
var genericDef = returnType.GetGenericTypeDefinition();
26-
if (genericDef == typeof(Task<>) || genericDef == typeof(ValueTask<>))
27-
{
28-
var taskResultType = returnType.GenericTypeArguments[0];
29-
30-
if (typeof(IResult).IsAssignableFrom(taskResultType))
31-
{
32-
var statusCode = GetOrleansStatusCode(exception);
33-
34-
if (taskResultType == typeof(Result))
35-
{
36-
context.Result = Result.Fail(exception, statusCode);
37-
}
38-
else
39-
{
40-
// Result<T> - use Activator with an internal constructor
41-
var resultInstance = Activator.CreateInstance(taskResultType, BindingFlags.NonPublic | BindingFlags.Instance, null,
42-
[exception], CultureInfo.CurrentCulture);
43-
context.Result = resultInstance;
44-
}
45-
46-
return;
47-
}
48-
}
19+
return;
4920
}
5021

5122
throw;
5223
}
5324
}
54-
55-
private static HttpStatusCode GetOrleansStatusCode(Exception exception)
56-
{
57-
return OrleansHttpStatusCodeHelper.GetStatusCodeForException(exception);
58-
}
59-
}
25+
}

ManagedCode.Communication.Orleans/ManagedCode.Communication.Orleans.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
</PropertyGroup>
1616

1717
<ItemGroup>
18-
<PackageReference Include="Microsoft.Orleans.Sdk" Version="9.2.1"/>
19-
<PackageReference Include="Microsoft.Orleans.Runtime" Version="9.2.1"/>
20-
<PackageReference Include="Microsoft.Orleans.Serialization.Abstractions" Version="9.2.1"/>
18+
<PackageReference Include="Microsoft.Orleans.Sdk" Version="10.1.0" />
19+
<PackageReference Include="Microsoft.Orleans.Runtime" Version="10.1.0" />
20+
<PackageReference Include="Microsoft.Orleans.Serialization.Abstractions" Version="10.1.0" />
2121
</ItemGroup>
2222

2323
<ItemGroup>
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
using System.Threading.Tasks;
2+
using ManagedCode.Communication.CollectionResultT;
23
using ManagedCode.Communication.Tests.Common.TestApp.Models;
34
using Orleans;
45

56
namespace ManagedCode.Communication.Tests.Common.TestApp.Grains;
67

78
public interface ITestGrain : IGrainWithIntegerKey
89
{
10+
Task TestPlainTaskError();
11+
Task<int> TestPlainTaskIntError();
12+
913
Task<Result> TestResult();
1014
Task<Result<int>> TestResultInt();
1115

1216
Task<Result> TestResultError();
1317
Task<Result<int>> TestResultIntError();
18+
Task<Result<int>> TestResultIntInvalidOperationError();
19+
Task<CollectionResult<int>> TestCollectionResultIntError();
20+
21+
ValueTask<int> TestPlainValueTaskIntError();
1422

1523
ValueTask<Result> TestValueTaskResult();
1624
ValueTask<Result<string>> TestValueTaskResultString();
@@ -19,4 +27,6 @@ public interface ITestGrain : IGrainWithIntegerKey
1927
ValueTask<Result> TestValueTaskResultError();
2028
ValueTask<Result<string>> TestValueTaskResultStringError();
2129
ValueTask<Result<ComplexTestModel>> TestValueTaskResultComplexObjectError();
22-
}
30+
ValueTask<CollectionResult<string>> TestValueTaskCollectionResultStringError();
31+
ValueTask<CollectionResult<string>> TestValueTaskCollectionResultStringUnauthorizedError();
32+
}

ManagedCode.Communication.Tests/Common/TestApp/Grains/TestGrain.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Threading.Tasks;
4+
using ManagedCode.Communication.CollectionResultT;
45
using ManagedCode.Communication.Tests.Common.TestApp.Models;
56
using Orleans;
67

78
namespace ManagedCode.Communication.Tests.Common.TestApp.Grains;
89

910
public class TestGrain : Grain, ITestGrain
1011
{
12+
public Task TestPlainTaskError()
13+
{
14+
throw new InvalidOperationException("plain task error");
15+
}
16+
17+
public Task<int> TestPlainTaskIntError()
18+
{
19+
throw new InvalidOperationException("plain task int error");
20+
}
21+
1122
public Task<Result> TestResult()
1223
{
1324
return Result.Succeed()
@@ -30,6 +41,21 @@ public Task<Result<int>> TestResultIntError()
3041
throw new Exception("result int error");
3142
}
3243

44+
public Task<Result<int>> TestResultIntInvalidOperationError()
45+
{
46+
throw new InvalidOperationException("result int invalid operation error");
47+
}
48+
49+
public Task<CollectionResult<int>> TestCollectionResultIntError()
50+
{
51+
throw new Exception("collection result int error");
52+
}
53+
54+
public ValueTask<int> TestPlainValueTaskIntError()
55+
{
56+
throw new InvalidOperationException("plain valuetask int error");
57+
}
58+
3359
public ValueTask<Result> TestValueTaskResult()
3460
{
3561
return ValueTask.FromResult(Result.Succeed());
@@ -78,4 +104,14 @@ public ValueTask<Result<ComplexTestModel>> TestValueTaskResultComplexObjectError
78104
{
79105
throw new Exception("valuetask result complex object error");
80106
}
81-
}
107+
108+
public ValueTask<CollectionResult<string>> TestValueTaskCollectionResultStringError()
109+
{
110+
throw new Exception("valuetask collection result string error");
111+
}
112+
113+
public ValueTask<CollectionResult<string>> TestValueTaskCollectionResultStringUnauthorizedError()
114+
{
115+
throw new UnauthorizedAccessException("valuetask collection result string unauthorized error");
116+
}
117+
}

ManagedCode.Communication.Tests/ManagedCode.Communication.Tests.csproj

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@
2222
</None>
2323
</ItemGroup>
2424
<ItemGroup>
25-
<PackageReference Include="Microsoft.Orleans.TestingHost" Version="9.2.1"/>
25+
<PackageReference Include="Microsoft.Orleans.TestingHost" Version="10.1.0" />
2626
<PackageReference Include="Shouldly" Version="4.3.0" />
27-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
28-
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="10.0.0" />
29-
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="10.0.0" />
30-
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="10.0.0" />
27+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.6.0" />
28+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="10.0.8" />
29+
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="10.0.8" />
30+
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="10.0.8" />
3131
<PackageReference Include="xunit" Version="2.9.3"/>
3232
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
3333
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3434
<PrivateAssets>all</PrivateAssets>
3535
</PackageReference>
36-
<PackageReference Include="coverlet.collector" Version="6.0.4">
36+
<PackageReference Include="coverlet.collector" Version="10.0.1">
3737
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3838
<PrivateAssets>all</PrivateAssets>
3939
</PackageReference>
40-
<PackageReference Include="coverlet.msbuild" Version="6.0.4">
40+
<PackageReference Include="coverlet.msbuild" Version="10.0.1">
4141
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
4242
<PrivateAssets>all</PrivateAssets>
4343
</PackageReference>

0 commit comments

Comments
 (0)