From d33595cdf0da82c14e7d12dd74a8cedbd9900cee Mon Sep 17 00:00:00 2001 From: Mike Wey Date: Thu, 3 Feb 2011 23:09:50 +0100 Subject: Some changes to the Exceptions --- dmagick/Exception.d | 89 ++++++++++++++++++++++++++++++----------------------- dmagick/Image.d | 2 +- dmagick/Utils.d | 8 +++-- 3 files changed, 56 insertions(+), 43 deletions(-) (limited to 'dmagick') diff --git a/dmagick/Exception.d b/dmagick/Exception.d index 6fbd828..8d5e4cc 100644 --- a/dmagick/Exception.d +++ b/dmagick/Exception.d @@ -15,7 +15,8 @@ import dmagick.c.exception; /** * A base class for all exceptions thrown bij DMagick. - * The following Exceptions are derived from this class: $(BR)$(BR) + * The following Exceptions are derived from this class: + * * ResourceLimitException, TypeException, OptionException, * DelegateException, MissingDelegateException, CorruptImageException, * FileOpenException, BlobException, StreamException, CacheException, @@ -23,7 +24,7 @@ import dmagick.c.exception; * ImageException, WandException, RandomException, XServerException, * MonitorException, RegistryException, ConfigureException, PolicyException */ -class MagickException : Exception +class DMagickException : Exception { this(string reason, string description) { @@ -36,11 +37,51 @@ class MagickException : Exception super(message); } + + private enum string[] severities = [ "Blob", "Cache", "Coder", + "Configure", "CorruptImage", "Delegate", "Draw", "FileOpen", + "Filter", "Image", "MissingDelegate", "Module", "Monitor", + "Option", "Policy", "Random", "Registry", "ResourceLimit", + "Stream", "Type", "Wand", "XServer" ]; + + /** + * Throws an Exception or error matching the ExceptionInfo. + */ + static void throwException(ExceptionInfo* exception) + { + if ( exception.severity == ExceptionType.UndefinedException ) + return; + + string reason = to!(string)(exception.reason); + string description = to!(string)(exception.description); + + mixin( + { + string exceptions = + "switch ( exception.severity ) + {"; + + foreach ( severity; severities ) + { + exceptions ~= + "case ExceptionType."~ severity ~"Warning: + throw new "~ severity ~"Exception(reason, description); + break; + case ExceptionType."~ severity ~"Error: + case ExceptionType."~ severity ~"FatalError: + throw new "~ severity ~"Error(reason, description); + break;"; + } + + return exceptions ~= "}"; + }()); + } } /** * A base class for all errors thrown bij DMagick. - * The following Errors are derived from this class: $(BR)$(BR) + * The following Errors are derived from this class: + * * ResourceLimitError, TypeError, OptionError, * DelegateError, MissingDelegateError, CorruptImageError, * FileOpenError, BlobError, StreamError, CacheError, @@ -48,7 +89,7 @@ class MagickException : Exception * ImageError, WandError, RandomError, XServerError, * MonitorError, RegistryError, ConfigureError, PolicyError */ -class MagickError : Error +class DMagickError : Error { this(string reason, string description) { @@ -63,47 +104,17 @@ class MagickError : Error } } -mixin(generateExceptions()); - /** * Generate the exceptions and the throwException function; */ -private string generateExceptions() +mixin( { - 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 ) + foreach ( severity; DMagickException.severities ) { exceptions ~= - "class " ~ severity ~ "Exception : MagickException + "class " ~ severity ~ "Exception : DMagickException { this(string reason, string description) { @@ -112,7 +123,7 @@ private string generateExceptions() }"; exceptions ~= - "class " ~ severity ~ "Error : MagickError + "class " ~ severity ~ "Error : DMagickError { this(string reason, string description) { @@ -122,5 +133,5 @@ private string generateExceptions() } return exceptions; -} +}()); diff --git a/dmagick/Image.d b/dmagick/Image.d index 5d3636a..aadeb17 100644 --- a/dmagick/Image.d +++ b/dmagick/Image.d @@ -50,7 +50,7 @@ class Image ExceptionInfo* exception = AcquireExceptionInfo(); MagickCoreImage* image = ReadImage(options.imageInfo, exception); - throwException(exception); + DMagickException.throwException(exception); imageRef = ImageRef(image); DestroyExceptionInfo(exception); diff --git a/dmagick/Utils.d b/dmagick/Utils.d index 1396ddf..d5e8ff6 100644 --- a/dmagick/Utils.d +++ b/dmagick/Utils.d @@ -10,6 +10,8 @@ module dmagick.Utils; import std.math; +import dmagick.Exception; + import dmagick.c.memory; import dmagick.c.magickString; import dmagick.c.magickType; @@ -21,7 +23,7 @@ import dmagick.c.magickType; void copyString(ref char[MaxTextExtent] dest, string source) { if ( source.length < MaxTextExtent ) - throw new Exception("text is to long"); //TODO: a proper exception. + throw new ResourceLimitException("Source is larger then MaxTextExtend", null); dest[0 .. source.length] = source; dest[source.length] = '\0'; @@ -44,7 +46,7 @@ void copyString(ref char* dest, string source) } if ( ~source.length < MaxTextExtent ) - throw new Exception("UnableToAcquireString"); //TODO: a proper exception. + throw new ResourceLimitException("unable to acquire string", null); if ( dest is null ) dest = cast(char*)AcquireQuantumMemory(source.length+MaxTextExtent, dest.sizeof); @@ -52,7 +54,7 @@ void copyString(ref char* dest, string source) dest = cast(char*)ResizeQuantumMemory(dest, source.length+MaxTextExtent, dest.sizeof); if ( dest is null ) - throw new Exception("UnableToAcquireString"); //TODO: a proper exception. + throw new ResourceLimitException("unable to acquire string", null); if ( source.length > 0 ) dest[0 .. source.length] = source; -- cgit v1.2.3