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

Support FunctionCallback returning an error, and native Exceptions. #195

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

tommie
Copy link
Contributor

@tommie tommie commented Oct 13, 2021

DRAFT: Awaiting #274 to refactor JSError.

Returning an error from a callback feels like the most Go way of supporting exceptions, and it ensures the Go code doesn't continue executing after throwing (as it would if we instead supported Isolate.ThrowException).

This also adds Error constructors, so functions can throw real JS Errors. Note that the new callback interface does allow throwing arbitrary objects, but *Exception is the only built-in object that is both a Valuer and error.

If the error is a Valuer (like an *Exception), it will be thrown as a
proper JS error, otherwise it becomes a thrown string.
@codecov
Copy link

codecov bot commented Oct 14, 2021

Codecov Report

Merging #195 (fd6c569) into master (5e91d3d) will decrease coverage by 1.23%.
The diff coverage is 85.96%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #195      +/-   ##
==========================================
- Coverage   95.86%   94.62%   -1.24%     
==========================================
  Files          17       18       +1     
  Lines         580      633      +53     
==========================================
+ Hits          556      599      +43     
- Misses         15       20       +5     
- Partials        9       14       +5     
Impacted Files Coverage Δ
exception.go 80.00% <80.00%> (ø)
function_template.go 86.66% <87.50%> (-7.09%) ⬇️
isolate.go 100.00% <100.00%> (ø)
promise.go 86.95% <100.00%> (+2.34%) ⬆️
value.go 94.91% <100.00%> (+0.08%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5e91d3d...fd6c569. Read the comment docs.

)

// NewRangeError creates a RangeError.
func NewRangeError(iso *Isolate, msg string) *Exception {
Copy link
Collaborator

Choose a reason for hiding this comment

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

There is already the JSError type which is used to return exceptions from v8go functions. It seems like there should be consistency between the exception value returned by other v8go functions and these functions to construct errors. That should make it easier to propagate exceptions, even without adding a new feature to return errors from function callbacks.

Currently, JSError is converting the underlying JS error object to a string and storing it in the Message field. Instead, it should store the underlying JS error object, so that the error can be propagated. That Message field could be deprecated in favour of a method so that the conversion can be done on-demand in the future. It looks like the stack trace can similarly be obtained on-demand using v8::Exception::GetStackTrace.

The JSError.Location field is derived from the a v8::Message on v8::TryCatch, which may be present without a stack trace for compilation errors. v8::Exception::CreateMessage can be used to obtained a v8::Message from an exception value, but we don't want to automatically create it in a getter method. Since it is a public field, it will need to continue populating on creation for the current code paths, but it could similarly be deprecated in favour of a method to obtain this on-demand for new code paths. This on-demand code path could at least obtain the when the error has a stack trace, even if we don't get it to initially work on this new code path errors with a location but no stack trace.

It seems like this refactoring and the addition of these functions to create exceptions could be split out into a separate PR.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I've opened this as an issue (#274) for greater visibility on this issue with JSError

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't remember exactly why I created a new type for it, but it was probably to avoid having to refactor JSError (avoiding merge conflicts). If there's consensus that's better, that sounds good to me. Thanks for reviewing.

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.

2 participants