2011-06-30 23:00:13 -04:00
|
|
|
/**
|
|
|
|
* ease.js warning system
|
|
|
|
*
|
2013-12-20 01:11:26 -05:00
|
|
|
* Copyright (C) 2011, 2012, 2013 Mike Gerwitz
|
2011-06-30 23:00:13 -04:00
|
|
|
*
|
|
|
|
* This file is part of ease.js.
|
|
|
|
*
|
|
|
|
* ease.js is free software: you can redistribute it and/or modify it under the
|
Relicensed under the GPLv3+
This project was originally LGPLv+-licensed to encourage its use in a community
that is largely copyleft-phobic. After further reflection, that was a mistake,
as adoption is not the important factor here---software freedom is.
When submitting ease.js to the GNU project, it was asked if I would be willing
to relicense it under the GPLv3+; I agreed happily, because there is no reason
why we should provide proprietary software any sort of edge. Indeed, proprietary
JavaScript is a huge problem since it is automatically downloaded on the user's
PC generally without them even knowing, and is a current focus for the FSF. As
such, to remain firm in our stance against proprietary JavaScript, relicensing
made the most sense for GNU.
This is likely to upset current users of ease.js. I am not sure of their
number---I have only seen download counts periodically on npmjs.org---but I know
there are at least a small number. These users are free to continue using the
previous LGPL'd releases, but with the understanding that there will be no
further maintenance (not even bug fixes). If possible, users should use the
GPL-licensed versions and release their software as free software.
Here comes GNU ease.js.
2013-12-20 01:00:35 -05:00
|
|
|
* terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
* version.
|
2011-06-30 23:00:13 -04:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
Relicensed under the GPLv3+
This project was originally LGPLv+-licensed to encourage its use in a community
that is largely copyleft-phobic. After further reflection, that was a mistake,
as adoption is not the important factor here---software freedom is.
When submitting ease.js to the GNU project, it was asked if I would be willing
to relicense it under the GPLv3+; I agreed happily, because there is no reason
why we should provide proprietary software any sort of edge. Indeed, proprietary
JavaScript is a huge problem since it is automatically downloaded on the user's
PC generally without them even knowing, and is a current focus for the FSF. As
such, to remain firm in our stance against proprietary JavaScript, relicensing
made the most sense for GNU.
This is likely to upset current users of ease.js. I am not sure of their
number---I have only seen download counts periodically on npmjs.org---but I know
there are at least a small number. These users are free to continue using the
previous LGPL'd releases, but with the understanding that there will be no
further maintenance (not even bug fixes). If possible, users should use the
GPL-licensed versions and release their software as free software.
Here comes GNU ease.js.
2013-12-20 01:00:35 -05:00
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
2011-06-30 23:00:13 -04:00
|
|
|
*
|
Relicensed under the GPLv3+
This project was originally LGPLv+-licensed to encourage its use in a community
that is largely copyleft-phobic. After further reflection, that was a mistake,
as adoption is not the important factor here---software freedom is.
When submitting ease.js to the GNU project, it was asked if I would be willing
to relicense it under the GPLv3+; I agreed happily, because there is no reason
why we should provide proprietary software any sort of edge. Indeed, proprietary
JavaScript is a huge problem since it is automatically downloaded on the user's
PC generally without them even knowing, and is a current focus for the FSF. As
such, to remain firm in our stance against proprietary JavaScript, relicensing
made the most sense for GNU.
This is likely to upset current users of ease.js. I am not sure of their
number---I have only seen download counts periodically on npmjs.org---but I know
there are at least a small number. These users are free to continue using the
previous LGPL'd releases, but with the understanding that there will be no
further maintenance (not even bug fixes). If possible, users should use the
GPL-licensed versions and release their software as free software.
Here comes GNU ease.js.
2013-12-20 01:00:35 -05:00
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* this program. If not, see <http://www.gnu.org/licenses/>.
|
2011-06-30 23:00:13 -04:00
|
|
|
*
|
|
|
|
* @author Mike Gerwitz
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Active warning handler
|
2011-12-13 21:19:14 -05:00
|
|
|
* @type {?function( Warning )}
|
2011-06-30 23:00:13 -04:00
|
|
|
*/
|
|
|
|
var _handler = null;
|
|
|
|
|
2011-12-04 12:30:15 -05:00
|
|
|
/**
|
|
|
|
* Console to use for logging
|
|
|
|
*
|
|
|
|
* This reference allows an alternative console to be used. Must contain warn()
|
|
|
|
* or log() methods.
|
|
|
|
*
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
2011-12-04 12:50:01 -05:00
|
|
|
var _console = ( typeof console !== 'undefined' ) ? console : undefined;
|
2011-12-04 12:30:15 -05:00
|
|
|
|
2011-06-30 23:00:13 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Permits wrapping an exception as a warning
|
|
|
|
*
|
|
|
|
* Warnings are handled differently by the system, depending on the warning
|
|
|
|
* level that has been set.
|
|
|
|
*
|
|
|
|
* @param {Error} e exception (error) to wrap
|
|
|
|
*
|
|
|
|
* @return {Warning} new warning instance
|
2011-12-13 21:19:14 -05:00
|
|
|
*
|
|
|
|
* @constructor
|
2011-06-30 23:00:13 -04:00
|
|
|
*/
|
|
|
|
var Warning = exports.Warning = function( e )
|
|
|
|
{
|
|
|
|
// allow instantiation without use of 'new' keyword
|
|
|
|
if ( !( this instanceof Warning ) )
|
|
|
|
{
|
|
|
|
return new Warning( e );
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure we're wrapping an exception
|
|
|
|
if ( !( e instanceof Error ) )
|
|
|
|
{
|
|
|
|
throw TypeError( "Must provide exception to wrap" );
|
|
|
|
}
|
|
|
|
|
2011-07-06 17:55:40 -04:00
|
|
|
Error.prototype.constructor.call( this, e.message );
|
|
|
|
|
2011-06-30 23:00:13 -04:00
|
|
|
// copy over the message for convenience
|
|
|
|
this.message = e.message;
|
2011-07-06 17:55:40 -04:00
|
|
|
this.name = 'Warning';
|
2011-06-30 23:00:13 -04:00
|
|
|
this._error = e;
|
2011-07-06 17:55:40 -04:00
|
|
|
|
2011-07-06 19:10:23 -04:00
|
|
|
this.stack = e.stack &&
|
|
|
|
e.stack.replace( /^.*?\n+/,
|
2011-07-06 17:55:40 -04:00
|
|
|
this.name + ': ' + this.message + "\n"
|
|
|
|
);
|
2011-06-30 23:00:13 -04:00
|
|
|
};
|
|
|
|
|
2011-07-06 17:55:40 -04:00
|
|
|
// ensures the closest compatibility...just be careful not to modify Warning's
|
|
|
|
// prototype
|
2011-06-30 23:00:13 -04:00
|
|
|
Warning.prototype = Error();
|
2011-07-06 17:55:40 -04:00
|
|
|
Warning.prototype.constructor = Warning;
|
|
|
|
Warning.prototype.name = 'Warning';
|
2011-06-30 23:00:13 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the error wrapped by the warning
|
|
|
|
*
|
|
|
|
* @return {Error} wrapped error
|
|
|
|
*/
|
|
|
|
Warning.prototype.getError = function()
|
|
|
|
{
|
|
|
|
return this._error;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Core warning handlers
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
exports.handlers = {
|
|
|
|
/**
|
|
|
|
* Logs message to console
|
|
|
|
*
|
|
|
|
* Will attempt to log using console.warn(), falling back to console.log()
|
|
|
|
* if necessary and aborting entirely if neither is available.
|
|
|
|
*
|
|
|
|
* This is useful as a default option to bring problems to the developer's
|
|
|
|
* attention without affecting the control flow of the software.
|
|
|
|
*
|
|
|
|
* @param {Warning} warning to log
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
log: function( warning )
|
|
|
|
{
|
|
|
|
var dest;
|
|
|
|
|
2011-12-04 12:30:15 -05:00
|
|
|
_console && ( dest = _console.warn || _console.log ) &&
|
2012-12-17 09:43:51 -05:00
|
|
|
dest.call( _console, ( 'Warning: ' + warning.message ) );
|
2011-06-30 23:00:13 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throws the error associated with the warning
|
|
|
|
*
|
|
|
|
* This handler is useful for development and will ensure that problems are
|
|
|
|
* brought to the attention of the developer.
|
|
|
|
*
|
|
|
|
* @param {Warning} warning to log
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
throwError: function( warning )
|
|
|
|
{
|
|
|
|
throw warning.getError();
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ignores warnings
|
|
|
|
*
|
|
|
|
* This is useful in a production environment where (a) warnings will affect
|
|
|
|
* the reputation of the software or (b) warnings may provide too much
|
|
|
|
* insight into the software. If using this option, you should always
|
|
|
|
* develop in a separate environment so that the system may bring warnings
|
|
|
|
* to your attention.
|
|
|
|
*
|
|
|
|
* @param {Warning} warning to log
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
dismiss: function( warning )
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-04 12:30:15 -05:00
|
|
|
/**
|
|
|
|
* Sets active console
|
|
|
|
*
|
|
|
|
* @param {Object} console containing warn() or log() method
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
exports.setConsole = function( console )
|
|
|
|
{
|
|
|
|
_console = console;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-06-30 23:00:13 -04:00
|
|
|
// set the default handler
|
|
|
|
_handler = exports.handlers.log;
|
|
|
|
|