aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Services/Imaging/ImageException.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-04-25 21:20:04 +0800
committercrupest <crupest@outlook.com>2021-04-25 21:20:04 +0800
commita4a75188bd17e31b39a02511bbd6d628bab5c909 (patch)
treef2f9c63eb5beabb7a1a2f2605c2d5022f6a72c08 /BackEnd/Timeline/Services/Imaging/ImageException.cs
parent434be212c77bdade04722046e92c3dac25d0aff3 (diff)
downloadtimeline-a4a75188bd17e31b39a02511bbd6d628bab5c909.tar.gz
timeline-a4a75188bd17e31b39a02511bbd6d628bab5c909.tar.bz2
timeline-a4a75188bd17e31b39a02511bbd6d628bab5c909.zip
...
Diffstat (limited to 'BackEnd/Timeline/Services/Imaging/ImageException.cs')
-rw-r--r--BackEnd/Timeline/Services/Imaging/ImageException.cs58
1 files changed, 58 insertions, 0 deletions
diff --git a/BackEnd/Timeline/Services/Imaging/ImageException.cs b/BackEnd/Timeline/Services/Imaging/ImageException.cs
new file mode 100644
index 00000000..926ecc0a
--- /dev/null
+++ b/BackEnd/Timeline/Services/Imaging/ImageException.cs
@@ -0,0 +1,58 @@
+using System;
+using System.Globalization;
+
+namespace Timeline.Services.Imaging
+{
+ [Serializable]
+ public class ImageException : Exception
+ {
+ public enum ErrorReason
+ {
+ /// <summary>
+ /// Decoding image failed.
+ /// </summary>
+ CantDecode,
+ /// <summary>
+ /// Decoding succeeded but the real type is not the specified type.
+ /// </summary>
+ UnmatchedFormat,
+ /// <summary>
+ /// Image is not of required size.
+ /// </summary>
+ NotSquare,
+ /// <summary>
+ /// Other unknown errer.
+ /// </summary>
+ Unknown
+ }
+
+ public ImageException() : this(null) { }
+ public ImageException(string? message) : this(message, null) { }
+ public ImageException(string? message, Exception? inner) : this(ErrorReason.Unknown, null, null, null, message, inner) { }
+
+ public ImageException(ErrorReason error, byte[]? data, string? requestType, string? realType, Exception? inner) : this(error, data, requestType, realType, null, inner) { }
+ public ImageException(ErrorReason error, byte[]? data, string? requestType = null, string? realType = null, string? message = null, Exception? inner = null) : base(message ?? MakeMessage(error), inner) { Error = error; ImageData = data; RequestType = requestType; RealType = realType; }
+
+ protected ImageException(
+ System.Runtime.Serialization.SerializationInfo info,
+ System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
+
+ private static string MakeMessage(ErrorReason? reason) =>
+ string.Format(CultureInfo.InvariantCulture, Resource.ExceptionImage, reason switch
+ {
+ ErrorReason.CantDecode => Resource.ExceptionImageReasonCantDecode,
+ ErrorReason.UnmatchedFormat => Resource.ExceptionImageReasonUnmatchedFormat,
+ ErrorReason.NotSquare => Resource.ExceptionImageReasonBadSize,
+ _ => Resource.ExceptionImageReasonUnknownError
+ });
+
+ public ErrorReason Error { get; }
+#pragma warning disable CA1819 // Properties should not return arrays
+ public byte[]? ImageData { get; }
+#pragma warning restore CA1819 // Properties should not return arrays
+ public string? RequestType { get; }
+
+ // This field will be null if decoding failed.
+ public string? RealType { get; }
+ }
+}