From 3dc9460798798e5ba155fae7b6afe84522c2c619 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 17 Jun 2020 17:10:40 +0800 Subject: refactor(back): Refactor timeline service. --- Timeline/Services/Exceptions/ImageException.cs | 57 ++++++++++++++++++++++ .../Exceptions/TimelinePostNoDataException.cs | 15 ++++++ .../Exceptions/TimelinePostNotExistException.cs | 2 +- 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 Timeline/Services/Exceptions/ImageException.cs create mode 100644 Timeline/Services/Exceptions/TimelinePostNoDataException.cs (limited to 'Timeline/Services/Exceptions') diff --git a/Timeline/Services/Exceptions/ImageException.cs b/Timeline/Services/Exceptions/ImageException.cs new file mode 100644 index 00000000..20dd48ae --- /dev/null +++ b/Timeline/Services/Exceptions/ImageException.cs @@ -0,0 +1,57 @@ +using System; +using System.Globalization; + +namespace Timeline.Services.Exceptions +{ + [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 of required size. + /// + NotSquare, + /// + /// Other unknown errer. + /// + 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 = null, string? realType = null, string? message = null, Exception? inner = null) : base(MakeMessage(error).AppendAdditionalMessage(message), 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.Exceptions.ImageException, reason switch + { + ErrorReason.CantDecode => Resources.Services.Exceptions.ImageExceptionCantDecode, + ErrorReason.UnmatchedFormat => Resources.Services.Exceptions.ImageExceptionUnmatchedFormat, + ErrorReason.NotSquare => Resources.Services.Exceptions.ImageExceptionBadSize, + _ => Resources.Services.Exceptions.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; } + } +} diff --git a/Timeline/Services/Exceptions/TimelinePostNoDataException.cs b/Timeline/Services/Exceptions/TimelinePostNoDataException.cs new file mode 100644 index 00000000..c4b6bf62 --- /dev/null +++ b/Timeline/Services/Exceptions/TimelinePostNoDataException.cs @@ -0,0 +1,15 @@ +using System; + +namespace Timeline.Services.Exceptions +{ + [Serializable] + public class TimelinePostNoDataException : Exception + { + public TimelinePostNoDataException() : this(null, null) { } + public TimelinePostNoDataException(string? message) : this(message, null) { } + public TimelinePostNoDataException(string? message, Exception? inner) : base(Resources.Services.Exceptions.TimelineNoDataException.AppendAdditionalMessage(message), inner) { } + protected TimelinePostNoDataException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + } +} diff --git a/Timeline/Services/Exceptions/TimelinePostNotExistException.cs b/Timeline/Services/Exceptions/TimelinePostNotExistException.cs index bbb9e908..f95dd410 100644 --- a/Timeline/Services/Exceptions/TimelinePostNotExistException.cs +++ b/Timeline/Services/Exceptions/TimelinePostNotExistException.cs @@ -6,7 +6,7 @@ namespace Timeline.Services.Exceptions [Serializable] public class TimelinePostNotExistException : EntityNotExistException { - public TimelinePostNotExistException() { } + public TimelinePostNotExistException() : this(null, null, false, null, null) { } [Obsolete("This has no meaning.")] public TimelinePostNotExistException(string? message) : this(message, null) { } [Obsolete("This has no meaning.")] -- cgit v1.2.3