| 1 |
/* LICENSE BLOCK |
|---|
| 2 |
Part of mde: a Modular D game-oriented Engine |
|---|
| 3 |
Copyright © 2007-2008 Diggory Hardy |
|---|
| 4 |
|
|---|
| 5 |
This program is free software: you can redistribute it and/or modify it under the terms |
|---|
| 6 |
of the GNU General Public License as published by the Free Software Foundation, either |
|---|
| 7 |
version 2 of the License, or (at your option) any later version. |
|---|
| 8 |
|
|---|
| 9 |
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
|---|
| 10 |
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|---|
| 11 |
See the GNU General Public License for more details. |
|---|
| 12 |
|
|---|
| 13 |
You should have received a copy of the GNU General Public License |
|---|
| 14 |
along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
|---|
| 15 |
|
|---|
| 16 |
/// Contains the base class for all mde exceptions plus some exception classes. |
|---|
| 17 |
module mde.exception; |
|---|
| 18 |
|
|---|
| 19 |
/** Base class for all mde Exceptions. |
|---|
| 20 |
* |
|---|
| 21 |
* All packages should have their own base exception type extending this one, and for each package |
|---|
| 22 |
* level a CTOR taking a message should pass the message to the super. |
|---|
| 23 |
* A CTOR not taking a message and calling the super without a parameter may also be provided. |
|---|
| 24 |
* |
|---|
| 25 |
* The static string symbol |
|---|
| 26 |
* should be overriden as below so that it ends up as something like "mde.file" or |
|---|
| 27 |
* "mde.pkg.file.Class" describing where the exception was thrown. (Since only methods overload |
|---|
| 28 |
* correctly, symbol is made static and an overloadable method is used to access the correct symbol.) |
|---|
| 29 |
*/ |
|---|
| 30 |
class mdeException : Exception { |
|---|
| 31 |
/// Override in derived classes to name the module where the error occured. |
|---|
| 32 |
char[] getSymbol () { |
|---|
| 33 |
return "mde"; |
|---|
| 34 |
} |
|---|
| 35 |
/// Prefix msg, e.g.: "mde.foo: message" |
|---|
| 36 |
char[] prefixedMsg () { |
|---|
| 37 |
return getSymbol() ~ ": " ~ msg; |
|---|
| 38 |
} |
|---|
| 39 |
this (char[] msg) { |
|---|
| 40 |
super(msg); |
|---|
| 41 |
} |
|---|
| 42 |
this () { // No supplied error message. |
|---|
| 43 |
super(""); |
|---|
| 44 |
} |
|---|
| 45 |
} |
|---|
| 46 |
|
|---|
| 47 |
/// Thrown when loading options fails. |
|---|
| 48 |
class optionsLoadException : mdeException { |
|---|
| 49 |
char[] getSymbol () { |
|---|
| 50 |
return super.getSymbol ~ ".options"; |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
this (char[] msg) { |
|---|
| 54 |
super(msg); |
|---|
| 55 |
} |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
/// Thrown when loading strings for the requested name and current locale fails. |
|---|
| 59 |
class L10nLoadException : mdeException { |
|---|
| 60 |
char[] getSymbol () { |
|---|
| 61 |
return super.getSymbol ~ ".i18n.I18nTranslation"; |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
this (char[] msg) { |
|---|
| 65 |
super(msg); |
|---|
| 66 |
} |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
/// Thrown when an image fails to load or cannot be loaded to a texture (unsupported format?). |
|---|
| 70 |
class ImageException : mdeException { |
|---|
| 71 |
char[] getSymbol () { |
|---|
| 72 |
return super.getSymbol ~ ".gl.texture"; |
|---|
| 73 |
} |
|---|
| 74 |
this (char[] msg) { |
|---|
| 75 |
super (msg); |
|---|
| 76 |
} |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
debug (mdeUnitTest) { |
|---|
| 81 |
import tango.util.log.Log : Log, Logger; |
|---|
| 82 |
|
|---|
| 83 |
private Logger logger; |
|---|
| 84 |
static this() { |
|---|
| 85 |
logger = Log.getLogger ("mde.exception"); |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
unittest { |
|---|
| 89 |
// Check message prepending works correctly. |
|---|
| 90 |
mdeException mE = new optionsLoadException(""); |
|---|
| 91 |
assert (mE.getSymbol() == "mde.options", mE.getSymbol()); |
|---|
| 92 |
try { |
|---|
| 93 |
throw new mdeException ("ABC"); |
|---|
| 94 |
assert (false); |
|---|
| 95 |
} catch (mdeException e) { |
|---|
| 96 |
assert (e.msg == "ABC", e.msg); |
|---|
| 97 |
assert (e.prefixedMsg == "mde: ABC", e.prefixedMsg); |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
logger.info ("Unittest complete."); |
|---|
| 101 |
} |
|---|
| 102 |
} |
|---|