aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Services/Imaging/ImageException.cs
blob: 18c8a4d39ae57cc45c6aa5580361c9372db37397 (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;

namespace Timeline.Services.Imaging
{
    [Serializable]
    public class ImageException : Exception
    {
        public enum ErrorReason
        {
            /// <summary>
            /// Decoding image failed.
            /// </summary>
            CantDecode,
            /// <summary>
            /// Decoding succeeded but the real type is not the specified type.
            /// </summary>
            UnmatchedFormat,
            /// <summary>
            /// Image is not of required size.
            /// </summary>
            BadSize,
            /// <summary>
            /// Other unknown errer.
            /// </summary>
            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, string? realType, Exception? inner) : this(error, data, requestType, realType, null, inner) { }
        public ImageException(ErrorReason error, byte[]? data, string? requestType = null, string? realType = null, string? message = null, Exception? inner = null) : base(message ?? MakeMessage(error), 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.CurrentCulture, Resource.ExceptionImage, reason switch
            {
                ErrorReason.CantDecode => Resource.ExceptionImageReasonCantDecode,
                ErrorReason.UnmatchedFormat => Resource.ExceptionImageReasonUnmatchedFormat,
                ErrorReason.BadSize => Resource.ExceptionImageReasonBadSize,
                _ => Resource.ExceptionImageReasonUnknownError
            });

        public ErrorReason Error { get; }
        public byte[]? ImageData { get; }
        public string? RequestType { get; }
        // This field will be null if decoding failed.
        public string? RealType { get; }
    }
}