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

added more protections for errors in callback #24

Merged
merged 3 commits into from
May 3, 2021

Conversation

zakagan
Copy link
Collaborator

@zakagan zakagan commented Apr 12, 2021

added more protections for errors in callback, plus an option for the user to control their own error handling as part of the callback

Listener.js Outdated
};

const onMessageUserHandling = (msg) => {
let callback = onMessageCallback(msg);
Copy link

@jfmesse jfmesse Apr 15, 2021

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();
    }
  });
}

Copy link
Collaborator Author

@zakagan zakagan Apr 21, 2021

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.

Copy link
Collaborator Author

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?

Copy link

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);
Copy link

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);
Copy link

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

@zakagan zakagan merged commit 3b8dbb1 into master May 3, 2021
@zakagan zakagan deleted the feature/error-handling-improvements branch May 3, 2021 14:30
@zakagan zakagan restored the feature/error-handling-improvements branch May 3, 2021 14:30
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.

3 participants