Skip to content

Commit

Permalink
Migrate to localization
Browse files Browse the repository at this point in the history
  • Loading branch information
Equbuxu committed May 6, 2023
1 parent 42d1e2d commit 975250b
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 90 deletions.
36 changes: 15 additions & 21 deletions src/PixiEditor/Exceptions/CorruptedFileException.cs
Original file line number Diff line number Diff line change
@@ -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) { }
}
21 changes: 0 additions & 21 deletions src/PixiEditor/Exceptions/InvalidFileFormatException.cs

This file was deleted.

20 changes: 20 additions & 0 deletions src/PixiEditor/Exceptions/InvalidFileTypeException.cs
Original file line number Diff line number Diff line change
@@ -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) { }

}
22 changes: 6 additions & 16 deletions src/PixiEditor/Exceptions/MissingFileException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
23 changes: 16 additions & 7 deletions src/PixiEditor/Exceptions/RecoverableException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<KeyDefinition> keyDefinitions = new List<KeyDefinition>(); // DefaultShortcut is actually mapped shortcut.
Expand All @@ -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);
}
}

Expand Down
17 changes: 9 additions & 8 deletions src/PixiEditor/Models/IO/Importer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}
}
Expand All @@ -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);
}
}
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private static async Task<PaletteFileData> 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<bool> SaveFile(string path, PaletteFileData data)
Expand Down
8 changes: 6 additions & 2 deletions src/PixiEditor/ViewModels/ImportFilePopupViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/PixiEditor/ViewModels/SettingsWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private static bool TryImport(OpenFileDialog dialog, ref List<Shortcut> shortcut
}
catch (RecoverableException e)
{
NoticeDialog.Show(title: "Error", message: e.Message);
NoticeDialog.Show(title: "ERROR", message: e.DisplayMessage);
return false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/PixiEditor/ViewModels/SubViewModels/Main/FileViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ public void ImportReferenceLayer()
}
catch (RecoverableException e)
{
NoticeDialog.Show("ERROR_IMPORTING_IMAGE", "ERROR");
NoticeDialog.Show(title: "ERROR", message: e.DisplayMessage);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 975250b

Please sign in to comment.