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

Commit b146cae

Browse files
LeonardoBragalgalfaso
authored andcommitted
refactor(minErr): cleanup the generation of the error message
Removes a "magic number" used multiple times in the code Removes unnecessary variables "arg" and "prefix" Removed a condition within the "for" loop that generates query string parameters
1 parent 3353afb commit b146cae

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

src/minErr.js

+18-13
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,33 @@
3333
function minErr(module, ErrorConstructor) {
3434
ErrorConstructor = ErrorConstructor || Error;
3535
return function() {
36-
var code = arguments[0],
37-
prefix = '[' + (module ? module + ':' : '') + code + '] ',
38-
template = arguments[1],
39-
templateArgs = arguments,
36+
var SKIP_INDEXES = 2;
4037

41-
message, i;
38+
var templateArgs = arguments,
39+
code = templateArgs[0],
40+
message = '[' + (module ? module + ':' : '') + code + '] ',
41+
template = templateArgs[1],
42+
paramPrefix, i;
4243

43-
message = prefix + template.replace(/\{\d+\}/g, function(match) {
44-
var index = +match.slice(1, -1), arg;
44+
message += template.replace(/\{\d+\}/g, function(match) {
45+
var index = +match.slice(1, -1),
46+
shiftedIndex = index + SKIP_INDEXES;
4547

46-
if (index + 2 < templateArgs.length) {
47-
return toDebugString(templateArgs[index + 2]);
48+
if (shiftedIndex < templateArgs.length) {
49+
return toDebugString(templateArgs[shiftedIndex]);
4850
}
51+
4952
return match;
5053
});
5154

52-
message = message + '\nhttp://errors.angularjs.org/"NG_VERSION_FULL"/' +
55+
message += '\nhttp://errors.angularjs.org/"NG_VERSION_FULL"/' +
5356
(module ? module + '/' : '') + code;
54-
for (i = 2; i < arguments.length; i++) {
55-
message = message + (i == 2 ? '?' : '&') + 'p' + (i - 2) + '=' +
56-
encodeURIComponent(toDebugString(arguments[i]));
57+
58+
for (i = SKIP_INDEXES, paramPrefix = '?'; i < templateArgs.length; i++, paramPrefix = '&') {
59+
message += paramPrefix + 'p' + (i - SKIP_INDEXES) + '=' +
60+
encodeURIComponent(toDebugString(templateArgs[i]));
5761
}
62+
5863
return new ErrorConstructor(message);
5964
};
6065
}

test/minErrSpec.js

+7
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,11 @@ describe('minErr', function() {
9797
var typeMinErr = minErr('type', TypeError);
9898
expect(typeMinErr('acode', 'aproblem') instanceof TypeError).toBe(true);
9999
});
100+
101+
102+
it('should include a properly formatted error reference URL in the message', function() {
103+
// to avoid maintaining the root URL in two locations, we only validate the parameters
104+
expect(testError('acode', 'aproblem', 'a', 'b', 'value with space').message)
105+
.toMatch(/^[\s\S]*\?p0=a&p1=b&p2=value%20with%20space$/);
106+
});
100107
});

0 commit comments

Comments
 (0)