From a6bd4bc866a18f860c7548fa1b3f6d4c2a953416 Mon Sep 17 00:00:00 2001 From: Jeff Cross Date: Thu, 21 Aug 2014 12:23:24 -0700 Subject: [PATCH] feat(minErr): allow specifying ErrorConstructor in minErr constructor In some cases, the type of Error thrown by minErr is meaningful, such as in $q where a TypeError is sometimes required. This fix allows providing an error constructor as the second argument to minErr, which will be used to construct the error that gets returned by the factory function. --- lib/promises-aplus/promises-aplus-test-adapter.js | 6 ++++++ src/minErr.js | 8 +++++--- test/minErrSpec.js | 8 ++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) 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); + }); });