aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-06-13 00:28:35 +0800
committercrupest <crupest@outlook.com>2020-06-13 00:28:35 +0800
commiteba8e9698c09b805d8ac2a8f58db93b947ac29e3 (patch)
treec3bf0aef9d80c19e9a71e9e9aaab16b8364db15b
parentcc62df19e5f8aa216660915f46ff290c8eeab1d0 (diff)
downloadtimeline-eba8e9698c09b805d8ac2a8f58db93b947ac29e3.tar.gz
timeline-eba8e9698c09b805d8ac2a8f58db93b947ac29e3.tar.bz2
timeline-eba8e9698c09b805d8ac2a8f58db93b947ac29e3.zip
refactor(back): Fix #100 .
-rw-r--r--Timeline/Controllers/TimelineController.cs3
-rw-r--r--Timeline/Controllers/TokenController.cs1
-rw-r--r--Timeline/Controllers/UserAvatarController.cs1
-rw-r--r--Timeline/Controllers/UserController.cs5
-rw-r--r--Timeline/Filters/Timeline.cs2
-rw-r--r--Timeline/Properties/launchSettings.json38
-rw-r--r--Timeline/Resources/Services/Exception.Designer.cs36
-rw-r--r--Timeline/Resources/Services/Exception.resx12
-rw-r--r--Timeline/Resources/Services/Exceptions.Designer.cs135
-rw-r--r--Timeline/Resources/Services/Exceptions.resx124
-rw-r--r--Timeline/Services/ConflictException.cs21
-rw-r--r--Timeline/Services/EntityNames.cs14
-rw-r--r--Timeline/Services/Exceptions/EntityAlreadyExistError.cs63
-rw-r--r--Timeline/Services/Exceptions/EntityNotExistError.cs55
-rw-r--r--Timeline/Services/Exceptions/ExceptionMessageHelper.cs13
-rw-r--r--Timeline/Services/Exceptions/TimelineNotExistException.cs21
-rw-r--r--Timeline/Services/Exceptions/TimelinePostNotExistException.cs33
-rw-r--r--Timeline/Services/Exceptions/UserNotExistException.cs40
-rw-r--r--Timeline/Services/TimelineNotExistException.cs19
-rw-r--r--Timeline/Services/TimelinePostNotExistException.cs28
-rw-r--r--Timeline/Services/TimelineService.cs9
-rw-r--r--Timeline/Services/UserNotExistException.cs41
-rw-r--r--Timeline/Services/UserService.cs3
-rw-r--r--Timeline/Services/UserTokenManager.cs1
-rw-r--r--Timeline/Timeline.csproj9
25 files changed, 542 insertions, 185 deletions
diff --git a/Timeline/Controllers/TimelineController.cs b/Timeline/Controllers/TimelineController.cs
index 5b894e26..6c7cfa95 100644
--- a/Timeline/Controllers/TimelineController.cs
+++ b/Timeline/Controllers/TimelineController.cs
@@ -13,6 +13,7 @@ using Timeline.Models;
using Timeline.Models.Http;
using Timeline.Models.Validation;
using Timeline.Services;
+using Timeline.Services.Exceptions;
namespace Timeline.Controllers
{
@@ -283,7 +284,7 @@ namespace Timeline.Controllers
var result = _mapper.Map<TimelineInfo>(timeline);
return result;
}
- catch (ConflictException)
+ catch (EntityAlreadyExistException e) when (e.EntityName == EntityNames.Timeline)
{
return BadRequest(ErrorResponse.TimelineController.NameConflict());
}
diff --git a/Timeline/Controllers/TokenController.cs b/Timeline/Controllers/TokenController.cs
index 1fb0b17a..cd67225c 100644
--- a/Timeline/Controllers/TokenController.cs
+++ b/Timeline/Controllers/TokenController.cs
@@ -8,6 +8,7 @@ using System.Threading.Tasks;
using Timeline.Helpers;
using Timeline.Models.Http;
using Timeline.Services;
+using Timeline.Services.Exceptions;
using static Timeline.Resources.Controllers.TokenController;
namespace Timeline.Controllers
diff --git a/Timeline/Controllers/UserAvatarController.cs b/Timeline/Controllers/UserAvatarController.cs
index 4062837b..b2e2e852 100644
--- a/Timeline/Controllers/UserAvatarController.cs
+++ b/Timeline/Controllers/UserAvatarController.cs
@@ -10,6 +10,7 @@ using Timeline.Helpers;
using Timeline.Models.Http;
using Timeline.Models.Validation;
using Timeline.Services;
+using Timeline.Services.Exceptions;
using static Timeline.Resources.Controllers.UserAvatarController;
namespace Timeline.Controllers
diff --git a/Timeline/Controllers/UserController.cs b/Timeline/Controllers/UserController.cs
index 0bc8bcda..c8c1e610 100644
--- a/Timeline/Controllers/UserController.cs
+++ b/Timeline/Controllers/UserController.cs
@@ -11,6 +11,7 @@ using Timeline.Models;
using Timeline.Models.Http;
using Timeline.Models.Validation;
using Timeline.Services;
+using Timeline.Services.Exceptions;
using static Timeline.Resources.Controllers.UserController;
using static Timeline.Resources.Messages;
@@ -70,7 +71,7 @@ namespace Timeline.Controllers
_logger.LogInformation(e, Log.Format(LogPatchUserNotExist, ("Username", username)));
return NotFound(ErrorResponse.UserCommon.NotExist());
}
- catch (ConflictException)
+ catch (EntityAlreadyExistException e) when (e.EntityName == EntityNames.User)
{
return BadRequest(ErrorResponse.UserController.UsernameConflict());
}
@@ -116,7 +117,7 @@ namespace Timeline.Controllers
var user = await _userService.CreateUser(_mapper.Map<User>(body));
return Ok(ConvertToUserInfo(user));
}
- catch (ConflictException)
+ catch (EntityAlreadyExistException e) when (e.EntityName == EntityNames.User)
{
return BadRequest(ErrorResponse.UserController.UsernameConflict());
}
diff --git a/Timeline/Filters/Timeline.cs b/Timeline/Filters/Timeline.cs
index 76e8d751..90b87223 100644
--- a/Timeline/Filters/Timeline.cs
+++ b/Timeline/Filters/Timeline.cs
@@ -1,7 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Timeline.Models.Http;
-using Timeline.Services;
+using Timeline.Services.Exceptions;
namespace Timeline.Filters
{
diff --git a/Timeline/Properties/launchSettings.json b/Timeline/Properties/launchSettings.json
index daa4b011..4baafa62 100644
--- a/Timeline/Properties/launchSettings.json
+++ b/Timeline/Properties/launchSettings.json
@@ -1,19 +1,19 @@
-{
- "profiles": {
- "Timeline": {
- "commandName": "Project",
- "environmentVariables": {
- "ASPNETCORE_ENVIRONMENT": "Development",
- "ASPNETCORE_FRONTENDPROXYONLY": "true",
- "ASPNETCORE_WORKDIR": "D:\\timeline-development"
- }
- },
- "Timeline-Staging": {
- "commandName": "Project",
- "environmentVariables": {
- "ASPNETCORE_ENVIRONMENT": "Staging",
- "ASPNETCORE_WORKDIR": "D:\\timeline-development"
- }
- }
- }
-}
+{
+ "profiles": {
+ "Timeline": {
+ "commandName": "Project",
+ "environmentVariables": {
+ "ASPNETCORE_ENVIRONMENT": "Development",
+ "ASPNETCORE_FRONTENDPROXYONLY": "true",
+ "ASPNETCORE_WORKDIR": "D:\\timeline-development"
+ }
+ },
+ "Timeline-Staging": {
+ "commandName": "Project",
+ "environmentVariables": {
+ "ASPNETCORE_ENVIRONMENT": "Staging",
+ "ASPNETCORE_WORKDIR": "D:\\timeline-development"
+ }
+ }
+ }
+}
diff --git a/Timeline/Resources/Services/Exception.Designer.cs b/Timeline/Resources/Services/Exception.Designer.cs
index 0c721d92..889aa1a7 100644
--- a/Timeline/Resources/Services/Exception.Designer.cs
+++ b/Timeline/Resources/Services/Exception.Designer.cs
@@ -70,15 +70,6 @@ namespace Timeline.Resources.Services {
}
/// <summary>
- /// Looks up a localized string similar to A present resource conflicts with the given resource..
- /// </summary>
- internal static string ConflictException {
- get {
- return ResourceManager.GetString("ConflictException", resourceCulture);
- }
- }
-
- /// <summary>
/// Looks up a localized string similar to The hashes password is of bad format. It might not be created by server..
/// </summary>
internal static string HashedPasswordBadFromatException {
@@ -259,33 +250,6 @@ namespace Timeline.Resources.Services {
}
/// <summary>
- /// Looks up a localized string similar to Timeline does not exist. If this is a personal timeline, it means the user does not exist and inner exception should be a UserNotExistException..
- /// </summary>
- internal static string TimelineNotExistException {
- get {
- return ResourceManager.GetString("TimelineNotExistException", resourceCulture);
- }
- }
-
- /// <summary>
- /// Looks up a localized string similar to The timeline post does not exist. You can&apos;t do operation on it..
- /// </summary>
- internal static string TimelinePostNotExistException {
- get {
- return ResourceManager.GetString("TimelinePostNotExistException", resourceCulture);
- }
- }
-
- /// <summary>
- /// Looks up a localized string similar to The user does not exist..
- /// </summary>
- internal static string UserNotExistException {
- get {
- return ResourceManager.GetString("UserNotExistException", resourceCulture);
- }
- }
-
- /// <summary>
/// Looks up a localized string similar to The token is of bad format, which means it may not be created by the server..
/// </summary>
internal static string UserTokenBadFormatException {
diff --git a/Timeline/Resources/Services/Exception.resx b/Timeline/Resources/Services/Exception.resx
index 660e5b3d..d455f0fc 100644
--- a/Timeline/Resources/Services/Exception.resx
+++ b/Timeline/Resources/Services/Exception.resx
@@ -120,9 +120,6 @@
<data name="BadPasswordException" xml:space="preserve">
<value>The password is wrong.</value>
</data>
- <data name="ConflictException" xml:space="preserve">
- <value>A present resource conflicts with the given resource.</value>
- </data>
<data name="HashedPasswordBadFromatException" xml:space="preserve">
<value>The hashes password is of bad format. It might not be created by server.</value>
</data>
@@ -183,15 +180,6 @@
<data name="PasswordBadFormatException" xml:space="preserve">
<value>Password is of bad format.</value>
</data>
- <data name="TimelineNotExistException" xml:space="preserve">
- <value>Timeline does not exist. If this is a personal timeline, it means the user does not exist and inner exception should be a UserNotExistException.</value>
- </data>
- <data name="TimelinePostNotExistException" xml:space="preserve">
- <value>The timeline post does not exist. You can't do operation on it.</value>
- </data>
- <data name="UserNotExistException" xml:space="preserve">
- <value>The user does not exist.</value>
- </data>
<data name="UserTokenBadFormatException" xml:space="preserve">
<value>The token is of bad format, which means it may not be created by the server.</value>
</data>
diff --git a/Timeline/Resources/Services/Exceptions.Designer.cs b/Timeline/Resources/Services/Exceptions.Designer.cs
new file mode 100644
index 00000000..84439716
--- /dev/null
+++ b/Timeline/Resources/Services/Exceptions.Designer.cs
@@ -0,0 +1,135 @@
+//------------------------------------------------------------------------------
+// <auto-generated>
+// This code was generated by a tool.
+// Runtime Version:4.0.30319.42000
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+// </auto-generated>
+//------------------------------------------------------------------------------
+
+namespace Timeline.Resources.Services {
+ using System;
+
+
+ /// <summary>
+ /// A strongly-typed resource class, for looking up localized strings, etc.
+ /// </summary>
+ // This class was auto-generated by the StronglyTypedResourceBuilder
+ // class via a tool like ResGen or Visual Studio.
+ // To add or remove a member, edit your .ResX file then rerun ResGen
+ // with the /str option, or rebuild your VS project.
+ [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")]
+ [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
+ [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
+ internal class Exceptions {
+
+ private static global::System.Resources.ResourceManager resourceMan;
+
+ private static global::System.Globalization.CultureInfo resourceCulture;
+
+ [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
+ internal Exceptions() {
+ }
+
+ /// <summary>
+ /// Returns the cached ResourceManager instance used by this class.
+ /// </summary>
+ [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
+ internal static global::System.Resources.ResourceManager ResourceManager {
+ get {
+ if (object.ReferenceEquals(resourceMan, null)) {
+ global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Timeline.Resources.Services.Exceptions", typeof(Exceptions).Assembly);
+ resourceMan = temp;
+ }
+ return resourceMan;
+ }
+ }
+
+ /// <summary>
+ /// Overrides the current thread's CurrentUICulture property for all
+ /// resource lookups using this strongly typed resource class.
+ /// </summary>
+ [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
+ internal static global::System.Globalization.CultureInfo Culture {
+ get {
+ return resourceCulture;
+ }
+ set {
+ resourceCulture = value;
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to A entity of type &quot;{0}&quot; already exists..
+ /// </summary>
+ internal static string EntityAlreadyExistError {
+ get {
+ return ResourceManager.GetString("EntityAlreadyExistError", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to The entity already exists..
+ /// </summary>
+ internal static string EntityAlreadyExistErrorDefault {
+ get {
+ return ResourceManager.GetString("EntityAlreadyExistErrorDefault", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to The required entity of type &quot;{0}&quot; does not exist..
+ /// </summary>
+ internal static string EntityNotExistError {
+ get {
+ return ResourceManager.GetString("EntityNotExistError", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to The required entity does not exist..
+ /// </summary>
+ internal static string EntityNotExistErrorDefault {
+ get {
+ return ResourceManager.GetString("EntityNotExistErrorDefault", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Request timeline name is &quot;{0}&quot;. If this is a personal timeline whose name starts with &apos;@&apos;, it means the user does not exist and inner exception should be a UserNotExistException..
+ /// </summary>
+ internal static string TimelineNotExistException {
+ get {
+ return ResourceManager.GetString("TimelineNotExistException", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Request timeline name is &quot;{0}&quot;. Request timeline post id is &quot;{1}&quot;..
+ /// </summary>
+ internal static string TimelinePostNotExistException {
+ get {
+ return ResourceManager.GetString("TimelinePostNotExistException", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Request timeline name is &quot;{0}&quot;. Request timeline post id is &quot;{1}&quot;. The post does not exist because it has been deleted..
+ /// </summary>
+ internal static string TimelinePostNotExistExceptionDeleted {
+ get {
+ return ResourceManager.GetString("TimelinePostNotExistExceptionDeleted", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Request username is &quot;{0}&quot;. Request id is &quot;{1}&quot;..
+ /// </summary>
+ internal static string UserNotExistException {
+ get {
+ return ResourceManager.GetString("UserNotExistException", resourceCulture);
+ }
+ }
+ }
+}
diff --git a/Timeline/Resources/Services/Exceptions.resx b/Timeline/Resources/Services/Exceptions.resx
new file mode 100644
index 00000000..b064fd44
--- /dev/null
+++ b/Timeline/Resources/Services/Exceptions.resx
@@ -0,0 +1,124 @@
+<?xml version="1.0" encoding="utf-8"?>
+<root>
+ <!--
+ Microsoft ResX Schema
+
+ Version 1.3
+
+ The primary goals of this format is to allow a simple XML format
+ that is mostly human readable. The generation and parsing of the
+ various data types are done through the TypeConverter classes
+ associated with the data types.
+
+ Example:
+
+ ... ado.net/XML headers & schema ...
+ <resheader name="resmimetype">text/microsoft-resx</resheader>
+ <resheader name="version">1.3</resheader>
+ <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
+ <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
+ <data name="Name1">this is my long string</data>
+ <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
+ <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
+ [base64 mime encoded serialized .NET Framework object]
+ </data>
+ <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
+ [base64 mime encoded string representing a byte array form of the .NET Framework object]
+ </data>
+
+ There are any number of "resheader" rows that contain simple
+ name/value pairs.
+
+ Each data row contains a name, and value. The row also contains a
+ type or mimetype. Type corresponds to a .NET class that support
+ text/value conversion through the TypeConverter architecture.
+ Classes that don't support this are serialized and stored with the
+ mimetype set.
+
+ The mimetype is used for serialized objects, and tells the
+ ResXResourceReader how to depersist the object. This is currently not
+ extensible. For a given mimetype the value must be set accordingly:
+
+ Note - application/x-microsoft.net.object.binary.base64 is the format
+ that the ResXResourceWriter will generate, however the reader can
+ read any of the formats listed below.
+
+ mimetype: application/x-microsoft.net.object.binary.base64
+ value : The object must be serialized with
+ : System.Serialization.Formatters.Binary.BinaryFormatter
+ : and then encoded with base64 encoding.
+
+ mimetype: application/x-microsoft.net.object.soap.base64
+ value : The object must be serialized with
+ : System.Runtime.Serialization.Formatters.Soap.SoapFormatter
+ : and then encoded with base64 encoding.
+
+ mimetype: application/x-microsoft.net.object.bytearray.base64
+ value : The object must be serialized into a byte array
+ : using a System.ComponentModel.TypeConverter
+ : and then encoded with base64 encoding.
+ -->
+ <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
+ <xsd:element name="root" msdata:IsDataSet="true">
+ <xsd:complexType>
+ <xsd:choice maxOccurs="unbounded">
+ <xsd:element name="data">
+ <xsd:complexType>
+ <xsd:sequence>
+ <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+ <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
+ </xsd:sequence>
+ <xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
+ <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
+ <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
+ </xsd:complexType>
+ </xsd:element>
+ <xsd:element name="resheader">
+ <xsd:complexType>
+ <xsd:sequence>
+ <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+ </xsd:sequence>
+ <xsd:attribute name="name" type="xsd:string" use="required" />
+ </xsd:complexType>
+ </xsd:element>
+ </xsd:choice>
+ </xsd:complexType>
+ </xsd:element>
+ </xsd:schema>
+ <resheader name="resmimetype">
+ <value>text/microsoft-resx</value>
+ </resheader>
+ <resheader name="version">
+ <value>1.3</value>
+ </resheader>
+ <resheader name="reader">
+ <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+ </resheader>
+ <resheader name="writer">
+ <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+ </resheader>
+ <data name="EntityNotExistError" xml:space="preserve">
+ <value>The required entity of type "{0}" does not exist.</value>
+ </data>
+ <data name="EntityAlreadyExistErrorDefault" xml:space="preserve">
+ <value>The entity already exists.</value>
+ </data>
+ <data name="EntityAlreadyExistError" xml:space="preserve">
+ <value>A entity of type "{0}" already exists.</value>
+ </data>
+ <data name="EntityNotExistErrorDefault" xml:space="preserve">
+ <value>The required entity does not exist.</value>
+ </data>
+ <data name="TimelineNotExistException" xml:space="preserve">
+ <value>Request timeline name is "{0}". If this is a personal timeline whose name starts with '@', it means the user does not exist and inner exception should be a UserNotExistException.</value>
+ </data>
+ <data name="TimelinePostNotExistExceptionDeleted" xml:space="preserve">
+ <value>Request timeline name is "{0}". Request timeline post id is "{1}". The post does not exist because it has been deleted.</value>
+ </data>
+ <data name="TimelinePostNotExistException" xml:space="preserve">
+ <value>Request timeline name is "{0}". Request timeline post id is "{1}".</value>
+ </data>
+ <data name="UserNotExistException" xml:space="preserve">
+ <value>Request username is "{0}". Request id is "{1}".</value>
+ </data>
+</root> \ No newline at end of file
diff --git a/Timeline/Services/ConflictException.cs b/Timeline/Services/ConflictException.cs
deleted file mode 100644
index 6ede183a..00000000
--- a/Timeline/Services/ConflictException.cs
+++ /dev/null
@@ -1,21 +0,0 @@
-using System;
-
-namespace Timeline.Services
-{
- /// <summary>
- /// Thrown when a resource already exists and conflicts with the given resource.
- /// </summary>
- /// <remarks>
- /// For example a username already exists and conflicts with the given username.
- /// </remarks>
- [Serializable]
- public class ConflictException : Exception
- {
- public ConflictException() : base(Resources.Services.Exception.ConflictException) { }
- public ConflictException(string message) : base(message) { }
- public ConflictException(string message, Exception inner) : base(message, inner) { }
- protected ConflictException(
- System.Runtime.Serialization.SerializationInfo info,
- System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
- }
-}
diff --git a/Timeline/Services/EntityNames.cs b/Timeline/Services/EntityNames.cs
new file mode 100644
index 00000000..0ce1de3b
--- /dev/null
+++ b/Timeline/Services/EntityNames.cs
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace Timeline.Services
+{
+ public static class EntityNames
+ {
+ public const string User = "User";
+ public const string Timeline = "Timeline";
+ public const string TimelinePost = "TimelinePost";
+ }
+}
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; }
+ }
+}
diff --git a/Timeline/Services/TimelineNotExistException.cs b/Timeline/Services/TimelineNotExistException.cs
deleted file mode 100644
index 6dfd0bab..00000000
--- a/Timeline/Services/TimelineNotExistException.cs
+++ /dev/null
@@ -1,19 +0,0 @@
-using System;
-
-namespace Timeline.Services
-{
- [Serializable]
- public class TimelineNotExistException : Exception
- {
- public TimelineNotExistException() : base(Resources.Services.Exception.TimelineNotExistException) { }
- public TimelineNotExistException(string name)
- : base(Resources.Services.Exception.TimelineNotExistException) { Name = name; }
- public TimelineNotExistException(string name, Exception inner)
- : base(Resources.Services.Exception.TimelineNotExistException, inner) { Name = name; }
- protected TimelineNotExistException(
- System.Runtime.Serialization.SerializationInfo info,
- System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
-
- public string? Name { get; set; }
- }
-}
diff --git a/Timeline/Services/TimelinePostNotExistException.cs b/Timeline/Services/TimelinePostNotExistException.cs
deleted file mode 100644
index c542e63e..00000000
--- a/Timeline/Services/TimelinePostNotExistException.cs
+++ /dev/null
@@ -1,28 +0,0 @@
-using System;
-
-namespace Timeline.Services
-{
- [Serializable]
- public class TimelinePostNotExistException : Exception
- {
- public TimelinePostNotExistException() { }
- public TimelinePostNotExistException(string message) : base(message) { }
- public TimelinePostNotExistException(string message, Exception inner) : base(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 = false) : base(Resources.Services.Exception.TimelinePostNotExistException) { TimelineName = timelineName; Id = id; IsDelete = isDelete; }
-
- public TimelinePostNotExistException(string timelineName, long id, bool isDelete, string message) : base(message) { TimelineName = timelineName; Id = id; IsDelete = isDelete; }
-
- public TimelinePostNotExistException(string timelineName, long id, bool isDelete, string message, Exception inner) : base(message, inner) { TimelineName = timelineName; Id = id; IsDelete = isDelete; }
-
- 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; } = false;
- }
-}
diff --git a/Timeline/Services/TimelineService.cs b/Timeline/Services/TimelineService.cs
index aecfeeec..a473ae66 100644
--- a/Timeline/Services/TimelineService.cs
+++ b/Timeline/Services/TimelineService.cs
@@ -10,6 +10,7 @@ using Timeline.Entities;
using Timeline.Helpers;
using Timeline.Models;
using Timeline.Models.Validation;
+using Timeline.Services.Exceptions;
using static Timeline.Resources.Services.TimelineService;
namespace Timeline.Services
@@ -428,7 +429,7 @@ namespace Timeline.Services
var postEntity = await Database.TimelinePosts.Where(p => p.TimelineId == timelineId && p.LocalId == postId).SingleOrDefaultAsync();
if (postEntity == null)
- throw new TimelinePostNotExistException(name, postId);
+ throw new TimelinePostNotExistException(name, postId, false);
if (postEntity.Content == null)
throw new TimelinePostNotExistException(name, postId, true);
@@ -450,7 +451,7 @@ namespace Timeline.Services
var postEntity = await Database.TimelinePosts.Where(p => p.TimelineId == timelineId && p.LocalId == postId).SingleOrDefaultAsync();
if (postEntity == null)
- throw new TimelinePostNotExistException(name, postId);
+ throw new TimelinePostNotExistException(name, postId, false);
if (postEntity.Content == null)
throw new TimelinePostNotExistException(name, postId, true);
@@ -586,7 +587,7 @@ namespace Timeline.Services
var post = await Database.TimelinePosts.Where(p => p.TimelineId == timelineId && p.LocalId == id).SingleOrDefaultAsync();
if (post == null || post.Content == null)
- throw new TimelinePostNotExistException(name, id);
+ throw new TimelinePostNotExistException(name, id, false);
string? dataTag = null;
@@ -977,7 +978,7 @@ namespace Timeline.Services
var conflict = await _database.Timelines.AnyAsync(t => t.Name == name);
if (conflict)
- throw new ConflictException(ExceptionTimelineNameConflict);
+ throw new EntityAlreadyExistException(EntityNames.Timeline, null, ExceptionTimelineNameConflict);
var newEntity = new TimelineEntity
{
diff --git a/Timeline/Services/UserNotExistException.cs b/Timeline/Services/UserNotExistException.cs
deleted file mode 100644
index fd0b5ecf..00000000
--- a/Timeline/Services/UserNotExistException.cs
+++ /dev/null
@@ -1,41 +0,0 @@
-using System;
-using Timeline.Helpers;
-
-namespace Timeline.Services
-{
- /// <summary>
- /// The user requested does not exist.
- /// </summary>
- [Serializable]
- public class UserNotExistException : Exception
- {
- public UserNotExistException() : base(Resources.Services.Exception.UserNotExistException) { }
- public UserNotExistException(string message, Exception inner) : base(message, inner) { }
-
- public UserNotExistException(string username)
- : base(Log.Format(Resources.Services.Exception.UserNotExistException, ("Username", username)))
- {
- Username = username;
- }
-
- public UserNotExistException(long id)
- : base(Log.Format(Resources.Services.Exception.UserNotExistException, ("Id", id)))
- {
- 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; }
- }
-}
diff --git a/Timeline/Services/UserService.cs b/Timeline/Services/UserService.cs
index e0a5ab50..c20b180c 100644
--- a/Timeline/Services/UserService.cs
+++ b/Timeline/Services/UserService.cs
@@ -8,6 +8,7 @@ using Timeline.Entities;
using Timeline.Helpers;
using Timeline.Models;
using Timeline.Models.Validation;
+using Timeline.Services.Exceptions;
using static Timeline.Resources.Services.UserService;
namespace Timeline.Services
@@ -197,7 +198,7 @@ namespace Timeline.Services
private static void ThrowUsernameConflict()
{
- throw new ConflictException(ExceptionUsernameConflict);
+ throw new EntityAlreadyExistException(EntityNames.User, ExceptionUsernameConflict);
}
private static User CreateUserFromEntity(UserEntity entity)
diff --git a/Timeline/Services/UserTokenManager.cs b/Timeline/Services/UserTokenManager.cs
index 6decf8f9..a016ff96 100644
--- a/Timeline/Services/UserTokenManager.cs
+++ b/Timeline/Services/UserTokenManager.cs
@@ -2,6 +2,7 @@
using System;
using System.Threading.Tasks;
using Timeline.Models;
+using Timeline.Services.Exceptions;
namespace Timeline.Services
{
diff --git a/Timeline/Timeline.csproj b/Timeline/Timeline.csproj
index 4f5c0f1f..55acd805 100644
--- a/Timeline/Timeline.csproj
+++ b/Timeline/Timeline.csproj
@@ -149,6 +149,11 @@
<AutoGen>True</AutoGen>
<DependentUpon>Exception.resx</DependentUpon>
</Compile>
+ <Compile Update="Resources\Services\Exceptions.Designer.cs">
+ <DesignTime>True</DesignTime>
+ <AutoGen>True</AutoGen>
+ <DependentUpon>Exceptions.resx</DependentUpon>
+ </Compile>
<Compile Update="Resources\Services\TimelineService.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
@@ -237,6 +242,10 @@
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Exception.Designer.cs</LastGenOutput>
</EmbeddedResource>
+ <EmbeddedResource Update="Resources\Services\Exceptions.resx">
+ <Generator>ResXFileCodeGenerator</Generator>
+ <LastGenOutput>Exceptions.Designer.cs</LastGenOutput>
+ </EmbeddedResource>
<EmbeddedResource Update="Resources\Services\TimelineService.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>TimelineService.Designer.cs</LastGenOutput>