view mde/exception.d @ 15:4608be19ebe2

Use OS paths (linux only for now), merging multiple paths. Init changes regarding options. Reorganised policies.txt a little. Implemented mde.resource.paths to read config from appropriate paths (currently linux only). Changed Init to load options before all other delegates are run and set logging level from options. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Fri, 14 Mar 2008 11:39:45 +0000
parents bff0d802cb7d
children 5f90774ea1ef
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);
    }
}

/// Thrown when Init fails.
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);
    }
}

/// Thrown when loading options fails.
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);
    }
}

/// Thrown when loading strings for the requested name and current locale fails.
class L10nLoadException : 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 ~ ".i18n.I18nTranslation";	}
    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.exception");
    }
    
    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.");
    }
}