aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Services/Exceptions/EntityAlreadyExistError.cs
blob: 7db2e86024db2e28adf27ed2666ebf703f02d447 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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; }
    }
}