Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hoek.clone drop an error with axios errors in version @hapi/hoek 11.0.6+ #400

Closed
kedderli opened this issue Nov 14, 2024 · 5 comments · Fixed by #402
Closed

Hoek.clone drop an error with axios errors in version @hapi/hoek 11.0.6+ #400

kedderli opened this issue Nov 14, 2024 · 5 comments · Fixed by #402
Assignees
Labels
bug Bug or defect
Milestone

Comments

@kedderli
Copy link

kedderli commented Nov 14, 2024

Runtime

node.js

Runtime version

20.17

Module version

11.0.6

Last module version without issue

11.0.4

Used with

No response

Any other relevant information

Function structuredClone drop an error, if clonning object has functions https://github.com/hapijs/hoek/blob/v11.0.6/lib/clone.js#L177

What are you trying to achieve or the steps to reproduce?

const { Boom } = require('@hapi/boom');
const { AxiosError } = require('axios');

const err = new AxiosError('error');

console.log(err instanceof Error); // true

err.fun = () => {};

const boomError = new Boom(err);

console.log(boomError);
console.log(boomError instanceof Error);

What was the result you got?

Got an error like:
node:internal/per_context/domexception:53 ErrorCaptureStackTrace(this); ^ DOMException [DataCloneError]: function httpAdapter(config) { return new Promise(function dispatchHttpRequest(resolvePromise, rejectPromise)...<omitted>... } could not be cloned.

What result did you expect?

Haven't gotten this error on previous version of lib

@kedderli kedderli added the bug Bug or defect label Nov 14, 2024
@Marsup
Copy link
Contributor

Marsup commented Nov 14, 2024

I don't think it's generally safe to do this on errors.

You should either create your own custom error class, like so:

class CustomError extends AxiosError {
  foo() {}
}
const err = new CustomError('error');

Or add it as non-enumerable:

const error = new AxiosError('error');
Object.defineProperty(error, 'fun', { value: () => {}, enumerable: false });

@kanongil
Copy link
Contributor

@Marsup I tend to agree, but Hoek should never throw an error when cloning.

Something strange is going on here. I am unable to replicate the issue with regular errors, only AxiosError seem to trigger the issue. It seems that regular (and subclassed) errors passed to structuredClone() will not copy any "own" properties, but when created as AxiosError they suddenly will!

kanongil added a commit to kanongil/hoek that referenced this issue Nov 14, 2024
@Marsup
Copy link
Contributor

Marsup commented Nov 14, 2024

Doesn't it then become axios's problem?

@Marsup
Copy link
Contributor

Marsup commented Nov 14, 2024

Fix is released in 11.0.7, thanks @kanongil for the patch 🙏🏻

@Marsup Marsup closed this as completed Nov 14, 2024
@kedderli
Copy link
Author

Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug or defect
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants