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

doc: suggest not to throw JS errors from C++ #18149

Closed
wants to merge 3 commits into from

Conversation

joyeecheung
Copy link
Member

@joyeecheung joyeecheung commented Jan 14, 2018

Also provide an example on how to use internal/errors
to handle errors in C++.

Refs: #18106

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)

doc

Also provide an example on how to use internal/errors
to handle errors in C++.
@nodejs-github-bot nodejs-github-bot added the doc Issues and PRs related to the documentations. label Jan 14, 2018

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
Copy link
Member

Choose a reason for hiding this comment

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

should be done

Copy link
Member Author

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!

throw errors.Error('ERR_ERROR_NAME', ctx.code);
}
return result;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

A nit: missing semicolon.

const ctx = {};
const result = binding.foo(str, ctx);
if (ctx.code !== undefined) {
throw errors.Error('ERR_ERROR_NAME', ctx.code);
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing new?

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++.
Copy link
Member

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?

@joyeecheung
Copy link
Member Author

@joyeecheung joyeecheung added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Jan 15, 2018
Copy link
Member

@mhdawson mhdawson left a 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.
Copy link
Member

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.

Copy link
Member Author

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?

Copy link
Member

Choose a reason for hiding this comment

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

Either is fine.

Copy link
Member Author

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 Maybes. We also need a section about using the Maybe APIs instead of the deprecated V8 APIs.

@joyeecheung
Copy link
Member Author

Landed in 27925c4, thanks!

joyeecheung added a commit that referenced this pull request Jan 16, 2018
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 <[email protected]>
Reviewed-By: Jon Moss <[email protected]>
Reviewed-By: Weijia Wang <[email protected]>
Reviewed-By: Daniel Bevenius <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
@joyeecheung joyeecheung removed the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Jan 16, 2018
evanlucas pushed a commit that referenced this pull request Jan 30, 2018
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 <[email protected]>
Reviewed-By: Jon Moss <[email protected]>
Reviewed-By: Weijia Wang <[email protected]>
Reviewed-By: Daniel Bevenius <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
MylesBorins pushed a commit that referenced this pull request Feb 27, 2018
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 <[email protected]>
Reviewed-By: Jon Moss <[email protected]>
Reviewed-By: Weijia Wang <[email protected]>
Reviewed-By: Daniel Bevenius <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations.
Projects
None yet
Development

Successfully merging this pull request may close these issues.