diff options
author | crupest <crupest@outlook.com> | 2020-06-13 00:28:35 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-06-13 00:28:35 +0800 |
commit | eba8e9698c09b805d8ac2a8f58db93b947ac29e3 (patch) | |
tree | c3bf0aef9d80c19e9a71e9e9aaab16b8364db15b /Timeline/Services/Exceptions | |
parent | cc62df19e5f8aa216660915f46ff290c8eeab1d0 (diff) | |
download | timeline-eba8e9698c09b805d8ac2a8f58db93b947ac29e3.tar.gz timeline-eba8e9698c09b805d8ac2a8f58db93b947ac29e3.tar.bz2 timeline-eba8e9698c09b805d8ac2a8f58db93b947ac29e3.zip |
refactor(back): Fix #100 .
Diffstat (limited to 'Timeline/Services/Exceptions')
6 files changed, 225 insertions, 0 deletions
diff --git a/Timeline/Services/Exceptions/EntityAlreadyExistError.cs b/Timeline/Services/Exceptions/EntityAlreadyExistError.cs new file mode 100644 index 00000000..7db2e860 --- /dev/null +++ b/Timeline/Services/Exceptions/EntityAlreadyExistError.cs @@ -0,0 +1,63 @@ +using System;
+using System.Globalization;
+using System.Text;
+
+namespace Timeline.Services.Exceptions
+{
+ /// <summary>
+ /// Thrown when an entity is already exists.
+ /// </summary>
+ /// <remarks>
+ /// For example, want to create a timeline but a timeline with the same name already exists.
+ /// </remarks>
+ [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/Timeline/Services/Exceptions/EntityNotExistError.cs b/Timeline/Services/Exceptions/EntityNotExistError.cs new file mode 100644 index 00000000..e79496d3 --- /dev/null +++ b/Timeline/Services/Exceptions/EntityNotExistError.cs @@ -0,0 +1,55 @@ +using System;
+using System.Globalization;
+using System.Text;
+
+namespace Timeline.Services.Exceptions
+{
+ /// <summary>
+ /// Thrown when you want to get an entity that does not exist.
+ /// </summary>
+ /// <example>
+ /// For example, you want to get a timeline with given name but it does not exist.
+ /// </example>
+ [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/Timeline/Services/Exceptions/ExceptionMessageHelper.cs b/Timeline/Services/Exceptions/ExceptionMessageHelper.cs new file mode 100644 index 00000000..be3c42a4 --- /dev/null +++ b/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/Timeline/Services/Exceptions/TimelineNotExistException.cs b/Timeline/Services/Exceptions/TimelineNotExistException.cs new file mode 100644 index 00000000..70970b24 --- /dev/null +++ b/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/Timeline/Services/Exceptions/TimelinePostNotExistException.cs b/Timeline/Services/Exceptions/TimelinePostNotExistException.cs new file mode 100644 index 00000000..bbb9e908 --- /dev/null +++ b/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() { }
+ [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; }
+
+ /// <summary>
+ /// True if the post is deleted. False if the post does not exist at all.
+ /// </summary>
+ public bool IsDelete { get; set; }
+ }
+}
diff --git a/Timeline/Services/Exceptions/UserNotExistException.cs b/Timeline/Services/Exceptions/UserNotExistException.cs new file mode 100644 index 00000000..7ef714df --- /dev/null +++ b/Timeline/Services/Exceptions/UserNotExistException.cs @@ -0,0 +1,40 @@ +using System;
+using System.Globalization;
+
+namespace Timeline.Services.Exceptions
+{
+ /// <summary>
+ /// The user requested does not exist.
+ /// </summary>
+ [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) { }
+
+ /// <summary>
+ /// The username of the user that does not exist.
+ /// </summary>
+ public string? Username { get; set; }
+
+ /// <summary>
+ /// The id of the user that does not exist.
+ /// </summary>
+ public long? Id { get; set; }
+ }
+}
|