stackTrace.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.addInternalStackPrefix = void 0;
  6. exports.captureLibraryStackText = captureLibraryStackText;
  7. exports.captureLibraryStackTrace = captureLibraryStackTrace;
  8. exports.captureRawStack = captureRawStack;
  9. exports.formatCallLog = formatCallLog;
  10. exports.rewriteErrorMessage = rewriteErrorMessage;
  11. exports.splitErrorMessage = splitErrorMessage;
  12. exports.stringifyStackFrames = stringifyStackFrames;
  13. var _path = _interopRequireDefault(require("path"));
  14. var _utilsBundle = require("../utilsBundle");
  15. var _ = require("./");
  16. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  17. /**
  18. * Copyright (c) Microsoft Corporation.
  19. *
  20. * Licensed under the Apache License, Version 2.0 (the "License");
  21. * you may not use this file except in compliance with the License.
  22. * You may obtain a copy of the License at
  23. *
  24. * http://www.apache.org/licenses/LICENSE-2.0
  25. *
  26. * Unless required by applicable law or agreed to in writing, software
  27. * distributed under the License is distributed on an "AS IS" BASIS,
  28. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  29. * See the License for the specific language governing permissions and
  30. * limitations under the License.
  31. */
  32. function rewriteErrorMessage(e, newMessage) {
  33. var _e$stack;
  34. const lines = (((_e$stack = e.stack) === null || _e$stack === void 0 ? void 0 : _e$stack.split('\n')) || []).filter(l => l.startsWith(' at '));
  35. e.message = newMessage;
  36. const errorTitle = `${e.name}: ${e.message}`;
  37. if (lines.length) e.stack = `${errorTitle}\n${lines.join('\n')}`;
  38. return e;
  39. }
  40. const CORE_DIR = _path.default.resolve(__dirname, '..', '..');
  41. const COVERAGE_PATH = _path.default.join(CORE_DIR, '..', '..', 'tests', 'config', 'coverage.js');
  42. const internalStackPrefixes = [CORE_DIR];
  43. const addInternalStackPrefix = prefix => internalStackPrefixes.push(prefix);
  44. exports.addInternalStackPrefix = addInternalStackPrefix;
  45. function captureRawStack() {
  46. const stackTraceLimit = Error.stackTraceLimit;
  47. Error.stackTraceLimit = 50;
  48. const error = new Error();
  49. const stack = error.stack || '';
  50. Error.stackTraceLimit = stackTraceLimit;
  51. return stack.split('\n');
  52. }
  53. function captureLibraryStackTrace(rawStack) {
  54. const stack = rawStack || captureRawStack();
  55. const isTesting = (0, _.isUnderTest)();
  56. let parsedFrames = stack.map(line => {
  57. const frame = (0, _utilsBundle.parseStackTraceLine)(line);
  58. if (!frame || !frame.file) return null;
  59. if (!process.env.PWDEBUGIMPL && isTesting && frame.file.includes(COVERAGE_PATH)) return null;
  60. const isPlaywrightLibrary = frame.file.startsWith(CORE_DIR);
  61. const parsed = {
  62. frame,
  63. frameText: line,
  64. isPlaywrightLibrary
  65. };
  66. return parsed;
  67. }).filter(Boolean);
  68. let apiName = '';
  69. // Deepest transition between non-client code calling into client
  70. // code is the api entry.
  71. for (let i = 0; i < parsedFrames.length - 1; i++) {
  72. const parsedFrame = parsedFrames[i];
  73. if (parsedFrame.isPlaywrightLibrary && !parsedFrames[i + 1].isPlaywrightLibrary) {
  74. apiName = apiName || normalizeAPIName(parsedFrame.frame.function);
  75. break;
  76. }
  77. }
  78. function normalizeAPIName(name) {
  79. if (!name) return '';
  80. const match = name.match(/(API|JS|CDP|[A-Z])(.*)/);
  81. if (!match) return name;
  82. return match[1].toLowerCase() + match[2];
  83. }
  84. // This is for the inspector so that it did not include the test runner stack frames.
  85. parsedFrames = parsedFrames.filter(f => {
  86. if (process.env.PWDEBUGIMPL) return true;
  87. if (internalStackPrefixes.some(prefix => f.frame.file.startsWith(prefix))) return false;
  88. return true;
  89. });
  90. return {
  91. frames: parsedFrames.map(p => p.frame),
  92. apiName
  93. };
  94. }
  95. function stringifyStackFrames(frames) {
  96. const stackLines = [];
  97. for (const frame of frames) {
  98. if (frame.function) stackLines.push(` at ${frame.function} (${frame.file}:${frame.line}:${frame.column})`);else stackLines.push(` at ${frame.file}:${frame.line}:${frame.column}`);
  99. }
  100. return stackLines;
  101. }
  102. function captureLibraryStackText() {
  103. const parsed = captureLibraryStackTrace();
  104. return stringifyStackFrames(parsed.frames).join('\n');
  105. }
  106. function splitErrorMessage(message) {
  107. const separationIdx = message.indexOf(':');
  108. return {
  109. name: separationIdx !== -1 ? message.slice(0, separationIdx) : '',
  110. message: separationIdx !== -1 && separationIdx + 2 <= message.length ? message.substring(separationIdx + 2) : message
  111. };
  112. }
  113. function formatCallLog(log) {
  114. if (!log || !log.some(l => !!l)) return '';
  115. return `
  116. Call log:
  117. ${_utilsBundle.colors.dim('- ' + (log || []).join('\n - '))}
  118. `;
  119. }