view mde/exception.d @ 11:b940f267419e

Options class created & changes to mergetag exception messages. Options class created (barebones). Loading/saving from Init. Init no longer runs cleanup functions after initialisation failure. Improved mergetag exception messages & error reporting. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Thu, 21 Feb 2008 09:05:33 +0000
parents 4c3575400769
children bff0d802cb7d
line wrap: on
line source

/// Contains a base class for all mde exceptions.
module mde.exception;

/** Base class for all mde Exceptions.
 *
 * All packages should have their own base exception type extending this one, and for each package
 * level a CTOR taking a message should pass the message to the super.
 * A CTOR not taking a message and calling the super without a parameter may also be provided.
 *
 * The static string symbol
 * should be overriden as below so that it ends up as something like "mde.file" or
 * "mde.pkg.file.Class" describing where the exception was thrown. (Since only methods overload
 * correctly, symbol is made static and an overloadable method is used to access the correct symbol.)
 */
class mdeException : Exception {
    static const symbol = "mde";	/// Override in derived classes to name the module where the error occured.
    char[] getSymbol () {
        return symbol;
    }
    this (char[] msg) {
        super(getSymbol() ~ ": " ~ msg);
    }
    this () {			// No supplied error message.
        super(symbol);
    }
}

class initException : mdeException {
    // NOTE: if symbol is final, it can't be modified in the static this(), but as const it can
    static const char[] symbol;
    static this () {	symbol = super.symbol ~ ".init";	}
    char[] getSymbol () {
        return symbol;
    }
    
    this (char[] msg) {
        super(msg);
    }
}

class optionsLoadException : mdeException {
    // NOTE: if symbol is final, it can't be modified in the static this(), but as const it can
    static const char[] symbol;
    static this () {	symbol = super.symbol ~ ".options";	}
    char[] getSymbol () {
        return symbol;
    }
    
    this (char[] msg) {
        super(msg);
    }
}

debug (mdeUnitTest) {
    import tango.util.log.Log : Log, Logger;

    private Logger logger;
    static this() {
        logger = Log.getLogger ("mde.events");
        logger.info ("Got logger!");
    }
    
    unittest {
        // Check message prepending works correctly.
        mdeException mE = new initException("");
        assert (mE.getSymbol() == "mde.init", mE.getSymbol());
        try {
            throw new initException ("ABC");
            assert (false);
        } catch (Exception e) {
            assert (e.msg == "mde.init: ABC", e.msg);
        }
    
        logger.info ("Unittest complete.");
    }
}