1
0
Fork 0

Added ability to set and trigger the active warning handler

closure/master
Mike Gerwitz 2011-06-29 22:33:57 -04:00
parent e7c20e0494
commit c672a2b575
2 changed files with 121 additions and 0 deletions

View File

@ -22,6 +22,12 @@
* @package core
*/
/**
* Active warning handler
* @type {function()}
*/
var _handler = null;
/**
* Permits wrapping an exception as a warning
@ -128,3 +134,35 @@ exports.handlers = {
},
};
/**
* Sets the active warning handler
*
* You may use any of the predefined warning handlers or pass your own function.
*
* @param {function( Warning )} handler warning handler
*
* @return {undefined}
*/
exports.setHandler = function( handler )
{
_handler = handler;
};
/**
* Handles a warning using the active warning handler
*
* @param {Warning} warning warning to handle
*
* @return {undefined}
*/
exports.handle = function( warning )
{
_handler( warning );
}
// set the default handler
_handler = exports.handlers.log;

View File

@ -0,0 +1,83 @@
/**
* Tests warning system implementation
*
* Copyright (C) 2010 Mike Gerwitz
*
* This file is part of ease.js.
*
* ease.js is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Mike Gerwitz
* @package test
*/
var common = require( './common' ),
assert = require( 'assert' ),
warn = common.require( 'warn' )
;
/**
* The default warning handler should be the 'log' handler. This is a friendly
* compromise that will allow the developer to be warned of potential issues
* without affecting program execution.
*/
( function testDefaultHandlerIsLogger()
{
// back up console object
var console_ = ( typeof console !== 'undefined' ) ? console : undefined,
called = false;
// stub it
console = {
warn: function()
{
called = true;
},
};
warn.handle( warn.Warning( Error( 'foo' ) ) );
assert.ok( called,
"Default handler will log to console"
);
// restore console
console = console_;
} )();
/**
* The warning handler can be altered at runtime. Ensure we can set it and call
* it appropriately. We do not need to use one of the pre-defined handlers.
*/
( function testCanSetAndCallWarningHandler()
{
var given,
warning = warn.Warning( Error( 'foo' ) );
// set a stub warning handler
warn.setHandler( function( warn )
{
given = warn;
} );
// trigger the handler
warn.handle( warning );
assert.deepEqual( given, warning,
"Set warning handler should be called with given Warning"
);
} )();