-
Notifications
You must be signed in to change notification settings - Fork 3
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
added more protections for errors in callback #24
Conversation
Listener.js
Outdated
}; | ||
|
||
const onMessageUserHandling = (msg) => { | ||
let callback = onMessageCallback(msg); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this only works when onMessageCallback is synchronous. The response in this instance isn't really a callback. If onMessageCallback was a standard callback function, I'd have thought the structure of the method would be more like:
const onMessageUserHandling = (msg) => {
onMessageCallback(msg, err => {
if (err) {
console.error(`Error from callback: ${err}`);
msg.nack();
process.exit(1);
} else {
msg.ack();
}
});
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @jfmesse, I took a look at your proposed solution, but it hasn't clicked for me. Apologies, as I am not an experienced Node developer. However, the callback is intended to be provided by the user. It's what you want to be executed when a message is received. I'm not sure how passing an arrow function as an input in the callback addresses this issue.
Since this is an open repo, if you like you may submit your desired changes as a PR and I will evaluate and test them out. Otherwise I'll take a second pass and see what can be done. Thanks for bearing with us.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi again @jfmesse I took a second look at this, adding the error handling as a callback the way you suggested. I was confused initially, since it's a callback in a callback. Can you see if this works for you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @zakagan , sorry I missed this. Your changes look good to me. We have a developer working on the ingest component now who will be looking to use. Thank you.
Listener.js
Outdated
} else { | ||
console.error(`Error from callback: ${callback.err}\n`); | ||
msg.nack(); | ||
process.exit(1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's of lesser concern, but process.exit(1) feels a little brutal way of failing. It takes away the ability of the application to cleanly handle the client having a fatal error.
msg.ack(); | ||
const onMessageTryCatch = (msg) => { | ||
try { | ||
onMessageCallback(msg); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note the try catch behaviour only works with onMessageCallback being a synchronous method. If it was a promise or standard callback function, the try catch would not be triggered
added more protections for errors in callback, plus an option for the user to control their own error handling as part of the callback