diff --git a/src/Persistence/MsappPacking/MsappPackException.cs b/src/Persistence/MsappPacking/MsappPackException.cs index eebca4f6..571c21ac 100644 --- a/src/Persistence/MsappPacking/MsappPackException.cs +++ b/src/Persistence/MsappPacking/MsappPackException.cs @@ -8,13 +8,20 @@ namespace Microsoft.PowerPlatform.PowerApps.Persistence.MsappPacking; /// public sealed class MsappPackException : Exception { - public MsappPackException(string message) + public MsappPackException(MsappPackExceptionReason reason, string message) : base(message) { + Reason = reason; } - public MsappPackException(string message, Exception innerException) + public MsappPackException(MsappPackExceptionReason reason, string message, Exception innerException) : base(message, innerException) { + Reason = reason; } + + /// + /// Identifies the reason why this exception was thrown. + /// + public MsappPackExceptionReason Reason { get; } } diff --git a/src/Persistence/MsappPacking/MsappPackExceptionReason.cs b/src/Persistence/MsappPacking/MsappPackExceptionReason.cs new file mode 100644 index 00000000..2251ea72 --- /dev/null +++ b/src/Persistence/MsappPacking/MsappPackExceptionReason.cs @@ -0,0 +1,15 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +namespace Microsoft.PowerPlatform.PowerApps.Persistence.MsappPacking; + +/// +/// Identifies the scenario in which an was thrown. +/// +public enum MsappPackExceptionReason +{ + /// + /// An output file already exists and overwriting output is not enabled. + /// + OutputAlreadyExists, +} diff --git a/src/Persistence/MsappPacking/MsappPackingService.cs b/src/Persistence/MsappPacking/MsappPackingService.cs index 7e8922db..939b0c21 100644 --- a/src/Persistence/MsappPacking/MsappPackingService.cs +++ b/src/Persistence/MsappPacking/MsappPackingService.cs @@ -64,14 +64,14 @@ public async Task UnpackToDirectoryAsync( if (!options.OverwriteOutput) { if (File.Exists(msaprPath)) - throw new MsappUnpackException($"Output file '{msaprPath}' already exists and overwriting output is not enabled."); + throw new MsappUnpackException(MsappUnpackExceptionReason.OutputAlreadyExists, $"Output file '{msaprPath}' already exists and overwriting output is not enabled."); // Source code should all go into the same 'Src' folder, so we only need to see if it exists and has files inside // We will silently remove any empty 'Src' folder if (Directory.Exists(srcOutputDirectoryPath) && Directory.EnumerateFiles(srcOutputDirectoryPath, "*", SearchOption.AllDirectories).Any()) { - throw new MsappUnpackException($"Output folder '{srcOutputDirectoryPath}' is not empty and overwriting output is not enabled."); + throw new MsappUnpackException(MsappUnpackExceptionReason.OutputAlreadyExists, $"Output folder '{srcOutputDirectoryPath}' is not empty and overwriting output is not enabled."); } // Assets should all go into the same 'Assets' folder, so we only need to see if it exists and has files inside @@ -79,7 +79,7 @@ public async Task UnpackToDirectoryAsync( if (Directory.Exists(assetsOutputDirectoryPath) && Directory.EnumerateFiles(assetsOutputDirectoryPath, "*", SearchOption.AllDirectories).Any()) { - throw new MsappUnpackException($"Output folder '{assetsOutputDirectoryPath}' is not empty and overwriting output is not enabled."); + throw new MsappUnpackException(MsappUnpackExceptionReason.OutputAlreadyExists, $"Output folder '{assetsOutputDirectoryPath}' is not empty and overwriting output is not enabled."); } } @@ -132,10 +132,10 @@ public async Task UnpackToDirectoryAsync( internal static void ValidateMsappUnpackIsSupported(MsappArchive msappArchive) { if (msappArchive.MSAppStructureVersion < MinSupportedMSAppStructureVersion) - throw new MsappUnpackException($"MSAppStructureVersion {msappArchive.MSAppStructureVersion} is below the minimum supported version {MinSupportedMSAppStructureVersion}."); + throw new MsappUnpackException(MsappUnpackExceptionReason.UnsupportedMSAppStructureVersion, $"MSAppStructureVersion {msappArchive.MSAppStructureVersion} is below the minimum supported version {MinSupportedMSAppStructureVersion}."); if (msappArchive.DocVersion < MinSupportedDocVersion) - throw new MsappUnpackException($"DocVersion {msappArchive.DocVersion} is below the minimum supported version {MinSupportedDocVersion}."); + throw new MsappUnpackException(MsappUnpackExceptionReason.UnsupportedDocVersion, $"DocVersion {msappArchive.DocVersion} is below the minimum supported version {MinSupportedDocVersion}."); } private static MsaprHeaderJson CreateMsaprHeaderJson(UnpackedConfiguration unpackedConfig) @@ -235,7 +235,7 @@ public async Task PackFromMsappReferenceFileAsync( msaprPath = Path.GetFullPath(msaprPath); if (!options.OverwriteOutput && File.Exists(outputMsappPath)) - throw new MsappPackException($"Output file '{outputMsappPath}' already exists and overwriting output is not enabled."); + throw new MsappPackException(MsappPackExceptionReason.OutputAlreadyExists, $"Output file '{outputMsappPath}' already exists and overwriting output is not enabled."); var unpackedFolderPath = Path.GetDirectoryName(msaprPath)!; diff --git a/src/Persistence/MsappPacking/MsappUnpackException.cs b/src/Persistence/MsappPacking/MsappUnpackException.cs index 13125c04..e3766ec2 100644 --- a/src/Persistence/MsappPacking/MsappUnpackException.cs +++ b/src/Persistence/MsappPacking/MsappUnpackException.cs @@ -8,13 +8,20 @@ namespace Microsoft.PowerPlatform.PowerApps.Persistence.MsappPacking; /// public sealed class MsappUnpackException : Exception { - public MsappUnpackException(string message) + public MsappUnpackException(MsappUnpackExceptionReason reason, string message) : base(message) { + Reason = reason; } - public MsappUnpackException(string message, Exception innerException) + public MsappUnpackException(MsappUnpackExceptionReason reason, string message, Exception innerException) : base(message, innerException) { + Reason = reason; } + + /// + /// Identifies the reason why this exception was thrown. + /// + public MsappUnpackExceptionReason Reason { get; } } diff --git a/src/Persistence/MsappPacking/MsappUnpackExceptionReason.cs b/src/Persistence/MsappPacking/MsappUnpackExceptionReason.cs new file mode 100644 index 00000000..77ad4b56 --- /dev/null +++ b/src/Persistence/MsappPacking/MsappUnpackExceptionReason.cs @@ -0,0 +1,25 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +namespace Microsoft.PowerPlatform.PowerApps.Persistence.MsappPacking; + +/// +/// Identifies the scenario in which an was thrown. +/// +public enum MsappUnpackExceptionReason +{ + /// + /// An output file or folder already exists and overwriting output is not enabled. + /// + OutputAlreadyExists, + + /// + /// The MSApp structure version is below the minimum supported version. + /// + UnsupportedMSAppStructureVersion, + + /// + /// The document version is below the minimum supported version. + /// + UnsupportedDocVersion, +}