Skip to content

Commit 93a897c

Browse files
committed
Add the new build-stack-trace.js file
1 parent 0b34534 commit 93a897c

4 files changed

Lines changed: 92 additions & 24 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright 2017 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
function buildStackTrace(message) {
20+
var fauxError = new Error('');
21+
var fullStack = fauxError.stack.split('\n');
22+
return (message ? message + '\n' : '') + fullStack.slice(2).join('\n');
23+
}
24+
25+
module.exports = buildStackTrace;

packages/error-reporting/src/error-router.js

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var isObject = is.object;
2121
var isString = is.string;
2222
var isNumber = is.number;
2323
var isFunction = is.function;
24+
var buildStackTrace = require('./build-stack-trace.js');
2425

2526
/**
2627
* The Error handler router is responsible for taking an object of some type and
@@ -137,16 +138,7 @@ function populateFromObject(ob, errorMessage) {
137138
* @returns {Undefined} - does not return anything
138139
*/
139140
function populateFromString(ob, errorMessage) {
140-
var fauxError = new Error();
141-
var fullStack = fauxError.stack.split('\n');
142-
var cleanedStack = fullStack.slice(0, 1).concat(fullStack.slice(4));
143-
var errChecked = '';
144-
145-
// Replace the generic error message with the user-provided string
146-
cleanedStack[0] = ob;
147-
errChecked = cleanedStack.join('\n');
148-
149-
errorMessage.setMessage(errChecked);
141+
errorMessage.setMessage(buildStackTrace(ob));
150142
}
151143

152144
/**
@@ -161,14 +153,8 @@ function populateFromString(ob, errorMessage) {
161153
* @returns {Undefined} - does not return anything
162154
*/
163155
function populateFromNumber(ob, errorMessage) {
164-
var fauxError = new Error();
165-
var errChecked = fauxError.stack;
166-
167-
if (isNumber(ob) && isFunction(ob.toString)) {
168-
errChecked = ob.toString();
169-
}
170-
171-
errorMessage.setMessage(errChecked);
156+
var message = isNumber(ob) && isFunction(ob.toString) ? ob.toString() : '';
157+
errorMessage.setMessage(buildStackTrace(message));
172158
}
173159

174160
/**
@@ -184,9 +170,7 @@ function populateFromNumber(ob, errorMessage) {
184170
* @returns {Undefined} - does not return anything
185171
*/
186172
function populateFromUnknown(ob, errorMessage) {
187-
var fauxError = new Error();
188-
189-
errorMessage.setMessage(fauxError.stack);
173+
errorMessage.setMessage(buildStackTrace());
190174
}
191175

192176
module.exports = errorHandlerRouter;

packages/error-reporting/src/interfaces/message-builder.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
'use strict';
1818
var ErrorMessage = require('../classes/error-message.js');
19+
var buildStackTrace = require('../build-stack-trace.js');
1920

2021
/**
2122
* The handler setup function serves to produce a bound instance of the
@@ -44,9 +45,7 @@ function handlerSetup(config) {
4445
// error is used instead of the stack trace where the error is
4546
// reported to be consistent with the behavior of reporting a
4647
// an error when reporting an actual Node.js Error object.
47-
var fauxError = new Error('');
48-
var fullStack = fauxError.stack.split('\n');
49-
var cleanedStack = fullStack.slice(2).join('\n');
48+
var cleanedStack = buildStackTrace();
5049

5150
var em = new ErrorMessage().setServiceContext(
5251
config.getServiceContext().service,
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Copyright 2017 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
var assert = require('assert');
20+
var buildStackTrace = require('../../src/build-stack-trace.js');
21+
22+
describe('build-stack-trace', function() {
23+
it('Should not have a message attached if none is given', function() {
24+
assert(buildStackTrace().startsWith(' at'));
25+
assert(!buildStackTrace(undefined).startsWith('undefined'));
26+
assert(!buildStackTrace(null).startsWith('null'));
27+
});
28+
29+
it('Should attach a message if given', function() {
30+
assert(buildStackTrace('Some Message').startsWith('Some Message\n'));
31+
});
32+
33+
it('Should not contain error-reporting specific frames', function() {
34+
var internalFileName = 'build-stack-trace';
35+
var stackTrace = buildStackTrace();
36+
var firstIndex = stackTrace.indexOf(internalFileName);
37+
var lastIndex = stackTrace.lastIndexOf(internalFileName);
38+
// This file, named 'build-stack-trace.js', tests the
39+
// 'build-stack-trace.js' file. The stack trace should not contain
40+
// information about the 'build-stack-trace.js' file that is being
41+
// tested. Thus the stack trace should only contain the string
42+
// 'build-stack-trace' one time, which corresponds to this test file
43+
// and not the 'build-stack-trace.js' file being tested.
44+
assert.strictEqual(firstIndex, lastIndex);
45+
});
46+
47+
it('Should return the stack trace', function() {
48+
(function functionA() {
49+
(function functionB() {
50+
(function functionC() {
51+
var stackTrace = buildStackTrace();
52+
assert(stackTrace);
53+
assert.notStrictEqual(stackTrace.indexOf('functionA'), -1);
54+
assert.notStrictEqual(stackTrace.indexOf('functionB'), -1);
55+
assert.notStrictEqual(stackTrace.indexOf('functionC'), -1);
56+
})();
57+
})();
58+
})();
59+
});
60+
});

0 commit comments

Comments
 (0)