From 89c106169bd2a16310fdaa6e0c48a3402d97de3a Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Thu, 24 Oct 2019 16:56:41 +0800 Subject: ... --- Timeline/Services/AvatarFormatException.cs | 51 ++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Timeline/Services/AvatarFormatException.cs (limited to 'Timeline/Services/AvatarFormatException.cs') diff --git a/Timeline/Services/AvatarFormatException.cs b/Timeline/Services/AvatarFormatException.cs new file mode 100644 index 00000000..788eabb2 --- /dev/null +++ b/Timeline/Services/AvatarFormatException.cs @@ -0,0 +1,51 @@ +using System; +using System.Globalization; + +namespace Timeline.Services +{ + /// + /// Thrown when avatar is of bad format. + /// + [Serializable] + public class AvatarFormatException : 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. + /// + BadSize + } + + public AvatarFormatException() : base(MakeMessage(null)) { } + public AvatarFormatException(string message) : base(message) { } + public AvatarFormatException(string message, Exception inner) : base(message, inner) { } + + public AvatarFormatException(Avatar avatar, ErrorReason error) : base(MakeMessage(error)) { Avatar = avatar; Error = error; } + public AvatarFormatException(Avatar avatar, ErrorReason error, Exception inner) : base(MakeMessage(error), inner) { Avatar = avatar; Error = error; } + + protected AvatarFormatException( + 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.AvatarFormatException, reason switch + { + ErrorReason.CantDecode => Resources.Services.Exception.AvatarFormatExceptionCantDecode, + ErrorReason.UnmatchedFormat => Resources.Services.Exception.AvatarFormatExceptionUnmatchedFormat, + ErrorReason.BadSize => Resources.Services.Exception.AvatarFormatExceptionBadSize, + _ => Resources.Services.Exception.AvatarFormatExceptionUnknownError + }); + + public ErrorReason? Error { get; set; } + public Avatar? Avatar { get; set; } + } +} -- cgit v1.2.3