diff --git a/lib/promises-aplus/promises-aplus-test-adapter.js b/lib/promises-aplus/promises-aplus-test-adapter.js index b5a4a9f3..ba1ad125 100644 --- a/lib/promises-aplus/promises-aplus-test-adapter.js +++ b/lib/promises-aplus/promises-aplus-test-adapter.js @@ -4,6 +4,12 @@ var isFunction = function isFunction(value){return typeof value == 'function';}; var isPromiseLike = function isPromiseLike(obj) {return obj && isFunction(obj.then);}; var isObject = function isObject(value){return value != null && typeof value === 'object';}; +var minErr = function minErr (module, constructor) { + return function (){ + var ErrorConstructor = constructor || Error; + throw new ErrorConstructor(module + arguments[0] + arguments[1]); + }; +}; var $q = qFactory(process.nextTick, function noopExceptionHandler() {}); diff --git a/src/minErr.js b/src/minErr.js index 7df5fdff..48960858 100644 --- a/src/minErr.js +++ b/src/minErr.js @@ -25,10 +25,13 @@ * should all be static strings, not variables or general expressions. * * @param {string} module The namespace to use for the new minErr instance. + * @param {function} ErrorConstructor Custom error constructor to be instantiated when returning + * error from returned function, for cases when a particular type of error is useful. * @returns {function(code:string, template:string, ...templateArgs): Error} minErr instance */ -function minErr(module) { +function minErr(module, ErrorConstructor) { + ErrorConstructor = ErrorConstructor || Error; return function () { var code = arguments[0], prefix = '[' + (module ? module + ':' : '') + code + '] ', @@ -69,7 +72,6 @@ function minErr(module) { message = message + (i == 2 ? '?' : '&') + 'p' + (i-2) + '=' + encodeURIComponent(stringify(arguments[i])); } - - return new Error(message); + return new ErrorConstructor(message); }; } diff --git a/test/minErrSpec.js b/test/minErrSpec.js index 6b7d93b8..da387f52 100644 --- a/test/minErrSpec.js +++ b/test/minErrSpec.js @@ -82,4 +82,12 @@ describe('minErr', function () { expect(myError.message).toMatch(/^\[26\] This is a Foo/); expect(myNamespacedError.message).toMatch(/^\[test:26\] That is a Bar/); }); + + + it('should accept an optional 2nd argument to construct custom errors', function() { + var normalMinErr = minErr('normal'); + expect(normalMinErr('acode', 'aproblem') instanceof TypeError).toBe(false); + var typeMinErr = minErr('type', TypeError); + expect(typeMinErr('acode', 'aproblem') instanceof TypeError).toBe(true); + }); });