Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit a6bd4bc

Browse files
committed
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.
1 parent 23da614 commit a6bd4bc

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

lib/promises-aplus/promises-aplus-test-adapter.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
var isFunction = function isFunction(value){return typeof value == 'function';};
55
var isPromiseLike = function isPromiseLike(obj) {return obj && isFunction(obj.then);};
66
var isObject = function isObject(value){return value != null && typeof value === 'object';};
7+
var minErr = function minErr (module, constructor) {
8+
return function (){
9+
var ErrorConstructor = constructor || Error;
10+
throw new ErrorConstructor(module + arguments[0] + arguments[1]);
11+
};
12+
};
713

814
var $q = qFactory(process.nextTick, function noopExceptionHandler() {});
915

src/minErr.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@
2525
* should all be static strings, not variables or general expressions.
2626
*
2727
* @param {string} module The namespace to use for the new minErr instance.
28+
* @param {function} ErrorConstructor Custom error constructor to be instantiated when returning
29+
* error from returned function, for cases when a particular type of error is useful.
2830
* @returns {function(code:string, template:string, ...templateArgs): Error} minErr instance
2931
*/
3032

31-
function minErr(module) {
33+
function minErr(module, ErrorConstructor) {
34+
ErrorConstructor = ErrorConstructor || Error;
3235
return function () {
3336
var code = arguments[0],
3437
prefix = '[' + (module ? module + ':' : '') + code + '] ',
@@ -69,7 +72,6 @@ function minErr(module) {
6972
message = message + (i == 2 ? '?' : '&') + 'p' + (i-2) + '=' +
7073
encodeURIComponent(stringify(arguments[i]));
7174
}
72-
73-
return new Error(message);
75+
return new ErrorConstructor(message);
7476
};
7577
}

test/minErrSpec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,12 @@ describe('minErr', function () {
8282
expect(myError.message).toMatch(/^\[26\] This is a Foo/);
8383
expect(myNamespacedError.message).toMatch(/^\[test:26\] That is a Bar/);
8484
});
85+
86+
87+
it('should accept an optional 2nd argument to construct custom errors', function() {
88+
var normalMinErr = minErr('normal');
89+
expect(normalMinErr('acode', 'aproblem') instanceof TypeError).toBe(false);
90+
var typeMinErr = minErr('type', TypeError);
91+
expect(typeMinErr('acode', 'aproblem') instanceof TypeError).toBe(true);
92+
});
8593
});

0 commit comments

Comments
 (0)