-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
doc: suggest not to throw JS errors from C++ #18149
Conversation
Also provide an example on how to use internal/errors to handle errors in C++.
CPP_STYLE_GUIDE.md
Outdated
|
||
A lot of code inside Node.js is written so that typechecking etc. is performed | ||
in JavaScript. | ||
Note that in general, type-checks on arguments should done in JavaScript |
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.
should be done
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.
@targos Fixed, thanks for catching that!
CPP_STYLE_GUIDE.md
Outdated
throw errors.Error('ERR_ERROR_NAME', ctx.code); | ||
} | ||
return result; | ||
} |
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.
A nit: missing semicolon.
CPP_STYLE_GUIDE.md
Outdated
const ctx = {}; | ||
const result = binding.foo(str, ctx); | ||
if (ctx.code !== undefined) { | ||
throw errors.Error('ERR_ERROR_NAME', ctx.code); |
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.
Missing new
?
CPP_STYLE_GUIDE.md
Outdated
A lot of code inside Node.js is written so that typechecking etc. is performed | ||
in JavaScript. | ||
Note that in general, type-checks on arguments should be done in JavaScript | ||
before the arguments are passed into C++. |
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.
Perhaps add something about including a CHECK
at the C++ level?
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.
LGTM
### Avoid throwing JavaScript errors in nested C++ methods | ||
|
||
When you have to throw the errors from C++, try to do it at the top level and | ||
not inside of nested calls. |
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.
"Maybe" mention Maybe<>
and MaybeLocal<>
as possible methods to pass exception status if exceptions have to be transported through nested C++ calls.
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.
Hmm I think we could make a separate section for the Maybes and link from here?
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.
Either is fine.
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.
@TimothyGu Since this was how it's documented prior to this PR, I'll open a separate PR for the usage of Maybe
s. We also need a section about using the Maybe APIs instead of the deprecated V8 APIs.
Landed in 27925c4, thanks! |
Also provide an example on how to use internal/errors to handle errors in C++. PR-URL: #18149 Refs: #18106 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Weijia Wang <starkwang@126.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Also provide an example on how to use internal/errors to handle errors in C++. PR-URL: #18149 Refs: #18106 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Weijia Wang <starkwang@126.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Also provide an example on how to use internal/errors to handle errors in C++. PR-URL: #18149 Refs: #18106 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Weijia Wang <starkwang@126.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Also provide an example on how to use internal/errors
to handle errors in C++.
Refs: #18106
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
doc