From 90efbb83da4d90d641e244419a4528fc6aa6ebbc Mon Sep 17 00:00:00 2001 From: Mike Wey Date: Wed, 2 Feb 2011 23:59:22 +0100 Subject: Add the exceptions --- dmagick/Exception.d | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++ dmagick/Image.d | 3 +- dmagick/Options.d | 1 - 3 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 dmagick/Exception.d diff --git a/dmagick/Exception.d b/dmagick/Exception.d new file mode 100644 index 0000000..6fbd828 --- /dev/null +++ b/dmagick/Exception.d @@ -0,0 +1,126 @@ +/** + * A class to expose ImageInfo QuantizeInfo and DrawInfo + * + * Copyright: Mike Wey 2011 + * License: To be determined + * Authors: Mike Wey + */ + +module dmagick.Exception; + +import std.conv; + +import dmagick.c.client; +import dmagick.c.exception; + +/** + * A base class for all exceptions thrown bij DMagick. + * The following Exceptions are derived from this class: $(BR)$(BR) + * ResourceLimitException, TypeException, OptionException, + * DelegateException, MissingDelegateException, CorruptImageException, + * FileOpenException, BlobException, StreamException, CacheException, + * CoderException, FilterException, ModuleException, DrawException, + * ImageException, WandException, RandomException, XServerException, + * MonitorException, RegistryException, ConfigureException, PolicyException + */ +class MagickException : Exception +{ + this(string reason, string description) + { + string message = to!(string)(GetClientName()); + + if ( reason.length > 0 ) + message ~= ": " ~ reason; + if ( description.length > 0 ) + message ~= " (" ~ description ~ ")"; + + super(message); + } +} + +/** + * A base class for all errors thrown bij DMagick. + * The following Errors are derived from this class: $(BR)$(BR) + * ResourceLimitError, TypeError, OptionError, + * DelegateError, MissingDelegateError, CorruptImageError, + * FileOpenError, BlobError, StreamError, CacheError, + * CoderError, FilterError, ModuleError, DrawError, + * ImageError, WandError, RandomError, XServerError, + * MonitorError, RegistryError, ConfigureError, PolicyError + */ +class MagickError : Error +{ + this(string reason, string description) + { + string message = to!(string)(GetClientName()); + + if ( reason.length > 0 ) + message ~= ": " ~ reason; + if ( description.length > 0 ) + message ~= " (" ~ description ~ ")"; + + super(message); + } +} + +mixin(generateExceptions()); + +/** + * Generate the exceptions and the throwException function; + */ +private string generateExceptions() +{ + string[] severities = [ "Blob", "Cache", "Coder", "Configure", + "CorruptImage", "Delegate", "Draw", "FileOpen", "Filter", + "Image", "MissingDelegate", "Module", "Monitor", "Option", + "Policy", "Random", "Registry", "ResourceLimit", "Stream", + "Type", "Wand", "XServer" ]; + + string exceptions; + + exceptions ~= + "void throwException(ExceptionInfo* exception) + { + if ( exception.severity == ExceptionType.UndefinedException ) + return; + + string reason = to!(string)(exception.reason); + string description = to!(string)(exception.description); + + switch(exception.severity) + {"; + + foreach ( severity; severities ) + { + exceptions ~= + "case ExceptionType."~ severity ~"Warning: + throw new "~ severity ~"Exception(reason, description); + break;"; + } + + exceptions ~= "}}"; + + foreach ( severity; severities ) + { + exceptions ~= + "class " ~ severity ~ "Exception : MagickException + { + this(string reason, string description) + { + super(reason, description); + } + }"; + + exceptions ~= + "class " ~ severity ~ "Error : MagickError + { + this(string reason, string description) + { + super(reason, description); + } + }"; + } + + return exceptions; +} + diff --git a/dmagick/Image.d b/dmagick/Image.d index e946433..5d3636a 100644 --- a/dmagick/Image.d +++ b/dmagick/Image.d @@ -8,6 +8,7 @@ module dmagick.Image; +import dmagick.Exception; import dmagick.Options; import dmagick.Utils; @@ -49,7 +50,7 @@ class Image ExceptionInfo* exception = AcquireExceptionInfo(); MagickCoreImage* image = ReadImage(options.imageInfo, exception); - //TODO: Throw if exception. + throwException(exception); imageRef = ImageRef(image); DestroyExceptionInfo(exception); diff --git a/dmagick/Options.d b/dmagick/Options.d index 312e912..d7aa486 100644 --- a/dmagick/Options.d +++ b/dmagick/Options.d @@ -6,7 +6,6 @@ * Authors: Mike Wey */ - module dmagick.Options; import std.conv; -- cgit v1.2.3