replay icon indicating copy to clipboard operation
replay copied to clipboard

Improve error handling

Open OrKoN opened this issue 2 years ago • 3 comments

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.

OrKoN avatar Apr 06 '22 08:04 OrKoN

What is the status on this issue? Can we maybe add an onError hook that is called if a step throws an error?

pbkompasz avatar Jul 26 '23 13:07 pbkompasz

+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

davidlaprade avatar Sep 13 '23 15:09 davidlaprade

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;

youhackme avatar Oct 02 '23 21:10 youhackme