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';
|
2022-06-30 21:17:08 +02:00
|
|
|
import { parseStackTraceLine } from '../utilsBundle';
|
2022-04-07 12:55:44 -08:00
|
|
|
import { isUnderTest } from './';
|
2023-02-24 18:36:15 -08:00
|
|
|
import type { StackFrame } from '@protocol/channels';
|
2023-10-12 11:05:34 -07:00
|
|
|
import { colors } from '../utilsBundle';
|
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-10-11 10:52:17 -04:00
|
|
|
const CORE_DIR = path.resolve(__dirname, '..', '..');
|
2021-07-22 20:29:36 +02:00
|
|
|
|
2023-02-21 14:15:11 -08:00
|
|
|
const internalStackPrefixes = [
|
|
|
|
CORE_DIR,
|
2023-01-17 22:38:30 +01:00
|
|
|
];
|
2023-02-21 14:15:11 -08:00
|
|
|
export const addInternalStackPrefix = (prefix: string) => internalStackPrefixes.push(prefix);
|
2023-01-17 22:38:30 +01:00
|
|
|
|
2023-02-24 12:17:03 -08:00
|
|
|
export type RawStack = string[];
|
|
|
|
|
|
|
|
export function captureRawStack(): RawStack {
|
2021-04-29 09:28:19 -07:00
|
|
|
const stackTraceLimit = Error.stackTraceLimit;
|
2023-05-03 13:53:02 -07:00
|
|
|
Error.stackTraceLimit = 50;
|
2021-06-28 13:27:38 -07:00
|
|
|
const error = new Error();
|
2023-02-24 12:17:03 -08:00
|
|
|
const stack = error.stack || '';
|
2021-04-29 09:28:19 -07:00
|
|
|
Error.stackTraceLimit = stackTraceLimit;
|
2023-02-24 12:17:03 -08:00
|
|
|
return stack.split('\n');
|
2021-11-18 22:30:09 -08:00
|
|
|
}
|
|
|
|
|
2024-04-16 12:51:20 -07:00
|
|
|
export function captureLibraryStackTrace(): { frames: StackFrame[], apiName: string } {
|
|
|
|
const stack = captureRawStack();
|
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;
|
2023-02-21 14:15:11 -08:00
|
|
|
isPlaywrightLibrary: boolean;
|
2021-08-16 17:06:38 -07:00
|
|
|
};
|
2023-02-24 12:17:03 -08:00
|
|
|
let parsedFrames = stack.map(line => {
|
2023-02-21 19:24:17 -08:00
|
|
|
const frame = parseStackTraceLine(line);
|
2023-02-24 18:36:15 -08:00
|
|
|
if (!frame || !frame.file)
|
2021-10-26 12:45:53 -08:00
|
|
|
return null;
|
2023-02-24 18:36:15 -08:00
|
|
|
const isPlaywrightLibrary = frame.file.startsWith(CORE_DIR);
|
2021-08-16 17:06:38 -07:00
|
|
|
const parsed: ParsedFrame = {
|
2023-02-24 18:36:15 -08:00
|
|
|
frame,
|
2021-08-16 17:06:38 -07:00
|
|
|
frameText: line,
|
2023-02-21 14:15:11 -08:00
|
|
|
isPlaywrightLibrary
|
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 = '';
|
2023-02-21 14:15:11 -08:00
|
|
|
|
2023-02-24 12:17:03 -08:00
|
|
|
// Deepest transition between non-client code calling into client
|
2023-02-21 14:15:11 -08:00
|
|
|
// code is the api entry.
|
2022-03-14 19:01:13 -06:00
|
|
|
for (let i = 0; i < parsedFrames.length - 1; i++) {
|
2023-02-21 14:15:11 -08:00
|
|
|
const parsedFrame = parsedFrames[i];
|
|
|
|
if (parsedFrame.isPlaywrightLibrary && !parsedFrames[i + 1].isPlaywrightLibrary) {
|
|
|
|
apiName = apiName || normalizeAPIName(parsedFrame.frame.function);
|
2022-03-14 19:01:13 -06:00
|
|
|
break;
|
2021-06-28 13:27:38 -07:00
|
|
|
}
|
2021-02-10 18:52:28 -08:00
|
|
|
}
|
2021-04-19 22:37:38 +02:00
|
|
|
|
2021-11-11 17:46:17 +01:00
|
|
|
function normalizeAPIName(name?: string): string {
|
|
|
|
if (!name)
|
|
|
|
return '';
|
|
|
|
const match = name.match(/(API|JS|CDP|[A-Z])(.*)/);
|
|
|
|
if (!match)
|
|
|
|
return name;
|
|
|
|
return match[1].toLowerCase() + match[2];
|
|
|
|
}
|
|
|
|
|
2023-02-21 14:15:11 -08:00
|
|
|
// This is for the inspector so that it did not include the test runner stack frames.
|
|
|
|
parsedFrames = parsedFrames.filter(f => {
|
2022-08-01 13:44:59 -07:00
|
|
|
if (process.env.PWDEBUGIMPL)
|
|
|
|
return true;
|
2023-02-21 14:15:11 -08:00
|
|
|
if (internalStackPrefixes.some(prefix => f.frame.file.startsWith(prefix)))
|
2021-10-25 10:49:59 -08:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2021-08-16 17:06:38 -07:00
|
|
|
return {
|
|
|
|
frames: parsedFrames.map(p => p.frame),
|
|
|
|
apiName
|
|
|
|
};
|
2021-07-22 20:29:36 +02:00
|
|
|
}
|
|
|
|
|
2023-10-09 17:04:16 -07:00
|
|
|
export function stringifyStackFrames(frames: StackFrame[]): string[] {
|
|
|
|
const stackLines: string[] = [];
|
|
|
|
for (const frame of frames) {
|
|
|
|
if (frame.function)
|
|
|
|
stackLines.push(` at ${frame.function} (${frame.file}:${frame.line}:${frame.column})`);
|
|
|
|
else
|
|
|
|
stackLines.push(` at ${frame.file}:${frame.line}:${frame.column}`);
|
|
|
|
}
|
|
|
|
return stackLines;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function captureLibraryStackText() {
|
|
|
|
const parsed = captureLibraryStackTrace();
|
|
|
|
return stringifyStackFrames(parsed.frames).join('\n');
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|
2023-02-24 12:17:03 -08:00
|
|
|
|
2023-10-12 11:05:34 -07:00
|
|
|
export function formatCallLog(log: string[] | undefined): string {
|
|
|
|
if (!log || !log.some(l => !!l))
|
|
|
|
return '';
|
|
|
|
return `
|
|
|
|
Call log:
|
|
|
|
${colors.dim('- ' + (log || []).join('\n - '))}
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2023-02-24 12:17:03 -08:00
|
|
|
export type ExpectZone = {
|
|
|
|
title: string;
|
2024-05-14 12:10:46 -07:00
|
|
|
stepId: string;
|
2023-02-24 12:17:03 -08:00
|
|
|
};
|