2020-05-27 14:26:44 -07:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-02-11 06:36:15 -08:00
|
|
|
import path from 'path';
|
2021-07-02 16:45:09 -07:00
|
|
|
import { StackFrame } from '../protocol/channels';
|
2021-02-11 06:36:15 -08:00
|
|
|
import StackUtils from 'stack-utils';
|
2021-02-12 10:11:30 -08:00
|
|
|
import { isUnderTest } from './utils';
|
2020-05-27 14:26:44 -07:00
|
|
|
|
2021-02-10 18:52:28 -08:00
|
|
|
const stackUtils = new StackUtils();
|
2020-05-27 14:26:44 -07:00
|
|
|
|
2021-08-26 18:44:49 -07:00
|
|
|
export function rewriteErrorMessage<E extends Error>(e: E, newMessage: string): E {
|
|
|
|
const lines: string[] = (e.stack?.split('\n') || []).filter(l => l.startsWith(' at '));
|
2020-05-28 16:33:31 -07:00
|
|
|
e.message = newMessage;
|
2021-08-26 18:44:49 -07:00
|
|
|
const errorTitle = `${e.name}: ${e.message}`;
|
|
|
|
if (lines.length)
|
|
|
|
e.stack = `${errorTitle}\n${lines.join('\n')}`;
|
2020-05-28 16:33:31 -07:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2021-08-16 17:06:38 -07:00
|
|
|
const ROOT_DIR = path.resolve(__dirname, '..', '..');
|
|
|
|
const CLIENT_LIB = path.join(ROOT_DIR, 'lib', 'client');
|
|
|
|
const CLIENT_SRC = path.join(ROOT_DIR, 'src', 'client');
|
2021-09-17 15:24:15 -07:00
|
|
|
const TEST_LIB = path.join(ROOT_DIR, 'lib', 'test');
|
|
|
|
const TEST_SRC = path.join(ROOT_DIR, 'src', 'test');
|
2021-07-22 20:29:36 +02:00
|
|
|
|
2021-06-28 13:27:38 -07:00
|
|
|
export type ParsedStackTrace = {
|
2021-09-15 11:34:23 -07:00
|
|
|
allFrames: StackFrame[];
|
2021-06-28 13:27:38 -07:00
|
|
|
frames: StackFrame[];
|
|
|
|
frameTexts: string[];
|
|
|
|
apiName: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export function captureStackTrace(): ParsedStackTrace {
|
2021-04-29 09:28:19 -07:00
|
|
|
const stackTraceLimit = Error.stackTraceLimit;
|
|
|
|
Error.stackTraceLimit = 30;
|
2021-06-28 13:27:38 -07:00
|
|
|
const error = new Error();
|
|
|
|
const stack = error.stack!;
|
2021-04-29 09:28:19 -07:00
|
|
|
Error.stackTraceLimit = stackTraceLimit;
|
2021-06-28 13:27:38 -07:00
|
|
|
|
2021-08-16 17:06:38 -07:00
|
|
|
const isTesting = isUnderTest();
|
|
|
|
type ParsedFrame = {
|
|
|
|
frame: StackFrame;
|
|
|
|
frameText: string;
|
|
|
|
inClient: boolean;
|
|
|
|
};
|
|
|
|
let parsedFrames = stack.split('\n').map(line => {
|
2021-02-10 18:52:28 -08:00
|
|
|
const frame = stackUtils.parseLine(line);
|
|
|
|
if (!frame || !frame.file)
|
2021-08-16 17:06:38 -07:00
|
|
|
return null;
|
2021-02-10 18:52:28 -08:00
|
|
|
if (frame.file.startsWith('internal'))
|
2021-08-16 17:06:38 -07:00
|
|
|
return null;
|
2021-02-10 18:52:28 -08:00
|
|
|
const fileName = path.resolve(process.cwd(), frame.file);
|
2021-06-17 15:09:38 -07:00
|
|
|
if (isTesting && fileName.includes(path.join('playwright', 'tests', 'config', 'coverage.js')))
|
2021-08-16 17:06:38 -07:00
|
|
|
return null;
|
2021-09-17 15:24:15 -07:00
|
|
|
const inClient =
|
|
|
|
// Allow fixtures in the reported stacks.
|
|
|
|
(!fileName.includes('test/index') && !fileName.includes('test\\index')) && (
|
2021-09-27 09:19:59 -07:00
|
|
|
frame.file.includes(path.join('node_modules', 'expect'))
|
|
|
|
|| fileName.startsWith(CLIENT_LIB)
|
2021-09-17 15:24:15 -07:00
|
|
|
|| fileName.startsWith(CLIENT_SRC)
|
|
|
|
|| fileName.startsWith(TEST_LIB)
|
|
|
|
|| fileName.startsWith(TEST_SRC));
|
2021-08-16 17:06:38 -07:00
|
|
|
const parsed: ParsedFrame = {
|
|
|
|
frame: {
|
|
|
|
file: fileName,
|
|
|
|
line: frame.line,
|
|
|
|
column: frame.column,
|
|
|
|
function: frame.function,
|
|
|
|
},
|
|
|
|
frameText: line,
|
2021-09-17 15:24:15 -07:00
|
|
|
inClient
|
2021-08-16 17:06:38 -07:00
|
|
|
};
|
|
|
|
return parsed;
|
2021-09-17 15:24:15 -07:00
|
|
|
}).filter(Boolean) as ParsedFrame[];
|
2021-08-16 17:06:38 -07:00
|
|
|
|
|
|
|
let apiName = '';
|
|
|
|
// Deepest transition between non-client code calling into client code
|
|
|
|
// is the api entry.
|
2021-09-15 11:34:23 -07:00
|
|
|
const allFrames = parsedFrames;
|
2021-08-16 17:06:38 -07:00
|
|
|
for (let i = 0; i < parsedFrames.length - 1; i++) {
|
|
|
|
if (parsedFrames[i].inClient && !parsedFrames[i + 1].inClient) {
|
|
|
|
const frame = parsedFrames[i].frame;
|
2021-09-27 09:19:59 -07:00
|
|
|
const text = parsedFrames[i].frameText;
|
|
|
|
// expect matchers have the following stack structure:
|
|
|
|
// at __EXTERNAL_MATCHER_TRAP__ (.../index.js:342:30)
|
|
|
|
// at Object.throwingMatcher [as toBeChecked] (.../index.js:343:15)
|
|
|
|
const aliasIndex = text.indexOf('[as ');
|
|
|
|
if (aliasIndex !== -1)
|
|
|
|
apiName = 'expect.' + text.substring(aliasIndex + 4, text.indexOf(']'));
|
|
|
|
else
|
|
|
|
apiName = frame.function ? frame.function[0].toLowerCase() + frame.function.slice(1) : '';
|
2021-08-16 17:06:38 -07:00
|
|
|
parsedFrames = parsedFrames.slice(i + 1);
|
2021-06-28 13:27:38 -07:00
|
|
|
break;
|
|
|
|
}
|
2021-02-10 18:52:28 -08:00
|
|
|
}
|
2021-04-19 22:37:38 +02:00
|
|
|
|
2021-08-16 17:06:38 -07:00
|
|
|
return {
|
2021-09-15 11:34:23 -07:00
|
|
|
allFrames: allFrames.map(p => p.frame),
|
2021-08-16 17:06:38 -07:00
|
|
|
frames: parsedFrames.map(p => p.frame),
|
|
|
|
frameTexts: parsedFrames.map(p => p.frameText),
|
|
|
|
apiName
|
|
|
|
};
|
2021-07-22 20:29:36 +02:00
|
|
|
}
|
|
|
|
|
2021-04-19 22:37:38 +02:00
|
|
|
export function splitErrorMessage(message: string): { name: string, message: string } {
|
|
|
|
const separationIdx = message.indexOf(':');
|
|
|
|
return {
|
|
|
|
name: separationIdx !== -1 ? message.slice(0, separationIdx) : '',
|
|
|
|
message: separationIdx !== -1 && separationIdx + 2 <= message.length ? message.substring(separationIdx + 2) : message,
|
|
|
|
};
|
|
|
|
}
|