Key4.Grpc.ServerExceptionInterceptor 2.3.0.11-b
Key4.Grpc.ServerExceptionInterceptor
.NET Interceptor and middleware propagating errors between gRPC services.
Uses AutoWrapper for the returned object. Uses HttpExceptions for the mapping .net exception <-> HTTP status code.
Usage
Startup / Middleware registration
- Add the middleware to the configuration in Startup.cs of the client gRPC project. With an optional parameter ShowDetails (defaults to false).
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IApiVersionDescriptionProvider provider)
{
// configurations
app.UseK4GrpcException(options => options.ShowDetails = env.IsDevelopment() || env.IsStaging());
}
- Add the interceptor to the gRPC configuration in Startup.cs of the server gRPC project.
public void ConfigureServices(IServiceCollection services)
{
// configurations
services.AddGrpc(options => options.Interceptors.Add<K4GrpcExceptionInterceptor>());
}
Example
Example of an error returned by a gRPC client when an exception is thrown at the server level.
Example of error when ShowDetails is set to true.
{
"statusCode": 403,
"isError": true,
"responseException": {
"exceptionMessage": "No permissions for user",
"details": " at key4.Agency.BackOffice.API.Controllers.EchoController.ThrowAsync() in C:\\Repos\\key4.agency.docker\\key4.agency.api\\key4.Agency.BackOffice.API\\Controllers\\EchoController.cs:line 48\n at lambda_method82(Closure , Object )\n at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\n",
"status": 403,
"type": "https://httpstatuses.com/403",
"instance": "/v1/operations",
"remoteException": {
"message": "Status(StatusCode=\"PermissionDenied\", Detail=\"No permissions for user\")",
"details": " at key4.Service.Agency.Server.ThrowService.Throw(ThrowRequest request, ServerCallContext context) in C:\\Repos\\key4.agency.docker\\key4.service.agency\\1. Presentation\\key4.Service.Agency.Server\\Services\\ThrowService.cs:line 26\n at Grpc.Shared.Server.UnaryServerMethodInvoker`3.ResolvedInterceptorInvoker(TRequest resolvedRequest, ServerCallContext resolvedContext)\n at Grpc.Shared.Server.UnaryServerMethodInvoker`3.ResolvedInterceptorInvoker(TRequest resolvedRequest, ServerCallContext resolvedContext)\n at key4.Grpc.ServerExceptionInterceptor.K4GrpcExceptionInterceptor.UnaryServerHandler[TRequest,TResponse](TRequest request, ServerCallContext context, UnaryServerMethod`2 continuation) in C:\\Repos\\key4.grpc\\key4.Grpc.ServerExceptionInterceptor\\K4GrpcExceptionInterceptor.cs:line 24"
}
}
}
Example of error when ShowDetails is set to false.
{
"statusCode": 403,
"isError": true,
"responseException": {
"exceptionMessage": "No permissions for user",
"status": 403,
"type": "https://httpstatuses.com/403",
"instance": "/v1/operations",
"remoteException": {
"message": "Status(StatusCode=\"PermissionDenied\", Detail=\"No permissions for user\")"
}
}
}
gRPC error codes
Mapping for gRPC status codes <-> HTTP status codes is as follow:
| ExceptionType | Description | HttpCode | Status gRPC |
|---|---|---|---|
| ArgumentException | Bad request error | 400 | InvalidArgument |
| InvalidOperationException | Bad request error | 400 | InvalidArgument |
| HttpBadRequestException | Bad request error | 400 | InvalidArgument |
| HttpUnauthorizedException | Unauthorized error | 401 | Unauthenticated |
| HttpForbiddenException | Forbidden error | 403 | PermissionDenied |
| HttpNotFoundException | Not found error | 404 | NotFound |
| HttpModelValidationFailed | Unprocessable Entity | 422 | FailedPrecondition |
| ValidationException | Unprocessable Entity | 422 | FailedPrecondition |
| Exception | Internal Server Error | 500 | Internal |
No packages depend on Key4.Grpc.ServerExceptionInterceptor.
.NET 5.0
- AutoWrapper.Core (>= 4.5.0)
- FluentValidation (>= 10.3.6)
- Grpc.AspNetCore (>= 2.41.0)
- Grpc.AspNetCore.Web (>= 2.41.0)
- Key4.AspNetCore.HttpExceptions (>= 6.0.0.9-b)
| Version | Downloads | Last updated |
|---|---|---|
| 3.2.0.15-b | 11 | 12/08/2022 |
| 3.1.0.14-b | 12 | 12/08/2022 |
| 3.0.0.13-b | 11 | 12/08/2022 |
| 3.0.0.12-b | 10 | 12/08/2022 |
| 2.3.0.11-b | 11 | 12/08/2022 |
| 2.3.0.10-b | 10 | 12/08/2022 |
| 2.2.0.9-b | 12 | 12/08/2022 |
| 2.1.1.8-b | 9 | 12/08/2022 |
| 2.1.0.7-b | 11 | 12/08/2022 |
| 2.1.0.6-b | 12 | 12/08/2022 |
| 2.1.0.5-b | 10 | 12/08/2022 |
| 2.0.0.4-b | 10 | 12/08/2022 |
| 2.0.0.3-b | 9 | 12/08/2022 |
| 2.0.0.2-b | 10 | 12/08/2022 |
| 1.0.0.5-b | 10 | 12/08/2022 |
| 1.0.0.4-b | 10 | 12/08/2022 |