From 05ccb4d8f1bbe3fb64e117136b4a89bcfb0b0b33 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 27 Oct 2020 19:21:35 +0800 Subject: Split front and back end. --- .../Services/Exceptions/EntityAlreadyExistError.cs | 63 ++++++++++++++++++++++ .../Services/Exceptions/EntityNotExistError.cs | 55 +++++++++++++++++++ .../Services/Exceptions/ExceptionMessageHelper.cs | 13 +++++ .../Timeline/Services/Exceptions/ImageException.cs | 57 ++++++++++++++++++++ .../Exceptions/TimelineNotExistException.cs | 21 ++++++++ .../Exceptions/TimelinePostNoDataException.cs | 15 ++++++ .../Exceptions/TimelinePostNotExistException.cs | 33 ++++++++++++ .../Services/Exceptions/UserNotExistException.cs | 40 ++++++++++++++ 8 files changed, 297 insertions(+) create mode 100644 BackEnd/Timeline/Services/Exceptions/EntityAlreadyExistError.cs create mode 100644 BackEnd/Timeline/Services/Exceptions/EntityNotExistError.cs create mode 100644 BackEnd/Timeline/Services/Exceptions/ExceptionMessageHelper.cs create mode 100644 BackEnd/Timeline/Services/Exceptions/ImageException.cs create mode 100644 BackEnd/Timeline/Services/Exceptions/TimelineNotExistException.cs create mode 100644 BackEnd/Timeline/Services/Exceptions/TimelinePostNoDataException.cs create mode 100644 BackEnd/Timeline/Services/Exceptions/TimelinePostNotExistException.cs create mode 100644 BackEnd/Timeline/Services/Exceptions/UserNotExistException.cs (limited to 'BackEnd/Timeline/Services/Exceptions') diff --git a/BackEnd/Timeline/Services/Exceptions/EntityAlreadyExistError.cs b/BackEnd/Timeline/Services/Exceptions/EntityAlreadyExistError.cs new file mode 100644 index 00000000..7db2e860 --- /dev/null +++ b/BackEnd/Timeline/Services/Exceptions/EntityAlreadyExistError.cs @@ -0,0 +1,63 @@ +using System; +using System.Globalization; +using System.Text; + +namespace Timeline.Services.Exceptions +{ + /// + /// Thrown when an entity is already exists. + /// + /// + /// For example, want to create a timeline but a timeline with the same name already exists. + /// + [Serializable] + public class EntityAlreadyExistException : Exception + { + private readonly string? _entityName; + + public EntityAlreadyExistException() : this(null, null, null, null) { } + + public EntityAlreadyExistException(string? entityName) : this(entityName, null) { } + + public EntityAlreadyExistException(string? entityName, Exception? inner) : this(entityName, null, null, null, inner) { } + + public EntityAlreadyExistException(string? entityName, object? entity = null) : this(entityName, null, entity, null, null) { } + public EntityAlreadyExistException(Type? entityType, object? entity = null) : this(null, entityType, entity, null, null) { } + public EntityAlreadyExistException(string? entityName, Type? entityType, object? entity = null, string? message = null, Exception? inner = null) : base(MakeMessage(entityName, entityType, message), inner) + { + _entityName = entityName; + EntityType = entityType; + Entity = entity; + } + + private static string MakeMessage(string? entityName, Type? entityType, string? message) + { + string? name = entityName ?? (entityType?.Name); + + var result = new StringBuilder(); + + if (name == null) + result.Append(Resources.Services.Exceptions.EntityAlreadyExistErrorDefault); + else + result.AppendFormat(CultureInfo.InvariantCulture, Resources.Services.Exceptions.EntityAlreadyExistError, name); + + if (message != null) + { + result.Append(' '); + result.Append(message); + } + + return result.ToString(); + } + + protected EntityAlreadyExistException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + + public string? EntityName => _entityName ?? (EntityType?.Name); + + public Type? EntityType { get; } + + public object? Entity { get; } + } +} diff --git a/BackEnd/Timeline/Services/Exceptions/EntityNotExistError.cs b/BackEnd/Timeline/Services/Exceptions/EntityNotExistError.cs new file mode 100644 index 00000000..e79496d3 --- /dev/null +++ b/BackEnd/Timeline/Services/Exceptions/EntityNotExistError.cs @@ -0,0 +1,55 @@ +using System; +using System.Globalization; +using System.Text; + +namespace Timeline.Services.Exceptions +{ + /// + /// Thrown when you want to get an entity that does not exist. + /// + /// + /// For example, you want to get a timeline with given name but it does not exist. + /// + [Serializable] + public class EntityNotExistException : Exception + { + public EntityNotExistException() : this(null, null, null, null) { } + public EntityNotExistException(string? entityName) : this(entityName, null, null, null) { } + public EntityNotExistException(Type? entityType) : this(null, entityType, null, null) { } + public EntityNotExistException(string? entityName, Exception? inner) : this(entityName, null, null, inner) { } + public EntityNotExistException(Type? entityType, Exception? inner) : this(null, entityType, null, inner) { } + public EntityNotExistException(string? entityName, Type? entityType, string? message = null, Exception? inner = null) : base(MakeMessage(entityName, entityType, message), inner) + { + EntityName = entityName; + EntityType = entityType; + } + + private static string MakeMessage(string? entityName, Type? entityType, string? message) + { + string? name = entityName ?? (entityType?.Name); + + var result = new StringBuilder(); + + if (name == null) + result.Append(Resources.Services.Exceptions.EntityNotExistErrorDefault); + else + result.AppendFormat(CultureInfo.InvariantCulture, Resources.Services.Exceptions.EntityNotExistError, name); + + if (message != null) + { + result.Append(' '); + result.Append(message); + } + + return result.ToString(); + } + + protected EntityNotExistException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + + public string? EntityName { get; } + + public Type? EntityType { get; } + } +} diff --git a/BackEnd/Timeline/Services/Exceptions/ExceptionMessageHelper.cs b/BackEnd/Timeline/Services/Exceptions/ExceptionMessageHelper.cs new file mode 100644 index 00000000..be3c42a4 --- /dev/null +++ b/BackEnd/Timeline/Services/Exceptions/ExceptionMessageHelper.cs @@ -0,0 +1,13 @@ +namespace Timeline.Services.Exceptions +{ + public static class ExceptionMessageHelper + { + public static string AppendAdditionalMessage(this string origin, string? message) + { + if (message == null) + return origin; + else + return origin + " " + message; + } + } +} diff --git a/BackEnd/Timeline/Services/Exceptions/ImageException.cs b/BackEnd/Timeline/Services/Exceptions/ImageException.cs new file mode 100644 index 00000000..20dd48ae --- /dev/null +++ b/BackEnd/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/BackEnd/Timeline/Services/Exceptions/TimelineNotExistException.cs b/BackEnd/Timeline/Services/Exceptions/TimelineNotExistException.cs new file mode 100644 index 00000000..70970b24 --- /dev/null +++ b/BackEnd/Timeline/Services/Exceptions/TimelineNotExistException.cs @@ -0,0 +1,21 @@ +using System; +using System.Globalization; + +namespace Timeline.Services.Exceptions +{ + [Serializable] + public class TimelineNotExistException : EntityNotExistException + { + public TimelineNotExistException() : this(null, null) { } + public TimelineNotExistException(string? timelineName) : this(timelineName, null) { } + public TimelineNotExistException(string? timelineName, Exception? inner) : this(timelineName, null, inner) { } + public TimelineNotExistException(string? timelineName, string? message, Exception? inner = null) + : base(EntityNames.Timeline, null, string.Format(CultureInfo.InvariantCulture, Resources.Services.Exceptions.TimelineNotExistException, timelineName ?? "").AppendAdditionalMessage(message), inner) { TimelineName = timelineName; } + + protected TimelineNotExistException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + + public string? TimelineName { get; set; } + } +} diff --git a/BackEnd/Timeline/Services/Exceptions/TimelinePostNoDataException.cs b/BackEnd/Timeline/Services/Exceptions/TimelinePostNoDataException.cs new file mode 100644 index 00000000..c4b6bf62 --- /dev/null +++ b/BackEnd/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/BackEnd/Timeline/Services/Exceptions/TimelinePostNotExistException.cs b/BackEnd/Timeline/Services/Exceptions/TimelinePostNotExistException.cs new file mode 100644 index 00000000..f95dd410 --- /dev/null +++ b/BackEnd/Timeline/Services/Exceptions/TimelinePostNotExistException.cs @@ -0,0 +1,33 @@ +using System; +using System.Globalization; + +namespace Timeline.Services.Exceptions +{ + [Serializable] + public class TimelinePostNotExistException : EntityNotExistException + { + 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.")] + public TimelinePostNotExistException(string? message, Exception? inner) : this(null, null, false, message, inner) { } + protected TimelinePostNotExistException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + + public TimelinePostNotExistException(string? timelineName, long? id, bool isDelete, string? message = null, Exception? inner = null) : base(EntityNames.TimelinePost, null, MakeMessage(timelineName, id, isDelete).AppendAdditionalMessage(message), inner) { TimelineName = timelineName; Id = id; IsDelete = isDelete; } + + private static string MakeMessage(string? timelineName, long? id, bool isDelete) + { + return string.Format(CultureInfo.InvariantCulture, isDelete ? Resources.Services.Exceptions.TimelinePostNotExistExceptionDeleted : Resources.Services.Exceptions.TimelinePostNotExistException, timelineName ?? "", id); + } + + public string? TimelineName { get; set; } + public long? Id { get; set; } + + /// + /// True if the post is deleted. False if the post does not exist at all. + /// + public bool IsDelete { get; set; } + } +} diff --git a/BackEnd/Timeline/Services/Exceptions/UserNotExistException.cs b/BackEnd/Timeline/Services/Exceptions/UserNotExistException.cs new file mode 100644 index 00000000..7ef714df --- /dev/null +++ b/BackEnd/Timeline/Services/Exceptions/UserNotExistException.cs @@ -0,0 +1,40 @@ +using System; +using System.Globalization; + +namespace Timeline.Services.Exceptions +{ + /// + /// The user requested does not exist. + /// + [Serializable] + public class UserNotExistException : EntityNotExistException + { + public UserNotExistException() : this(null, null, null, null) { } + public UserNotExistException(string? username, Exception? inner) : this(username, null, null, inner) { } + + public UserNotExistException(string? username) : this(username, null, null, null) { } + + public UserNotExistException(long id) : this(null, id, null, null) { } + + public UserNotExistException(string? username, long? id, string? message, Exception? inner) : base(EntityNames.User, null, + string.Format(CultureInfo.InvariantCulture, Resources.Services.Exceptions.UserNotExistException, username ?? "", id).AppendAdditionalMessage(message), inner) + { + Username = username; + Id = id; + } + + protected UserNotExistException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + + /// + /// The username of the user that does not exist. + /// + public string? Username { get; set; } + + /// + /// The id of the user that does not exist. + /// + public long? Id { get; set; } + } +} -- cgit v1.2.3