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

Fix Error constructors to return rather than throw #749

Merged
merged 10 commits into from
Oct 5, 2020

Conversation

RageKnify
Copy link
Member

@RageKnify RageKnify commented Oct 1, 2020

On master let e = new Error('test'); doesn't work and results in an uncaught error, this PR fixes that by making it so the Error constructors aren't seen as throwing their return value by boa.

Originally opened because SyntaxError.toString and Error.toString were using Value::display while all other error types use Value::to_string.
I don't think this has any effect right now, but I think the errors should all conform to the same pattern.

Has an example you can see TypeError's implementation below:

pub(crate) fn to_string(this: &Value, _: &[Value], ctx: &mut Context) -> Result<Value> {
let name = this.get_field("name").to_string(ctx)?;
let message = this.get_field("message").to_string(ctx)?;
Ok(Value::from(format!("{}: {}", name, message)))
}

It changes the following:

  • Change error constructors to wrap their return in Ok() rather than Err()
  • Change conversion of name and message to use Value::to_string rather than Value::display in SyntaxError.toString
  • With the new Builin patterns Error.prototype.toString is only defined once, for the generic Error and all its "subclasses" use that implementation, I've made that version spec compliant.

SyntaxError.toString was using Value::display while all other error
types use Value::to_string
@codecov
Copy link

codecov bot commented Oct 1, 2020

Codecov Report

Merging #749 into master will increase coverage by 0.07%.
The diff coverage is 79.16%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #749      +/-   ##
==========================================
+ Coverage   59.13%   59.21%   +0.07%     
==========================================
  Files         155      155              
  Lines        9838     9857      +19     
==========================================
+ Hits         5818     5837      +19     
  Misses       4020     4020              
Impacted Files Coverage Δ
boa/src/context.rs 70.13% <ø> (ø)
boa/src/builtins/error/mod.rs 66.66% <75.00%> (+23.18%) ⬆️
boa/src/builtins/error/range.rs 65.00% <100.00%> (ø)
boa/src/builtins/error/reference.rs 65.00% <100.00%> (ø)
boa/src/builtins/error/syntax.rs 65.00% <100.00%> (ø)
boa/src/builtins/error/type.rs 65.00% <100.00%> (ø)
boa/src/value/operations.rs 44.67% <0.00%> (+0.07%) ⬆️
boa/src/builtins/json/mod.rs 82.43% <0.00%> (+2.43%) ⬆️

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 e60d081...d4b9824. Read the comment docs.

Introduces some changes accross files to accommodate for the change
Fix: Correct Error.prototype.toString to use Value::to_string rather
than Value::Display
Test: Add a test case for Error Objects in Object.prototype.toString
@RageKnify RageKnify changed the title Make SyntaxError.toString use Value::to_string Fix Error constructors to return rather than throw Oct 1, 2020
@RageKnify RageKnify added the builtins PRs and Issues related to builtins/intrinsics label Oct 1, 2020
boa/src/builtins/error/mod.rs Outdated Show resolved Hide resolved
boa/src/builtins/error/syntax.rs Outdated Show resolved Hide resolved
@HalidOdat HalidOdat added the bug Something isn't working label Oct 1, 2020
@HalidOdat
Copy link
Member

This should now be unblocked since #722 has been merged :)

@RageKnify
Copy link
Member Author

@HalidOdat on Discord we discussed using ? when invoking construct_type_error since it will now return result, but in situations where I know it won't Error, becuase I'm giving &str for the message, should I use expect instead? Using ? might generate unnecessary code.

There are some places where I'm forced to use expect because I'm inside closures which don't return Result.

@HalidOdat
Copy link
Member

@HalidOdat on Discord we discussed using ? when invoking construct_type_error since it will now return result, but in situations where I know it won't Error, becuase I'm giving &str for the message, should I use expect instead? Using ? might generate unnecessary code.

There are some places where I'm forced to use expect because I'm inside closures which don't return Result.

If we know that it construct_type_error will not give use a Err, then we can return Value and call .expect in the construct_type_error function.

@RageKnify
Copy link
Member Author

Ok, that's my opinion too, there's no need for ? if we know it's safe to use expect, I'll make sure all my changes use expect where possible.

@RageKnify RageKnify linked an issue Oct 4, 2020 that may be closed by this pull request
@Razican
Copy link
Member

Razican commented Oct 4, 2020

Ok, that's my opinion too, there's no need for ? if we know it's safe to use expect, I'll make sure all my changes use expect where possible.

The idea here is that if at any point in our code, we should never ever reach a branch, it's good to panic. Wether that branch is an Err(), a None or their opposites (or other things, of course).

A panic should mean "we messed up, this is a bug in Boa 100% for sure", and no JavaScript code should ever cause a panic (we still have to solve this part).

But in this case, I think expect() is the way to go in many places.

boa/src/context.rs Outdated Show resolved Hide resolved
@HalidOdat HalidOdat added this to the v0.11.0 milestone Oct 4, 2020
@HalidOdat HalidOdat added the execution Issues or PRs related to code execution label Oct 4, 2020
Co-authored-by: Halid Odat <halidodat@gmail.com>
@RageKnify RageKnify merged commit dc82aa2 into boa-dev:master Oct 5, 2020
@RageKnify RageKnify deleted the fix/syntax_to_string branch October 5, 2020 18:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working builtins PRs and Issues related to builtins/intrinsics execution Issues or PRs related to code execution
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Error builtin] does not output "name" property correctly
4 participants