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

#327 notifyAsync #684

Merged
merged 5 commits into from
Jan 20, 2022
Merged

#327 notifyAsync #684

merged 5 commits into from
Jan 20, 2022

Conversation

subzero10
Copy link
Member

@subzero10 subzero10 commented Jan 15, 2022

Status

READY

Description

Related Issue: #327

I came up with a simple implementation of notify that returns a promise (notifyAsync).
It all became simple because now we always call the afterNotify hook, so I used it to know when to resolve/reject the promise.
I decided to go for this because of our recent work on AWS Lambda (see here and here). The fire-and-forget approach does not work well with Lambda functions.

Let me know what you think.

Note: The target branch is aws_lambda_async_#677

Todos

  • Tests
  • Documentation
  • Changelog Entry (unreleased)

@subzero10 subzero10 requested review from joshuap and shalvah January 15, 2022 22:45
@subzero10 subzero10 self-assigned this Jan 15, 2022
@subzero10 subzero10 changed the base branch from master to aws_lambda_async_#677 January 15, 2022 22:46
Copy link
Contributor

@shalvah shalvah left a comment

Choose a reason for hiding this comment

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

Great! Love the test coverage. I do wonder, though—isn't there a global version of afterNotify? From the docs:

Honeybadger.afterNotify((err, notice) => {
  if (err) { return console.log(`Honeybadger notification failed: ${err}`); }
  console.log(`Honeybadger notice: https://app.honeybadger.io/notice/${notice.id}`);
});

I didn't see that covered here.

@subzero10
Copy link
Member Author

subzero10 commented Jan 17, 2022

Great! Love the test coverage. I do wonder, though—isn't there a global version of afterNotify? From the docs:

Honeybadger.afterNotify((err, notice) => {
  if (err) { return console.log(`Honeybadger notification failed: ${err}`); }
  console.log(`Honeybadger notice: https://app.honeybadger.io/notice/${notice.id}`);
});

I didn't see that covered here.

Thanks!
I wanted this afterNotify hook to be the last to be called.

And here's what I just realized:

  • The afterNotify hook that you specify with Honeybadger.notify() is actually called first.
// util.ts
export function runAfterNotifyHandlers(notice: Notice | null, handlers: AfterNotifyHandler[], error?: Error): boolean {
  if (notice && notice.afterNotify) {
    handlers.unshift(notice.afterNotify)
  }

  for (let i = 0, len = handlers.length; i < len; i++) {
    handlers[i](error, notice)
  }
  return true
}

I think I can just change the code to:

return new Promise((resolve, reject) => {
  this.afterNotify((err) => {
          if (err) {
            return reject(err)
          }
          resolve()
  })
  this.notify(noticeable, name, extra)
})

and skip all the block of code of objectToOverride. Then add a test to check that this hook is indeed called last.

@shalvah
Copy link
Contributor

shalvah commented Jan 17, 2022

Okay, that sounds cool.

Glad we're adding this. Hopefully we can even get rid of afterNotify in the future. It's a bad pattern, basically reimplementing continuations, which Node.js has supported since forever (callbacks, promises, events).

@subzero10
Copy link
Member Author

@joshuap Waiting for your feedback on this. We should also mention notifyAsync in the documentation, but I'm not sure how to. notifyAsync is a must for lambda functions, but optional server or browser.

@subzero10
Copy link
Member Author

Glad we're adding this. Hopefully we can even get rid of afterNotify in the future. It's a bad pattern, basically reimplementing continuations, which Node.js has supported since forever (callbacks, promises, events).

Sounds like another topic for discussion!

@shalvah
Copy link
Contributor

shalvah commented Jan 18, 2022

On second thought, since notify might be called by code the end user, I guess it's not a bad pattern at all.

Copy link
Member

@joshuap joshuap left a comment

Choose a reason for hiding this comment

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

Nice, I like the approach! See my comment below.

src/core/client.ts Outdated Show resolved Hide resolved
@subzero10 subzero10 merged commit b26566a into aws_lambda_async_#677 Jan 20, 2022
@shalvah shalvah deleted the notify_async branch January 22, 2022 11:44
joshuap pushed a commit that referenced this pull request Feb 2, 2022
* change lambdaHandler wrapper to return async handler #677

* aws lambda example project

* README in example project and CHANGELOG

* add temp logs

* fix README

* throw err in afterNotify

* debugging new aws lambda handler impl

* debugging new aws lambda handler impl

* adding logs

* wip

* wip

* remove aws default listener

* remove aws default listener

* remove logs

* remove logs

* #327 notifyAsync (#684)

* notifyAsync + tests

* simplify implementation

* changelog

* add test

* revert to original implementation + test

* update tests

* return isomorphic handler + tests

* make input and output generic in handler

* use any so users will not be forced to specify type

* CHANGELOG.md

* aws lambda example project, point to future version
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