Browse Source

Migrate to localization

Equbuxu 2 years ago
parent
commit
975250bb6d

+ 15 - 21
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) { }
 }

+ 0 - 21
src/PixiEditor/Exceptions/InvalidFileFormatException.cs

@@ -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) { }
-}

+ 20 - 0
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) { }
+
+}

+ 6 - 16
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;
-    }
 }

+ 16 - 7
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) { }
 }

+ 6 - 5
src/PixiEditor/Models/Commands/Templates/Providers/Parsers/AsepriteKeysParser.cs

@@ -31,12 +31,12 @@ public class AsepriteKeysParser : KeysParser
     {
         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 @@ public class AsepriteKeysParser : KeysParser
         }
         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<KeyDefinition> keyDefinitions = new List<KeyDefinition>(); // DefaultShortcut is actually mapped shortcut.
@@ -68,9 +68,10 @@ public class AsepriteKeysParser : KeysParser
         {
             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);
         }
     }
 

+ 9 - 8
src/PixiEditor/Models/IO/Importer.cs

@@ -10,6 +10,7 @@ using PixiEditor.DrawingApi.Core.Surface.ImageData;
 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 @@ internal class Importer : NotifyableObject
 
             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 @@ internal class Importer : NotifyableObject
             }
             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 @@ internal class Importer : NotifyableObject
             }
             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 @@ internal class Importer : NotifyableObject
     {
         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;
     }

+ 14 - 4
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) { }
 }

+ 1 - 1
src/PixiEditor/Models/IO/PaletteParsers/JascPalFile/JascFileParser.cs

@@ -31,7 +31,7 @@ internal class JascFileParser : PaletteFileParser
             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<bool> SaveFile(string path, PaletteFileData data)

+ 6 - 2
src/PixiEditor/ViewModels/ImportFilePopupViewModel.cs

@@ -80,9 +80,13 @@ internal class ImportFilePopupViewModel : ViewModelBase
                 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);
             }
         }
     }

+ 1 - 1
src/PixiEditor/ViewModels/SettingsWindowViewModel.cs

@@ -171,7 +171,7 @@ internal class SettingsWindowViewModel : ViewModelBase
             }
             catch (RecoverableException e)
             {
-                NoticeDialog.Show(title: "Error", message: e.Message);
+                NoticeDialog.Show(title: "ERROR", message: e.DisplayMessage);
                 return false;
             }
         }

+ 2 - 2
src/PixiEditor/ViewModels/SubViewModels/Main/FileViewModel.cs

@@ -172,9 +172,9 @@ internal class FileViewModel : SubViewModel<ViewModelMain>
                 OpenRegularImage(path);
             }
         }
-        catch (CorruptedFileException ex)
+        catch (RecoverableException ex)
         {
-            NoticeDialog.Show(ex.Message, "FAILED_TO_OPEN_FILE");
+            NoticeDialog.Show(ex.DisplayMessage, "ERROR");
         }
         catch (OldFileFormatException)
         {

+ 1 - 1
src/PixiEditor/ViewModels/SubViewModels/Main/LayersViewModel.cs

@@ -361,7 +361,7 @@ internal class LayersViewModel : SubViewModel<ViewModelMain>
         }
         catch (RecoverableException e)
         {
-            NoticeDialog.Show("ERROR_IMPORTING_IMAGE", "ERROR");
+            NoticeDialog.Show(title: "ERROR", message: e.DisplayMessage);
             return;
         }
 

+ 1 - 1
src/PixiEditor/Views/Dialogs/ImportShortcutTemplatePopup.xaml.cs

@@ -54,7 +54,7 @@ internal partial class ImportShortcutTemplatePopup : Window
         }
         catch (RecoverableException e)
         {
-            NoticeDialog.Show($"FILE_INCORRECT_FORMAT", "ERROR");
+            NoticeDialog.Show(e.DisplayMessage, "ERROR");
             return;
         }