From 99cf6981e7528b8e53f73ba91a7d6a0cf01f3a1f Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 6 Mar 2020 22:28:32 +0800 Subject: Init development of post image feature. --- Timeline/Services/ImageException.cs | 54 +++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Timeline/Services/ImageException.cs (limited to 'Timeline/Services/ImageException.cs') diff --git a/Timeline/Services/ImageException.cs b/Timeline/Services/ImageException.cs new file mode 100644 index 00000000..c6126aa3 --- /dev/null +++ b/Timeline/Services/ImageException.cs @@ -0,0 +1,54 @@ +using System; +using System.Globalization; + +namespace Timeline.Services +{ + [Serializable] + public class ImageException : Exception + { + public enum ErrorReason + { + /// + /// Decoding image failed. + /// + CantDecode, + /// + /// Decoding succeeded but the real type is not the specified type. + /// + UnmatchedFormat, + /// + /// Image is not a square. + /// + NotSquare + } + + public ImageException() : base(MakeMessage(null)) { } + public ImageException(string message) : base(message) { } + public ImageException(string message, Exception inner) : base(message, inner) { } + + public ImageException(ErrorReason error, byte[]? data = null, string? requestType = null, string? realType = null) : base(MakeMessage(error)) { Error = error; ImageData = data; RequestType = requestType; RealType = realType; } + public ImageException(Exception inner, ErrorReason error, byte[]? data = null, string? requestType = null, string? realType = null) : base(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, Resources.Services.Exception.ImageException, reason switch + { + ErrorReason.CantDecode => Resources.Services.Exception.ImageExceptionCantDecode, + ErrorReason.UnmatchedFormat => Resources.Services.Exception.ImageExceptionUnmatchedFormat, + ErrorReason.NotSquare => Resources.Services.Exception.ImageExceptionBadSize, + _ => Resources.Services.Exception.ImageExceptionUnknownError + }); + + 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; } + } +} -- cgit v1.2.3