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

Allow OnErrorCallbacks to return Promise<boolean> #1224

Merged
merged 5 commits into from
Jan 13, 2021

Conversation

imjoehaines
Copy link
Member

Goal

Currently an async OnErrorCallback can only return Promise<void> in the Typescript, which means they can't be used to prevent events from sending without causing a compile error. Returning/resolving a boolean value does work, so we can safely allow Promise<boolean> as a return type

@imjoehaines imjoehaines marked this pull request as ready for review January 6, 2021 15:05
@github-actions
Copy link

github-actions bot commented Jan 6, 2021

@bugsnag/browser bundle size diff

Minified Minfied + Gzipped
Before 40.70 kB 12.54 kB
After 40.70 kB 12.54 kB
± No change No change

Generated by 🚫 dangerJS against 2ad9e21

Copy link
Contributor

@bengourley bengourley left a comment

Choose a reason for hiding this comment

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

Changes look good, but since we're in this area I checked the node-style callback for correctness too and found it was also incorrect.

It's hard to get the typing strict enough that it doesn't let people write invalid sync/async without preventing people from writing valid node-style callbacks. I had a quick go and got close, but the never in this example makes it unusable for non-trivial node-style callbacks…

image

@imjoehaines
Copy link
Member Author

imjoehaines commented Jan 7, 2021

From some more testing, it looks like we can't make it perfectly strict because of how void works; I've pushed a fix for node style callbacks and split out each type so it's easier to see the three valid signatures nevermind, this breaks inference of event

Because of void you can still do 'invalid' things like returning an array, but we ignore anything that's not false so this shouldn't actually be a problem

@imjoehaines imjoehaines requested review from bengourley and removed request for bengourley January 7, 2021 10:07
Copy link
Contributor

@bengourley bengourley left a comment

Choose a reason for hiding this comment

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

image

Node-style callback signature is still not good. Is this the best we can do given the limitations?

@imjoehaines
Copy link
Member Author

Node-style callback signature is still not good. Is this the best we can do given the limitations?

The alternatives are to either make it a union type, which makes event any:

type OnErrorCallback
  = ((event: Event) => void | boolean)
  | ((event: Event) => Promise<void | boolean>)
  | ((event: Event, cb: (err: null | Error, shouldSend?: boolean) => void) => void)

notify(event => event.stuff) // error TS7006: Parameter 'event' implicitly has an 'any' type.

or mark cb as always present, which isn't true but does seem to fix the other problems:

type OnErrorCallback = (event: Event, cb: (err: null | Error, shouldSend?: boolean) => void) => void | boolean | Promise<void | boolean>

notify(event => event.stuff) // works
notify((event, cb) => { cb(null) }) // works

notify(() => 1) // error TS2322: Type 'number' is not assignable to type 'boolean | void | Promise<boolean | void>'.

@bengourley
Copy link
Contributor

mark cb as always present, which isn't true but does seem to fix the other problems:

I'd go with this. Marking it as always present doesn't mean people have to define it.

This allows returning/resolving false in an async callback to prevent
errors from sending. Previously this would cause a Typescript
compile error
These were missing the 'shouldSend' parameter, so couldn't prevent
sending
@imjoehaines imjoehaines merged commit 259b79e into next Jan 13, 2021
@imjoehaines imjoehaines deleted the fix-async-on-error-type branch January 13, 2021 11:14
@imjoehaines imjoehaines mentioned this pull request Jan 18, 2021
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.

None yet

2 participants