Files
cypress/packages/errors/src/stackUtils.ts
T
Tyler Biethman 405e7f7ccb chore(webkit): update error stack parsing and related system tests (#23730)
* chore(webkit): update error stack parsing and related system tests

* Adding better comment

* Putting column back. Indexing at 1.

* Let's wait for WebKit fixes to land for this

* Using default name/location values already used in stack enhancing logic

* Incorrect bracket in regex

* Trying without location, as the fake location causes more problems downstream.

* Loosening regex around locations in stack replacement

* Defaulting location sans row/column

* Parsing stack lines for reporter with unique regex

* D'oh

* Making the validation against '<unknown>' more specific

* Don't want a capture group here

* Updating spec_isolation system tests

* Consolidating regex pattern to errors package

* Can just keep this global now

* Simplifying regex. Removing lineAndColumn numbers from unknown locations.

* Updating system test stack regex

* Getting better baseline

* Revert "Updating system test stack regex"

This reverts commit 2b91eff369.

* Forking normalization for webkit to track down diffs

* Ensure line or column are set before rendering in enhanced stack

* Need to be a little more flexible

* Tweaking leading newline detection

* Trying out new composed regex

* Few more tweaks for multiple leading newlines and file locations without function name

* Updating remainderOfStack pattern with proper escaping

* Cleaning up comments

* Filtering native code from absolute path logic

* Rebuild CI after outage
2022-09-12 15:40:12 -05:00

66 lines
1.9 KiB
TypeScript

import _ from 'lodash'
import type { ErrorLike } from './errorTypes'
export const stackLineRegex = /^\s*(at )?.*@?(?:\(?.*(?::\d+:\d+|<unknown>|\[native code\])+\)?)$/
type MessageLines = [string[], string[]] & {messageEnded?: boolean}
// returns tuple of [message, stack]
export const splitStack = (stack: string) => {
const lines = stack.split('\n')
return _.reduce(lines, (memo, line) => {
if (memo.messageEnded || stackLineRegex.test(line)) {
memo.messageEnded = true
memo[1].push(line)
} else {
memo[0].push(line)
}
return memo
}, [[], []] as MessageLines)
}
export const unsplitStack = (messageLines: string | string[], stackLines: string[]) => {
return _.castArray(messageLines).concat(stackLines).join('\n')
}
export const getStackLines = (stack: string) => {
const [, stackLines] = splitStack(stack)
return stackLines
}
/**
* Captures & returns the absolute path, line, and column from a stack trace line
*/
export const parseStackLine = (line: string): null | { absolute: string, line: number, column: number } => {
const stackLineCapture = /^\s*(?:at )?.*@?\((.*?)\:(\d+)\:(\d+)\)?$/
const result = stackLineCapture.exec(line)
if (!result?.[1]) {
return null
}
return { absolute: result[1], line: Number(result[2]), column: Number(result[3]) }
}
/**
* Takes the stack and returns only the lines that contain stack-frame like entries,
* matching the `stackLineRegex` above
*/
export const stackWithoutMessage = (stack: string) => {
return getStackLines(stack).join('\n')
}
export const replacedStack = (err: ErrorLike, newStack: string) => {
// if err already lacks a stack or we've removed the stack
// for some reason, keep it stackless
if (!err.stack) return err.stack
const errString = err.toString()
const stackLines = getStackLines(newStack)
return unsplitStack(errString, stackLines)
}