D bindings to the GraphicsMagick library.
修订版 | 154f374257c8d9b73268853afff86e734a357f15 (tree) |
---|---|
时间 | 2023-07-23 13:07:43 |
作者 | Mio <stigma@disr...> |
Commiter | Mio |
[magickd] Update Exception classes
Future proofing by separating the API exceptions (from GraphicsMagick)
vs any errors that MagickD might throw itself.
@@ -23,36 +23,115 @@ | ||
23 | 23 | */ |
24 | 24 | module magickd.exception; |
25 | 25 | |
26 | -import core.stdc.string : strlen; | |
26 | +import std.string : fromStringz; | |
27 | 27 | |
28 | -import std.algorithm.mutation : copy; | |
28 | +import graphicsmagick_c.magick.error : ExceptionInfo; | |
29 | 29 | |
30 | -import graphicsmagick_c.magick.error; | |
30 | +/// | |
31 | +/// Base exception class encompassing all MagickD exceptions. | |
32 | +/// | |
33 | +/// Author: Mio | |
34 | +/// See_Also: MagickAPIException | |
35 | +/// | |
36 | +class MagickException : Exception | |
37 | +{ | |
38 | +package(magickd): | |
31 | 39 | |
32 | -class MagickException : Exception { | |
33 | - public ExceptionType severity; | |
34 | - public string description; | |
40 | + /// | |
41 | + /// Construct an exception | |
42 | + /// | |
43 | + public this(string msg) | |
44 | + { | |
45 | + super(msg); | |
46 | + } | |
47 | +} | |
48 | + | |
49 | +/// | |
50 | +/// Encapsulates a GraphicsMagick ExceptionInfo structure. | |
51 | +/// | |
52 | +/// Used whenever a MagickWand/PixelWand/DrawingWand throw an | |
53 | +/// exception in the C API. | |
54 | +/// | |
55 | +/// Author: Mio | |
56 | +/// | |
57 | +class MagickAPIException : MagickException | |
58 | +{ | |
59 | +package(magickd): | |
60 | + | |
61 | + /// | |
62 | + /// Descibes the exception that has occurred. | |
63 | + /// | |
64 | + private string m_description; | |
65 | + | |
66 | + /// | |
67 | + /// Reason for the exception being thrown. | |
68 | + /// | |
69 | + private string m_reason; | |
35 | 70 | |
36 | - this(ExceptionInfo ei) { | |
37 | - this.severity = ei.severity; | |
71 | + /// | |
72 | + /// Severity of the exception | |
73 | + /// | |
74 | + private int m_severity; | |
38 | 75 | |
39 | - string msg; | |
40 | - string desc; | |
76 | + /// | |
77 | + /// Construct an API exception | |
78 | + /// | |
79 | + /// Params: | |
80 | + /// exception = The ExceptionInfo structure from GraphicsMagick. | |
81 | + /// | |
82 | + this(ref ExceptionInfo exception) | |
83 | + { | |
84 | + m_severity = exception.severity; | |
85 | + m_reason = fromStringz(exception.reason).dup; | |
86 | + m_description = fromStringz(exception.description).dup; | |
87 | + super(m_reason); | |
88 | + } | |
41 | 89 | |
42 | - size_t reasonLen = strlen(ei.reason); | |
43 | - size_t descLen = strlen(ei.description); | |
90 | + /// | |
91 | + /// Descibes the exception that has occurred. | |
92 | + /// | |
93 | + @property public string description() const | |
94 | + { | |
95 | + return m_description; | |
96 | + } | |
44 | 97 | |
45 | - msg.reserve(reasonLen); | |
46 | - msg.length = reasonLen; | |
98 | + /// | |
99 | + /// Reason for the exception being thrown. | |
100 | + /// | |
101 | + @property public string reason() const | |
102 | + { | |
103 | + return m_reason; | |
104 | + } | |
47 | 105 | |
48 | - desc.reserve(descLen); | |
49 | - desc.length = descLen; | |
106 | + /// | |
107 | + /// Severity of the exception | |
108 | + /// | |
109 | + @property public int severity() const | |
110 | + { | |
111 | + return m_severity; | |
112 | + } | |
50 | 113 | |
51 | - copy(ei.reason[0..reasonLen], cast(char[])msg); | |
52 | - copy(ei.description[0..descLen], cast(char[])desc); | |
114 | + /// | |
115 | + /// Descibes the exception that has occurred. | |
116 | + /// | |
117 | + public string getDescription() const | |
118 | + { | |
119 | + return m_description; | |
120 | + } | |
53 | 121 | |
54 | - this.description = desc; | |
122 | + /// | |
123 | + /// Reason for the exception being thrown. | |
124 | + /// | |
125 | + public string getReason() const | |
126 | + { | |
127 | + return m_reason; | |
128 | + } | |
55 | 129 | |
56 | - super(msg); | |
57 | - } | |
130 | + /// | |
131 | + /// Severity of the exception | |
132 | + /// | |
133 | + public int getSeverity() const | |
134 | + { | |
135 | + return m_severity; | |
136 | + } | |
58 | 137 | } |
@@ -62,7 +62,7 @@ class Image { | ||
62 | 62 | GetExceptionInfo(&exception); |
63 | 63 | handle = ReadImage(info_.handle, &exception); |
64 | 64 | if (UndefinedException != exception.severity) { |
65 | - throw new MagickException(exception); | |
65 | + throw new MagickAPIException(exception); | |
66 | 66 | } |
67 | 67 | } |
68 | 68 |
@@ -71,7 +71,7 @@ class Image { | ||
71 | 71 | GetExceptionInfo(&exception); |
72 | 72 | handle = PingImage(info_.handle, &exception); |
73 | 73 | if (UndefinedException != exception.severity) { |
74 | - throw new MagickException(exception); | |
74 | + throw new MagickAPIException(exception); | |
75 | 75 | } |
76 | 76 | } |
77 | 77 |
@@ -177,7 +177,7 @@ class Image { | ||
177 | 177 | cImage* repl = AddNoiseImage(orig, noiseType, &exception); |
178 | 178 | |
179 | 179 | if (UndefinedException != exception.severity) { |
180 | - throw new MagickException(exception); | |
180 | + throw new MagickAPIException(exception); | |
181 | 181 | } |
182 | 182 | this.handle = repl; |
183 | 183 | DestroyImage(orig); |
@@ -201,7 +201,7 @@ class Image { | ||
201 | 201 | cImage* repl = BlurImage(orig, radius, stigma, &exception); |
202 | 202 | |
203 | 203 | if (UndefinedException != exception.severity) { |
204 | - throw new MagickException(exception); | |
204 | + throw new MagickAPIException(exception); | |
205 | 205 | } |
206 | 206 | this.handle = repl; |
207 | 207 | DestroyImage(orig); |