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

Handle errors with promise rejections #695

Merged
merged 1 commit into from
May 10, 2020
Merged

Handle errors with promise rejections #695

merged 1 commit into from
May 10, 2020

Conversation

ankon
Copy link
Contributor

@ankon ankon commented Mar 6, 2020

The code used promises, but in most cases dropped errors and rejects "on the floor", rather than just
exposing them further to the caller. This made it impossible to actually catch these problems, leading
to NodeJS warnings about "unhandled promise rejections" (which will soon make the NodeJS process
crash!)


This is related to #678, and at least allows to catch the errors.

The fixes follow a couple of patterns:

  1. Remove useless wrapping promises when there is already one available

    return new Promise((resolve, _reject) => doSomePromiseCode.then((result) => resolve(result)))

    This can be written as simply return doSomePromiseCode, which also makes the caller able to handle the rejection.

  2. Move the use of new Promise() calls for callbacks as close to the callback as possible.

  3. Make sure to return promises (for example the result of Promise.all)

Note that this does still contain the Presenation typo in writeFile, see #694 for that.

@ankon
Copy link
Contributor Author

ankon commented Mar 6, 2020

I've tested this by updating our application to use a pptxgenjs distribution compiled from this PR, and seeing that we now correctly catch the errors caused by undefined text given to addSlide in the saving path (see #678 for that problem)

})
return this.exportPresentation(outputType)
.catch(ex => {
throw new Error(ex.message + '\nDid you mean to use writeFile() instead?')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

FWIW: This kills the stack trace of the actual exception, which seriously sucks for debugging. I would suggest to actually drop this as well, and just do:

write(outputType: JSZIP_OUTPUT_TYPE): Promise<string | ArrayBuffer | Blob | Buffer | Uint8Array> {
	return this.exportPresentation(outputType)
}

@ankon
Copy link
Contributor Author

ankon commented Mar 24, 2020

@gitbrent do you maybe have some time to look over this? Right now the PptxGenJS library kills our processes due to the unhandled rejections, and this patch seems to fix things nicely -- but of course we don't want to run with local patches if possible :)

@ankon
Copy link
Contributor Author

ankon commented Apr 10, 2020

Anything I can do to help move this forward?

@ankon
Copy link
Contributor Author

ankon commented Apr 28, 2020

Link to #719 (fixed also by #720).

The code used promises, but in most cases dropped errors and rejects "on the floor", rather than just
exposing them further to the caller. This made it impossible to actually catch these problems, leading
to NodeJS warnings about "unhandled promise rejections" (which will soon make the NodeJS process
crash!)
@ankon
Copy link
Contributor Author

ankon commented May 4, 2020

Rebased onto current master to resolve the conflict introduced through #694

@gitbrent
Copy link
Owner

Thanks @ankon - great work! Appreciate the Promise expertise.

@ankon ankon deleted the pr/export-promises branch May 11, 2020 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

UnhandledPromiseRejectionWarning (node) / Uncaught (in promise) (browser) when passing text as undefined
2 participants