diff --git a/src/Serval.Shared/Controllers/FailedPreconditionRpcExceptionFilter.cs b/src/Serval.Shared/Controllers/FailedPreconditionRpcExceptionFilter.cs new file mode 100644 index 00000000..160eb9a6 --- /dev/null +++ b/src/Serval.Shared/Controllers/FailedPreconditionRpcExceptionFilter.cs @@ -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; + } + } +} diff --git a/src/Serval.Shared/Controllers/ServalControllerBase.cs b/src/Serval.Shared/Controllers/ServalControllerBase.cs index bebcdf29..08adbf54 100644 --- a/src/Serval.Shared/Controllers/ServalControllerBase.cs +++ b/src/Serval.Shared/Controllers/ServalControllerBase.cs @@ -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; diff --git a/src/Serval.Translation/Controllers/TranslationEnginesController.cs b/src/Serval.Translation/Controllers/TranslationEnginesController.cs index d3f6ab25..8f86ccb3 100644 --- a/src/Serval.Translation/Controllers/TranslationEnginesController.cs +++ b/src/Serval.Translation/Controllers/TranslationEnginesController.cs @@ -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)); @@ -267,17 +257,13 @@ CancellationToken cancellationToken if (!(await AuthorizeAsync(id, cancellationToken)).IsSuccess(out ActionResult? errorResult)) return errorResult; - IEnumerable? results; - try - { - results = await _engineService.TranslateAsync(id, n, segment, cancellationToken); - } - catch (RpcException r) - { - if (r.StatusCode == Grpc.Core.StatusCode.FailedPrecondition) - return Conflict(); - throw; - } + IEnumerable? results = await _engineService.TranslateAsync( + id, + n, + segment, + cancellationToken + ); + if (results == null) return NotFound(); return Ok(results.Select(Map)); @@ -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));