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-02-10 18:52:28 -08:00
|
|
|
import { StackFrame } from '../common/types';
|
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
|
|
|
|
2020-05-28 16:33:31 -07:00
|
|
|
export function rewriteErrorMessage(e: Error, newMessage: string): Error {
|
|
|
|
if (e.stack) {
|
|
|
|
const index = e.stack.indexOf(e.message);
|
|
|
|
if (index !== -1)
|
|
|
|
e.stack = e.stack.substring(0, index) + newMessage + e.stack.substring(index + e.message.length);
|
|
|
|
}
|
|
|
|
e.message = newMessage;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2021-02-24 17:08:27 +01:00
|
|
|
const PW_LIB_DIRS = [
|
|
|
|
'playwright',
|
|
|
|
'playwright-chromium',
|
|
|
|
'playwright-firefox',
|
|
|
|
'playwright-webkit',
|
2021-06-04 18:43:54 -07:00
|
|
|
path.join('@playwright', 'test'),
|
2021-06-28 13:27:38 -07:00
|
|
|
].map(packageName => path.sep + packageName);
|
2021-02-24 17:08:27 +01:00
|
|
|
|
2021-06-28 13:27:38 -07:00
|
|
|
const runnerLib = path.join('@playwright', 'test', 'lib', 'test');
|
|
|
|
const runnerSrc = path.join('src', 'test');
|
|
|
|
|
|
|
|
export type ParsedStackTrace = {
|
|
|
|
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-02-10 18:52:28 -08:00
|
|
|
const frames: StackFrame[] = [];
|
2021-06-28 13:27:38 -07:00
|
|
|
const frameTexts: string[] = [];
|
|
|
|
const lines = stack.split('\n').reverse();
|
|
|
|
let apiName = '';
|
|
|
|
|
|
|
|
const isTesting = process.env.PWTEST_CLI_ALLOW_TEST_COMMAND || isUnderTest();
|
|
|
|
|
|
|
|
for (const line of lines) {
|
2021-02-10 18:52:28 -08:00
|
|
|
const frame = stackUtils.parseLine(line);
|
|
|
|
if (!frame || !frame.file)
|
|
|
|
continue;
|
|
|
|
if (frame.file.startsWith('internal'))
|
|
|
|
continue;
|
|
|
|
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-02-12 10:11:30 -08:00
|
|
|
continue;
|
2021-06-28 13:27:38 -07:00
|
|
|
if (!fileName.includes(runnerLib) && !(isTesting && fileName.includes(runnerSrc)) && PW_LIB_DIRS.map(p => path.join(p, isTesting ? 'src' : 'lib')).some(libDir => fileName.includes(libDir))) {
|
|
|
|
apiName = frame.function ? frame.function[0].toLowerCase() + frame.function.slice(1) : '';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
frameTexts.push(line);
|
2021-02-10 18:52:28 -08:00
|
|
|
frames.push({
|
|
|
|
file: fileName,
|
|
|
|
line: frame.line,
|
|
|
|
column: frame.column,
|
|
|
|
function: frame.function,
|
|
|
|
});
|
|
|
|
}
|
2021-06-28 13:27:38 -07:00
|
|
|
frames.reverse();
|
|
|
|
frameTexts.reverse();
|
|
|
|
return { frames, frameTexts, apiName };
|
2021-02-10 18:52:28 -08: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,
|
|
|
|
};
|
|
|
|
}
|