puppeteer-extra icon indicating copy to clipboard operation
puppeteer-extra copied to clipboard

[Bug] Type mismatch when throwing error

Open khalitovsv opened this issue 3 years ago • 1 comments

Describe the bug

When setting throwOnError to true, plugnin thwows response.error object as a string message,

index.cjs.js:811:13

console.warn('PuppeteerExtraPluginRecaptcha: An error occured during "getRecaptchaSolutions":', response.error);

output:

PuppeteerExtraPluginRecaptcha: An error occured during "getRecaptchaSolutions": {
  _vendor: 'recaptcha',
  provider: '2captcha',
  id: 'eoajizvv30ew',
  requestAt: 2021-02-07T11:20:16.352Z,
  error: 'Error: 2captcha error: Missing response data: undefined'
}

So, response.error is an object.

index.cjs.js:813:9

if (this.opts.throwOnError && response.error) {
    throw new Error(response.error); // There you sending Object as a String
}

And when we capturing an error we get this:

Error: Error: [object Object]
    at PuppeteerExtraPluginRecaptcha.solveRecaptchas ( ...... /node_modules/puppeteer-extra-plugin-recaptcha/dist/index.cjs.js:869:19)

khalitovsv avatar Feb 07 '21 11:02 khalitovsv

You need own Error, for example:

class PuppeteerExtraPluginRecaptchaError extends Error {
    constructor(message, data) {
        super(message);
        this.name = this.constructor.name;
        this.data = data;
    }
}

and change this

if (this.opts.throwOnError && response.error) {
    throw new Error(response.error);
}

to that

if (response && response.error && this.opts.throwOnError ) {
    throw new PuppeteerExtraPluginRecaptchaError(response.error.error, response.error);
}

all over the code

khalitovsv avatar Feb 07 '21 11:02 khalitovsv