Skip to content

Commit

Permalink
Switch to exception filter --ECL
Browse files Browse the repository at this point in the history
  • Loading branch information
Enkidu93 committed Sep 11, 2023
1 parent 5f68270 commit 9199fc2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Serval.Shared.Controllers;

public class FailedPreconditionRpcExceptionFilter : ExceptionFilterAttribute
{
public override void OnException(ExceptionContext context)
{
if (context.Exception is RpcException rpcException && rpcException.StatusCode == StatusCode.FailedPrecondition)
{
context.Result = new ConflictObjectResult(rpcException.Message);
context.ExceptionHandled = true;
}
}
}
1 change: 1 addition & 0 deletions src/Serval.Shared/Controllers/ServalControllerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[TypeFilter(typeof(ServiceUnavailableExceptionFilter))]
[TypeFilter(typeof(ErrorResultFilter))]
[TypeFilter(typeof(AbortedRpcExceptionFilter))]
[TypeFilter(typeof(FailedPreconditionRpcExceptionFilter))]
public abstract class ServalControllerBase : Controller
{
private readonly IAuthorizationService _authService;
Expand Down
43 changes: 10 additions & 33 deletions src/Serval.Translation/Controllers/TranslationEnginesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,17 +216,7 @@ CancellationToken cancellationToken
if (!(await AuthorizeAsync(id, cancellationToken)).IsSuccess(out ActionResult? errorResult))
return errorResult;

TranslationResult? result;
try
{
result = await _engineService.TranslateAsync(id, segment, cancellationToken);
}
catch (RpcException r)
{
if (r.StatusCode == Grpc.Core.StatusCode.FailedPrecondition)
return Conflict();
throw;
}
TranslationResult? result = await _engineService.TranslateAsync(id, segment, cancellationToken);
if (result == null)
return NotFound();
return Ok(Map(result));
Expand Down Expand Up @@ -267,17 +257,13 @@ CancellationToken cancellationToken
if (!(await AuthorizeAsync(id, cancellationToken)).IsSuccess(out ActionResult? errorResult))
return errorResult;

IEnumerable<TranslationResult>? results;
try
{
results = await _engineService.TranslateAsync(id, n, segment, cancellationToken);
}
catch (RpcException r)
{
if (r.StatusCode == Grpc.Core.StatusCode.FailedPrecondition)
return Conflict();
throw;
}
IEnumerable<TranslationResult>? results = await _engineService.TranslateAsync(
id,
n,
segment,
cancellationToken
);

if (results == null)
return NotFound();
return Ok(results.Select(Map));
Expand Down Expand Up @@ -315,17 +301,8 @@ CancellationToken cancellationToken
{
if (!(await AuthorizeAsync(id, cancellationToken)).IsSuccess(out ActionResult? errorResult))
return errorResult;
WordGraph? wordGraph;
try
{
wordGraph = await _engineService.GetWordGraphAsync(id, segment, cancellationToken);
}
catch (RpcException r)
{
if (r.StatusCode == Grpc.Core.StatusCode.FailedPrecondition)
return Conflict();
throw;
}
WordGraph? wordGraph = await _engineService.GetWordGraphAsync(id, segment, cancellationToken);

if (wordGraph == null)
return NotFound();
return Ok(Map(wordGraph));
Expand Down

0 comments on commit 9199fc2

Please sign in to comment.