diff --git a/src/PixiEditor/Exceptions/CorruptedFileException.cs b/src/PixiEditor/Exceptions/CorruptedFileException.cs index 0fe36554f..ac49d22ec 100644 --- a/src/PixiEditor/Exceptions/CorruptedFileException.cs +++ b/src/PixiEditor/Exceptions/CorruptedFileException.cs @@ -1,29 +1,23 @@ using System.IO; +using System.Runtime.Serialization; +using PixiEditor.Localization; namespace PixiEditor.Exceptions; [Serializable] internal class CorruptedFileException : RecoverableException { - public CorruptedFileException() - : base("The file you've chosen might be corrupted.") - { - } - - public CorruptedFileException(string message) - : base(message) - { - } - - public CorruptedFileException(string message, Exception inner) - : base(message, inner) - { - } - - protected CorruptedFileException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) - : base(info, context) - { - } + public CorruptedFileException() : base("FAILED_TO_OPEN_FILE") { } + + public CorruptedFileException(Exception innerException) : base("FAILED_TO_OPEN_FILE", innerException) { } + + public CorruptedFileException(LocalizedString displayMessage) : base(displayMessage) { } + + public CorruptedFileException(LocalizedString displayMessage, Exception innerException) : base(displayMessage, innerException) { } + + public CorruptedFileException(LocalizedString displayMessage, string exceptionMessage) : base(displayMessage, exceptionMessage) { } + + public CorruptedFileException(LocalizedString displayMessage, string exceptionMessage, Exception innerException) : base(displayMessage, exceptionMessage, innerException) { } + + protected CorruptedFileException(SerializationInfo info, StreamingContext context) : base(info, context) { } } diff --git a/src/PixiEditor/Exceptions/InvalidFileFormatException.cs b/src/PixiEditor/Exceptions/InvalidFileFormatException.cs deleted file mode 100644 index e49b6ebb4..000000000 --- a/src/PixiEditor/Exceptions/InvalidFileFormatException.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.Runtime.Serialization; - -namespace PixiEditor.Exceptions; - -internal class InvalidFileFormatException : RecoverableException -{ - public string FilePath { get; set; } - - public InvalidFileFormatException() { } - - public InvalidFileFormatException(string message) : base(message) { } - - public InvalidFileFormatException(string message, string filePath) : base(message) - { - FilePath = filePath; - } - - public InvalidFileFormatException(string message, Exception innerException) : base(message, innerException) { } - - protected InvalidFileFormatException(SerializationInfo info, StreamingContext context) : base(info, context) { } -} diff --git a/src/PixiEditor/Exceptions/InvalidFileTypeException.cs b/src/PixiEditor/Exceptions/InvalidFileTypeException.cs new file mode 100644 index 000000000..b331a838e --- /dev/null +++ b/src/PixiEditor/Exceptions/InvalidFileTypeException.cs @@ -0,0 +1,20 @@ +using System.Runtime.Serialization; +using PixiEditor.Localization; + +namespace PixiEditor.Exceptions; + +internal class InvalidFileTypeException : RecoverableException +{ + public InvalidFileTypeException() { } + + public InvalidFileTypeException(LocalizedString displayMessage) : base(displayMessage) { } + + public InvalidFileTypeException(LocalizedString displayMessage, Exception innerException) : base(displayMessage, innerException) { } + + public InvalidFileTypeException(LocalizedString displayMessage, string exceptionMessage) : base(displayMessage, exceptionMessage) { } + + public InvalidFileTypeException(LocalizedString displayMessage, string exceptionMessage, Exception innerException) : base(displayMessage, exceptionMessage, innerException) { } + + protected InvalidFileTypeException(SerializationInfo info, StreamingContext context) : base(info, context) { } + +} diff --git a/src/PixiEditor/Exceptions/MissingFileException.cs b/src/PixiEditor/Exceptions/MissingFileException.cs index 87ec53b87..490d7ab6d 100644 --- a/src/PixiEditor/Exceptions/MissingFileException.cs +++ b/src/PixiEditor/Exceptions/MissingFileException.cs @@ -5,27 +5,17 @@ namespace PixiEditor.Exceptions; internal class MissingFileException : RecoverableException { - public string FilePath { get; set; } + public MissingFileException() : base("FILE_NOT_FOUND") { } - public MissingFileException() { } + public MissingFileException(Exception innerException) : base("FILE_NOT_FOUND", innerException) { } - public MissingFileException(string message) : base(message) { } + public MissingFileException(LocalizedString displayMessage) : base(displayMessage) { } - public MissingFileException(string message, Exception innerException) : base(message, innerException) { } + public MissingFileException(LocalizedString displayMessage, Exception innerException) : base(displayMessage, innerException) { } - public MissingFileException(string message, LocalizedString displayMessage) : base(message, displayMessage) { } + public MissingFileException(LocalizedString displayMessage, string exceptionMessage) : base(displayMessage, exceptionMessage) { } - public MissingFileException(string message, LocalizedString displayMessage, Exception innerException) : base(message, displayMessage, innerException) { } + public MissingFileException(LocalizedString displayMessage, string exceptionMessage, Exception innerException) : base(displayMessage, exceptionMessage, innerException) { } protected MissingFileException(SerializationInfo info, StreamingContext context) : base(info, context) { } - - public MissingFileException(string message, string filePath) : base(message) - { - FilePath = filePath; - } - - public MissingFileException(string message, LocalizedString displayMessage, string filePath) : base(message, displayMessage) - { - FilePath = filePath; - } } diff --git a/src/PixiEditor/Exceptions/RecoverableException.cs b/src/PixiEditor/Exceptions/RecoverableException.cs index 85b703344..2caa01a4e 100644 --- a/src/PixiEditor/Exceptions/RecoverableException.cs +++ b/src/PixiEditor/Exceptions/RecoverableException.cs @@ -7,21 +7,30 @@ public class RecoverableException : Exception { public LocalizedString DisplayMessage { get; set; } - public RecoverableException() { } - - public RecoverableException(string message) : base(message) { } + public RecoverableException() + { + DisplayMessage = "INTERNAL_ERROR"; + } - public RecoverableException(string message, Exception innerException) : base(message, innerException) { } + public RecoverableException(LocalizedString displayMessage) + { + DisplayMessage = displayMessage; + } - protected RecoverableException(SerializationInfo info, StreamingContext context) : base(info, context) { } + public RecoverableException(LocalizedString displayMessage, Exception innerException) : base(null, innerException) + { + DisplayMessage = displayMessage; + } - public RecoverableException(string message, LocalizedString displayMessage) : base(message) + public RecoverableException(LocalizedString displayMessage, string exceptionMessage) : base(exceptionMessage) { DisplayMessage = displayMessage; } - public RecoverableException(string message, LocalizedString displayMessage, Exception innerException) : base(message, innerException) + public RecoverableException(LocalizedString displayMessage, string exceptionMessage, Exception innerException) : base(exceptionMessage, innerException) { DisplayMessage = displayMessage; } + + protected RecoverableException(SerializationInfo info, StreamingContext context) : base(info, context) { } } diff --git a/src/PixiEditor/Models/Commands/Templates/Providers/Parsers/AsepriteKeysParser.cs b/src/PixiEditor/Models/Commands/Templates/Providers/Parsers/AsepriteKeysParser.cs index a7c2327d8..18bba3f9b 100644 --- a/src/PixiEditor/Models/Commands/Templates/Providers/Parsers/AsepriteKeysParser.cs +++ b/src/PixiEditor/Models/Commands/Templates/Providers/Parsers/AsepriteKeysParser.cs @@ -31,12 +31,12 @@ public override ShortcutsTemplate Parse(string path, bool applyDefaults) { if (!File.Exists(path)) { - throw new MissingFileException("File not found", path); + throw new MissingFileException("FILE_NOT_FOUND", $"File {path} not found"); } if (Path.GetExtension(path) != ".aseprite-keys") { - throw new InvalidFileFormatException("File is not aseprite-keys file", path); + throw new InvalidFileTypeException("FILE_FORMAT_NOT_ASEPRITE_KEYS", $"File {path} is not an aseprite-keys file"); } return LoadAndParse(path, applyDefaults); @@ -52,11 +52,11 @@ private ShortcutsTemplate LoadAndParse(string path, bool applyDefaults) } catch (Exception e) when (e is DirectoryNotFoundException or FileNotFoundException or PathTooLongException) { - throw new MissingFileException("File not found", e); + throw new MissingFileException("FILE_NOT_FOUND", e); } catch (Exception e) { - throw new RecoverableException("Error while reading the file", e); + throw new RecoverableException("FAILED_TO_OPEN_FILE", e); } List keyDefinitions = new List(); // DefaultShortcut is actually mapped shortcut. @@ -68,9 +68,10 @@ private ShortcutsTemplate LoadAndParse(string path, bool applyDefaults) { return ShortcutsTemplate.FromKeyDefinitions(keyDefinitions); } + catch (RecoverableException) { throw; } catch (Exception e) { - throw new InvalidFileFormatException("The file contains an invalid shortcut", e); + throw new CorruptedFileException("FILE_HAS_INVALID_SHORTCUT", e); } } diff --git a/src/PixiEditor/Models/IO/Importer.cs b/src/PixiEditor/Models/IO/Importer.cs index 9b21a7e2d..792d6f430 100644 --- a/src/PixiEditor/Models/IO/Importer.cs +++ b/src/PixiEditor/Models/IO/Importer.cs @@ -10,6 +10,7 @@ using PixiEditor.DrawingApi.Core.Surface.PaintImpl; using PixiEditor.Exceptions; using PixiEditor.Helpers; +using PixiEditor.Localization; using PixiEditor.Models.DataHolders; using PixiEditor.Parser; using PixiEditor.Parser.Deprecated; @@ -51,17 +52,17 @@ public static WriteableBitmap ImportWriteableBitmap(string path) return BitmapFactory.ConvertToPbgra32Format(bitmap); } - catch (NotSupportedException) + catch (NotSupportedException e) { - throw new CorruptedFileException($"The file type '{Path.GetExtension(path)}' is not supported"); + throw new InvalidFileTypeException(new LocalizedString("FILE_EXTENSION_NOT_SUPPORTED", Path.GetExtension(path)), e); } - catch (FileFormatException) + catch (FileFormatException e) { - throw new CorruptedFileException("The file appears to be corrupted"); + throw new CorruptedFileException("FAILED_TO_OPEN_FILE", e); } catch (Exception e) { - throw new RecoverableException("Error while importing the bitmap", e); + throw new RecoverableException("ERROR_IMPORTING_IMAGE", e); } } @@ -83,7 +84,7 @@ public static DocumentViewModel ImportDocument(string path) } catch (InvalidFileException e) { - throw new CorruptedFileException("The given file seems to be corrupted or from a newer version of PixiEditor", e); + throw new CorruptedFileException("FAILED_TO_OPEN_FILE", e); } } } @@ -106,7 +107,7 @@ public static DocumentViewModel ImportDocument(byte[] file, string? originalFile } catch (InvalidFileException e) { - throw new CorruptedFileException("The given file seems to be corrupted or from a newer version of PixiEditor", e); + throw new CorruptedFileException("FAILED_TO_OPEN_FILE", e); } } } @@ -115,7 +116,7 @@ public static WriteableBitmap GetPreviewBitmap(string path) { if (!IsSupportedFile(path)) { - throw new InvalidFileFormatException($"The file type '{Path.GetExtension(path)}' is not supported"); + throw new InvalidFileTypeException(new LocalizedString("FILE_EXTENSION_NOT_SUPPORTED", Path.GetExtension(path))); } return Path.GetExtension(path) != ".pixi" ? ImportWriteableBitmap(path) : PixiParser.Deserialize(path).ToDocument().PreviewBitmap; } diff --git a/src/PixiEditor/Models/IO/PaletteParsers/JascPalFile/JascFileException.cs b/src/PixiEditor/Models/IO/PaletteParsers/JascPalFile/JascFileException.cs index 7a862e18b..1b0e906ca 100644 --- a/src/PixiEditor/Models/IO/PaletteParsers/JascPalFile/JascFileException.cs +++ b/src/PixiEditor/Models/IO/PaletteParsers/JascPalFile/JascFileException.cs @@ -1,11 +1,21 @@ -using PixiEditor.Exceptions; +using System.Runtime.Serialization; +using PixiEditor.Exceptions; +using PixiEditor.Localization; namespace PixiEditor.Models.IO.PaletteParsers.JascPalFile; internal class JascFileException : RecoverableException { - public JascFileException(string message) : base(message) - { - } + public JascFileException() { } + + public JascFileException(LocalizedString displayMessage) : base(displayMessage) { } + + public JascFileException(LocalizedString displayMessage, Exception innerException) : base(displayMessage, innerException) { } + + public JascFileException(LocalizedString displayMessage, string exceptionMessage) : base(displayMessage, exceptionMessage) { } + + public JascFileException(LocalizedString displayMessage, string exceptionMessage, Exception innerException) : base(displayMessage, exceptionMessage, innerException) { } + + protected JascFileException(SerializationInfo info, StreamingContext context) : base(info, context) { } } diff --git a/src/PixiEditor/Models/IO/PaletteParsers/JascPalFile/JascFileParser.cs b/src/PixiEditor/Models/IO/PaletteParsers/JascPalFile/JascFileParser.cs index 612fb715d..3abdfb403 100644 --- a/src/PixiEditor/Models/IO/PaletteParsers/JascPalFile/JascFileParser.cs +++ b/src/PixiEditor/Models/IO/PaletteParsers/JascPalFile/JascFileParser.cs @@ -31,7 +31,7 @@ private static async Task ParseFile(string path) return new PaletteFileData(name, colors); } - throw new JascFileException("Invalid JASC-PAL file."); + throw new JascFileException("FAILED_TO_OPEN_FILE", "Invalid JASC-PAL file."); } public static async Task SaveFile(string path, PaletteFileData data) diff --git a/src/PixiEditor/ViewModels/ImportFilePopupViewModel.cs b/src/PixiEditor/ViewModels/ImportFilePopupViewModel.cs index f1a0f39e4..0916aa275 100644 --- a/src/PixiEditor/ViewModels/ImportFilePopupViewModel.cs +++ b/src/PixiEditor/ViewModels/ImportFilePopupViewModel.cs @@ -80,9 +80,13 @@ private void CheckForPath(string path) ImportHeight = bitmap.PixelHeight; ImportWidth = bitmap.PixelWidth; } - catch (Exception e) when (e is NotSupportedException or FileFormatException or COMException) + catch (Exception e) when (e is NotSupportedException or FileFormatException) { - throw new CorruptedFileException(); + throw new CorruptedFileException("FAILED_TO_OPEN_FILE", e); + } + catch (COMException e) + { + throw new RecoverableException("INTERNAL_ERROR", e); } } } diff --git a/src/PixiEditor/ViewModels/SettingsWindowViewModel.cs b/src/PixiEditor/ViewModels/SettingsWindowViewModel.cs index 7a9b0e92e..96d4bd619 100644 --- a/src/PixiEditor/ViewModels/SettingsWindowViewModel.cs +++ b/src/PixiEditor/ViewModels/SettingsWindowViewModel.cs @@ -171,7 +171,7 @@ private static bool TryImport(OpenFileDialog dialog, ref List shortcut } catch (RecoverableException e) { - NoticeDialog.Show(title: "Error", message: e.Message); + NoticeDialog.Show(title: "ERROR", message: e.DisplayMessage); return false; } } diff --git a/src/PixiEditor/ViewModels/SubViewModels/Main/FileViewModel.cs b/src/PixiEditor/ViewModels/SubViewModels/Main/FileViewModel.cs index d00efaa95..5623959ec 100644 --- a/src/PixiEditor/ViewModels/SubViewModels/Main/FileViewModel.cs +++ b/src/PixiEditor/ViewModels/SubViewModels/Main/FileViewModel.cs @@ -172,9 +172,9 @@ public void OpenFromPath(string path) OpenRegularImage(path); } } - catch (CorruptedFileException ex) + catch (RecoverableException ex) { - NoticeDialog.Show(ex.Message, "FAILED_TO_OPEN_FILE"); + NoticeDialog.Show(ex.DisplayMessage, "ERROR"); } catch (OldFileFormatException) { diff --git a/src/PixiEditor/ViewModels/SubViewModels/Main/LayersViewModel.cs b/src/PixiEditor/ViewModels/SubViewModels/Main/LayersViewModel.cs index bb516da20..ca1f60b2a 100644 --- a/src/PixiEditor/ViewModels/SubViewModels/Main/LayersViewModel.cs +++ b/src/PixiEditor/ViewModels/SubViewModels/Main/LayersViewModel.cs @@ -361,7 +361,7 @@ public void ImportReferenceLayer() } catch (RecoverableException e) { - NoticeDialog.Show("ERROR_IMPORTING_IMAGE", "ERROR"); + NoticeDialog.Show(title: "ERROR", message: e.DisplayMessage); return; } diff --git a/src/PixiEditor/Views/Dialogs/ImportShortcutTemplatePopup.xaml.cs b/src/PixiEditor/Views/Dialogs/ImportShortcutTemplatePopup.xaml.cs index f7144b32e..025eb785b 100644 --- a/src/PixiEditor/Views/Dialogs/ImportShortcutTemplatePopup.xaml.cs +++ b/src/PixiEditor/Views/Dialogs/ImportShortcutTemplatePopup.xaml.cs @@ -54,7 +54,7 @@ public static void ImportInstallation(ShortcutProvider provider) } catch (RecoverableException e) { - NoticeDialog.Show($"FILE_INCORRECT_FORMAT", "ERROR"); + NoticeDialog.Show(e.DisplayMessage, "ERROR"); return; }