From 03f2ad8a1e4398cd322ceb8a135d8378df8e8a51 Mon Sep 17 00:00:00 2001 From: Fesenko-A <111450737+Fesenko-A@users.noreply.github.com> Date: Thu, 23 Apr 2026 11:43:39 +0300 Subject: [PATCH] add error handling for Transformation library in 'Upload entry' action --- Apps.Contentful/Actions/EntryActions.cs | 11 ++++--- Apps.Contentful/Apps.Contentful.csproj | 2 +- .../Constants/KnownErrorMessages.cs | 7 +++++ Apps.Contentful/Utils/ErrorHandler.cs | 29 +++++++++++++++++++ Tests.Contentful/Tests.Contentful.csproj | 12 -------- 5 files changed, 44 insertions(+), 17 deletions(-) create mode 100644 Apps.Contentful/Constants/KnownErrorMessages.cs create mode 100644 Apps.Contentful/Utils/ErrorHandler.cs diff --git a/Apps.Contentful/Actions/EntryActions.cs b/Apps.Contentful/Actions/EntryActions.cs index b8de9cb..209a56f 100644 --- a/Apps.Contentful/Actions/EntryActions.cs +++ b/Apps.Contentful/Actions/EntryActions.cs @@ -397,10 +397,13 @@ public async Task SetEntryLocalizableFieldsFromHtmlFile( Transformation? transformation = null; if (Xliff2Serializer.IsXliff2(content) || Xliff1Serializer.IsXliff1(content)) { - transformation = Transformation.Parse(content, input.Content.Name); - content = transformation.Target().Serialize(); - if (content == null) - throw new PluginMisconfigurationException("XLIFF did not contain any files"); + content = ErrorHandler.ExecuteWithErrorHandling(() => + { + transformation = Transformation.Parse(content, input.Content.Name); + return + transformation.Target().Serialize() ?? + throw new PluginMisconfigurationException("XLIFF did not contain any files"); + }); } errors.AddRange(_customSizeValidationService.Validate(content, input.Locale, input.SkipCustomValidationStep == true)); diff --git a/Apps.Contentful/Apps.Contentful.csproj b/Apps.Contentful/Apps.Contentful.csproj index 30ccd84..c6c9006 100644 --- a/Apps.Contentful/Apps.Contentful.csproj +++ b/Apps.Contentful/Apps.Contentful.csproj @@ -6,7 +6,7 @@ enable Contentful The headless content management system - 1.8.14 + 1.8.15 Apps.Contentful diff --git a/Apps.Contentful/Constants/KnownErrorMessages.cs b/Apps.Contentful/Constants/KnownErrorMessages.cs new file mode 100644 index 0000000..af21491 --- /dev/null +++ b/Apps.Contentful/Constants/KnownErrorMessages.cs @@ -0,0 +1,7 @@ +namespace Apps.Contentful.Constants; + +public static class KnownErrorMessages +{ + public const string CannotConvertToContent = "Cannot convert to content, no original data found"; + public const string CouldNotDetectContentType = "Could not detect any valid content type this library can process"; +} \ No newline at end of file diff --git a/Apps.Contentful/Utils/ErrorHandler.cs b/Apps.Contentful/Utils/ErrorHandler.cs new file mode 100644 index 0000000..37493f7 --- /dev/null +++ b/Apps.Contentful/Utils/ErrorHandler.cs @@ -0,0 +1,29 @@ +using Apps.Contentful.Constants; +using Blackbird.Applications.Sdk.Common.Exceptions; + +namespace Apps.Contentful.Utils; + +public static class ErrorHandler +{ + private static readonly Dictionary ErrorMappings = new() + { + { KnownErrorMessages.CouldNotDetectContentType, "This file type is not supported" }, + { KnownErrorMessages.CannotConvertToContent, "Cannot generate the original document format. The uploaded XLIFF is missing the original file's structural data." } + }; + + public static T ExecuteWithErrorHandling(Func action) + { + try + { + return action(); + } + catch (Exception ex) + { + var mapping = ErrorMappings.FirstOrDefault(m => ex.Message.Contains(m.Key)); + if (mapping.Key != null) + throw new PluginMisconfigurationException(mapping.Value); + + throw new PluginApplicationException(ex.Message); + } + } +} \ No newline at end of file diff --git a/Tests.Contentful/Tests.Contentful.csproj b/Tests.Contentful/Tests.Contentful.csproj index 2dfd680..5d458fe 100644 --- a/Tests.Contentful/Tests.Contentful.csproj +++ b/Tests.Contentful/Tests.Contentful.csproj @@ -27,18 +27,6 @@ - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - PreserveNewest