-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8be0607
commit 660b902
Showing
4 changed files
with
79 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Orleans; | ||
using Orleans.Serialization.Invocation; | ||
|
||
namespace AElf.ExceptionHandler; | ||
|
||
public class AttributeCallFilter : IIncomingGrainCallFilter | ||
{ | ||
public async Task Invoke(IIncomingGrainCallContext context) | ||
{ | ||
if (context.ImplementationMethod.IsDefined(typeof(ExceptionHandlerAttribute), true)) | ||
{ | ||
var exceptionHandler = context.TargetContext.ActivationServices.GetService<ExceptionHandler>(); | ||
|
||
if (exceptionHandler == null) | ||
{ | ||
throw new Exception("ExceptionHandler is not registered."); | ||
} | ||
|
||
var arguments = new object?[context.Request.GetArgumentCount()]; | ||
for (var i = 0; i < context.Request.GetArgumentCount(); ++i) | ||
{ | ||
arguments[i] = context.Request.GetArgument(i); | ||
} | ||
|
||
var methodExecutionArgs = new MethodExecutionArgs | ||
{ | ||
TargetObject = context.Grain, | ||
MethodInfo = context.ImplementationMethod, | ||
Arguments = arguments!, | ||
ReturnValue = null, | ||
Exception = null, | ||
Invocation = async () => | ||
{ | ||
await context.Invoke(); | ||
} | ||
}; | ||
|
||
await exceptionHandler.InterceptAsync(methodExecutionArgs); | ||
|
||
if (methodExecutionArgs.Exception != null) | ||
{ | ||
context.Response = new ExceptionResponse | ||
{ | ||
Exception = methodExecutionArgs.Exception | ||
}; | ||
} | ||
else | ||
{ | ||
if (methodExecutionArgs.ReturnValue != null) | ||
{ | ||
var responseType = typeof(Response<>).MakeGenericType(methodExecutionArgs.ReturnValue.GetType()); | ||
var response = Activator.CreateInstance(responseType); | ||
responseType.GetProperty("Result")?.SetValue(response, methodExecutionArgs.ReturnValue); | ||
context.Response = (Response)response!; | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
await context.Invoke(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters