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
|
|
|
|
2021-02-10 18:52:28 -08:00
|
|
|
export function getCallerFilePath(ignorePrefix: string): string | null {
|
|
|
|
const frame = captureStackTrace().frames.find(f => !f.file.startsWith(ignorePrefix));
|
|
|
|
return frame ? frame.file : null;
|
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-02-24 20:34:48 +01:00
|
|
|
].map(packageName => path.join('node_modules', packageName, 'lib'));
|
2021-02-24 17:08:27 +01:00
|
|
|
|
2021-02-10 18:52:28 -08:00
|
|
|
export function captureStackTrace(): { stack: string, frames: StackFrame[] } {
|
|
|
|
const stack = new Error().stack!;
|
|
|
|
const frames: StackFrame[] = [];
|
|
|
|
for (const line of stack.split('\n')) {
|
|
|
|
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-02-24 17:08:27 +01:00
|
|
|
if (PW_LIB_DIRS.some(libDir => fileName.includes(libDir)))
|
2021-02-10 18:52:28 -08:00
|
|
|
continue;
|
|
|
|
// for tests.
|
2021-02-24 20:34:48 +01:00
|
|
|
if (isUnderTest() && fileName.includes(path.join('playwright', 'src')))
|
2021-02-10 18:52:28 -08:00
|
|
|
continue;
|
2021-02-12 10:11:30 -08:00
|
|
|
if (isUnderTest() && fileName.includes(path.join('playwright', 'test', 'coverage.js')))
|
|
|
|
continue;
|
2021-02-10 18:52:28 -08:00
|
|
|
frames.push({
|
|
|
|
file: fileName,
|
|
|
|
line: frame.line,
|
|
|
|
column: frame.column,
|
|
|
|
function: frame.function,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return { stack, frames };
|
|
|
|
}
|