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

Replace lodash methods #2454

Merged
merged 10 commits into from
Mar 12, 2024
Merged

Replace lodash methods #2454

merged 10 commits into from
Mar 12, 2024

Conversation

roggervalf
Copy link
Collaborator

No description provided.

@@ -35,7 +34,7 @@ export default (

process.on('uncaughtException', async (err: Error) => {
if (!err.message) {
err = new Error(toString(err));
err = new Error(`${err}`);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect that err gets into this line so converting it as a string should be sufficient

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, it is not really the same actually. As the issue here is that we do not know what err is. We are doing a lousy check to see if err contains a message, if not we just try to convert whatever err is to a string.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this event is only receiving Error objects https://nodejs.org/api/process.html#event-uncaughtexception, do we have another example when this is not true? If not looks like ${err} will be sufficient 🤔

Copy link
Contributor

@manast manast Mar 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, but then why are we doing this:

if (!err.message) 

If err is guaranteed an Error, then it should not be needed to do that check I think.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah agree, let me remove that check

Copy link
Contributor

@manast manast left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good initiative. I think we need to think more about toString, or worst case implement a similar function ourselves.

@@ -35,7 +34,7 @@ export default (

process.on('uncaughtException', async (err: Error) => {
if (!err.message) {
err = new Error(toString(err));
err = new Error(`${err}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, it is not really the same actually. As the issue here is that we do not know what err is. We are doing a lousy check to see if err contains a message, if not we just try to convert whatever err is to a string.

@@ -44,7 +43,12 @@ const optsDecodeMap = {
rdof: 'removeDependencyOnFailure',
};

const optsEncodeMap = invert(optsDecodeMap);
const optsEncodeMap = Object.entries(optsDecodeMap).reduce<
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be beneficial to refactor this code into a separate function (invertObject for example) so that we can more easily understand what this code is actually doing.

@manast
Copy link
Contributor

manast commented Mar 3, 2024

I wrote this simple experiment:

process.on("uncaughtException", function (err) {
  console.log("Caught exception: " + err);
  console.log(err);
  console.log(err.stack);
});

throw new Error("This will crash");

// throw ["This will crash too!"]

It is actually not guaranteed that err will be an Error, it can be anything that has been thrown... so the correct signature should be:

process.on("uncaughtException", function (err: any)

Now, I do not know if there is a lot of value in using the lodash toString method anyway, as at least from BullMQ we should always make sure that we throw errors and not other things, however it is important to make this handler crash proof, but I think it is too with the new code.

@@ -33,10 +32,7 @@ export default (
process.on('SIGTERM', () => childProcessor.waitForCurrentJobAndExit());
process.on('SIGINT', () => childProcessor.waitForCurrentJobAndExit());

process.on('uncaughtException', async (err: Error) => {
if (!err.message) {
err = new Error(toString(err));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes me a bit nervous, not sure if this is the correct thing to do. So if the error does not include .message we assume it is not an error object, but more likely just a string, and thus we create a new error here, which will create also an error stack. If we do not do this, then we call errorToJSON with a string , will this yield this a correct "value"? This uncaughException is emitted in userland code, out of our control... can we track who did add this code to see if we can understand the original intend?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see that this change was added last year with worker threads support 0820985#diff-dba50fb04784a53f9826c6cb96929de2d71329d82f1d97418a13fc54a955f8d5R37-R39

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that in order not to create a regression (even though no test is testing this feature), we should implement our own toString then if we want to get rid of lodash.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok, you did already 👍

@roggervalf roggervalf merged commit 5612d5b into master Mar 12, 2024
11 checks passed
@roggervalf roggervalf deleted the replace-lodash-methods branch March 12, 2024 02:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants