aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Services/ImageException.cs
blob: c6126aa370be393630b6f8e61056fdacc47bc539 (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
using System;
using System.Globalization;

namespace Timeline.Services
{
    [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 a square.
            /// </summary>
            NotSquare
        }

        public ImageException() : base(MakeMessage(null)) { }
        public ImageException(string message) : base(message) { }
        public ImageException(string message, Exception inner) : base(message, inner) { }

        public ImageException(ErrorReason error, byte[]? data = null, string? requestType = null, string? realType = null) : base(MakeMessage(error)) { Error = error; ImageData = data; RequestType = requestType; RealType = realType; }
        public ImageException(Exception inner, ErrorReason error, byte[]? data = null, string? requestType = null, string? realType = null) : base(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.InvariantCulture, Resources.Services.Exception.ImageException, reason switch
            {
                ErrorReason.CantDecode => Resources.Services.Exception.ImageExceptionCantDecode,
                ErrorReason.UnmatchedFormat => Resources.Services.Exception.ImageExceptionUnmatchedFormat,
                ErrorReason.NotSquare => Resources.Services.Exception.ImageExceptionBadSize,
                _ => Resources.Services.Exception.ImageExceptionUnknownError
            });

        public ErrorReason? Error { get; }
#pragma warning disable CA1819 // Properties should not return arrays
        public byte[]? ImageData { get; }
#pragma warning restore CA1819 // Properties should not return arrays
        public string? RequestType { get; }

        // This field will be null if decoding failed.
        public string? RealType { get; }
    }
}