aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Services/Exceptions/EntityNotExistError.cs
blob: e79496d358cf288825167c72062b43fa0e29983c (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
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; }
    }
}