Skip to content

Commit

Permalink
Fixes #130
Browse files Browse the repository at this point in the history
  • Loading branch information
Enkidu93 committed Nov 9, 2023
1 parent e1106b4 commit 9c6dee2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,11 @@ public static IMachineBuilder AddServalTranslationEngineService(
IEnumerable<TranslationEngineType>? engineTypes = null
)
{
builder.Services.AddGrpc(options => options.Interceptors.Add<UnimplementedInterceptor>());
builder.Services.AddGrpc(options =>
{
options.Interceptors.Add<CancellationInterceptor>();
options.Interceptors.Add<UnimplementedInterceptor>();
});
builder.AddServalPlatformService(connectionString ?? builder.Configuration.GetConnectionString("Serval"));
engineTypes ??=
builder.Configuration?.GetSection("TranslationEngines").Get<TranslationEngineType[]?>()
Expand Down
35 changes: 35 additions & 0 deletions src/SIL.Machine.AspNetCore/Services/CancellationInterceptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace SIL.Machine.AspNetCore.Services;

public class CancellationInterceptor : Interceptor
{
private readonly ILogger<CancellationInterceptor> _logger;

public CancellationInterceptor(ILogger<CancellationInterceptor> logger)
{
_logger = logger;
}

public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(
TRequest request,
ServerCallContext context,
UnaryServerMethod<TRequest, TResponse> continuation
)
{
try
{
return await continuation(request, context);
}
catch (Exception ex)
{
if (ex is OperationCanceledException)
{
_logger.LogInformation(exception: ex, message: null);
return null;

Check warning on line 27 in src/SIL.Machine.AspNetCore/Services/CancellationInterceptor.cs

View workflow job for this annotation

GitHub Actions / Build on ubuntu-20.04

Possible null reference return.

Check warning on line 27 in src/SIL.Machine.AspNetCore/Services/CancellationInterceptor.cs

View workflow job for this annotation

GitHub Actions / Build on ubuntu-20.04

Possible null reference return.

Check warning on line 27 in src/SIL.Machine.AspNetCore/Services/CancellationInterceptor.cs

View workflow job for this annotation

GitHub Actions / Build on windows-latest

Possible null reference return.

Check warning on line 27 in src/SIL.Machine.AspNetCore/Services/CancellationInterceptor.cs

View workflow job for this annotation

GitHub Actions / Build on windows-latest

Possible null reference return.

Check warning on line 27 in src/SIL.Machine.AspNetCore/Services/CancellationInterceptor.cs

View workflow job for this annotation

GitHub Actions / Create NuGet package

Possible null reference return.
}
else
{
throw;
}
}
}
}

0 comments on commit 9c6dee2

Please sign in to comment.