Initial version
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
using EonaCat.gRPC.Api.Helpers;
|
||||
|
||||
namespace EonaCat.gRPC.Api.Middleware.Interceptors;
|
||||
|
||||
public class ExceptionInterceptor : Interceptor
|
||||
{
|
||||
private readonly ILogger<ExceptionInterceptor> _logger;
|
||||
private readonly Guid _correlationId;
|
||||
|
||||
public ExceptionInterceptor(ILogger<ExceptionInterceptor> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
_correlationId = Guid.NewGuid();
|
||||
}
|
||||
|
||||
public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(
|
||||
TRequest request,
|
||||
ServerCallContext context,
|
||||
UnaryServerMethod<TRequest, TResponse> continuation)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await continuation(request, context);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw e.Handle(context, _logger, _correlationId);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task<TResponse> ClientStreamingServerHandler<TRequest, TResponse>(
|
||||
IAsyncStreamReader<TRequest> requestStream,
|
||||
ServerCallContext context,
|
||||
ClientStreamingServerMethod<TRequest, TResponse> continuation)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await continuation(requestStream, context);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw e.Handle(context, _logger, _correlationId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public override async Task ServerStreamingServerHandler<TRequest, TResponse>(
|
||||
TRequest request,
|
||||
IServerStreamWriter<TResponse> responseStream,
|
||||
ServerCallContext context,
|
||||
ServerStreamingServerMethod<TRequest, TResponse> continuation)
|
||||
{
|
||||
try
|
||||
{
|
||||
await continuation(request, responseStream, context);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw e.Handle(context, _logger, _correlationId);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task DuplexStreamingServerHandler<TRequest, TResponse>(
|
||||
IAsyncStreamReader<TRequest> requestStream,
|
||||
IServerStreamWriter<TResponse> responseStream,
|
||||
ServerCallContext context,
|
||||
DuplexStreamingServerMethod<TRequest, TResponse> continuation)
|
||||
{
|
||||
try
|
||||
{
|
||||
await continuation(requestStream, responseStream, context);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw e.Handle(context, _logger, _correlationId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
namespace EonaCat.gRPC.Api.Middleware.Interceptors;
|
||||
|
||||
public class LoggerInterceptor : Interceptor
|
||||
{
|
||||
private readonly ILogger<LoggerInterceptor> _logger;
|
||||
|
||||
public LoggerInterceptor(ILogger<LoggerInterceptor> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
|
||||
public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(
|
||||
TRequest request,
|
||||
ServerCallContext context,
|
||||
UnaryServerMethod<TRequest, TResponse> continuation)
|
||||
{
|
||||
LogCall(context);
|
||||
try
|
||||
{
|
||||
return await continuation(request, context);
|
||||
}
|
||||
catch (SqlException e)
|
||||
{
|
||||
_logger.LogError(e, $"An SQL error occurred when calling {context.Method}");
|
||||
Status status = e.Number == -2
|
||||
? new Status(StatusCode.DeadlineExceeded, $"SQL timeout: {e.Message}")
|
||||
: new Status(StatusCode.Internal, $"SQL error: {e.Message}");
|
||||
|
||||
throw new RpcException(status, e.Message);
|
||||
}
|
||||
catch (RpcException e)
|
||||
{
|
||||
_logger.LogError(e, $"gRPC error when calling {context.Method}: {e.Status.Detail}");
|
||||
throw;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, $"An error occurred when calling {context.Method}");
|
||||
throw new RpcException(Status.DefaultCancelled, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private void LogCall(ServerCallContext context)
|
||||
{
|
||||
var httpContext = context.GetHttpContext();
|
||||
_logger.LogDebug($"Starting call. Request: {httpContext.Request.Path}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user