replay
replay copied to clipboard
Improve error handling
For example, should the hooks like afterEach/afterAll be invoked when an error happens? I think it'd be expected if they are still invoked on errors.
What is the status on this issue?
Can we maybe add an onError
hook that is called if a step throws an error?
+1 on adding an onError
hook.
It'd be really great not to have to call runner.run()
in a try block -- especially given how brittle some recordings can turn out to be on certain sites
I ended up in the same situation and after hours of digging into this package code, I finally found the solution. We can capture the errors from WITHIN the extension by using the runStep
hook, which runs every time a step is executed. Here's a simple example:
import {PuppeteerRunnerExtension, Step, getSelectorType, selectorToPElementSelector} from "@puppeteer/replay"
class ScreenshotExtension extends PuppeteerRunnerExtension {
async runStep(step: Step, flow: UserFlow) {
try {
await super.runStep(step, flow)
} catch (error) {
let message = 'Unknown Error'
if (error instanceof Error) message = error.message
if (message.includes('waitForElement timed out')) {
throw new Error(`The selector(s) ${step.selectors.join()} is either not present or took too long to render.`)
}
throw new Error(message)
}
}
}
export default ScreenshotExtension;