X Tutup
Skip to content

Allow throwing arbitrary types and add builtin error classes#724

Merged
Perryvw merged 36 commits intoTypeScriptToLua:masterfrom
TheLartians:master
Oct 9, 2019
Merged

Allow throwing arbitrary types and add builtin error classes#724
Perryvw merged 36 commits intoTypeScriptToLua:masterfrom
TheLartians:master

Conversation

@TheLartians
Copy link
Contributor

@TheLartians TheLartians commented Sep 24, 2019

This PR enables throwing custom error objects and adds the builtin error classes as defined by JavaScript. Also fixes a bug in LuaTransformer.transformClassExpression() where local class definitions had global scope.

Example

Arbitrary errors

try {
  throw {x: "Hello error object!"};
} catch (error) {
  console.log(error.x); // -> Hello error object!
}

Builtin errors

try {
  throw RangeError("message"); // or new RangeError("message")
} catch (error) {
  if (error instanceof Error) {
    console.log(error); // -> RangeError: message
    console.log(error.name); // -> RangeError
    console.log(error.message); // -> message
    console.log(error.stack); // prints stack trace from where the error was thrown
  }
}

Inheritance

class MyError extends Error{
  name: "MyError"
}

try {
  throw new MyError("message");
} catch (error) {
  // same error properties as builtin errors
} 

@TheLartians TheLartians force-pushed the master branch 3 times, most recently from 10c9bc9 to 39704ca Compare September 25, 2019 13:19
@ark120202
Copy link
Contributor

Lazy getter would be nice, however unavailable here due to #725 as it would break subclassing Error.

I've been testing on Safari. In Chrome/V8 the stack does include the error name and message before the traceback. I prefer Error.stack to only contain the traceback, but we might just as well accept the V8 as ground truth here.

Actually, looks like Error#stack isn't part of a standard at all, so we aren't ought to follow any of existing implementations. But doing what Safari does there isn't good, since .stack is usually used to print the full error text in the same way you'd see it after throwing.

There is a stage 1 proposal (rendered) which I think we should follow. It defines stack as a getter with that logic: https://tc39.es/proposal-error-stacks/#sec-getstackstring.

@TheLartians
Copy link
Contributor Author

There is a stage 1 proposal (rendered) which I think we should follow.

I agree, but for that to work we would need to use accessors, which don't work with subclassing at the moment. We can update the functionality as soon as #725 is fixed.

TheLartians and others added 4 commits October 1, 2019 09:28
Co-Authored-By: ark120202 <ark120202@gmail.com>
Co-Authored-By: ark120202 <ark120202@gmail.com>
@TheLartians
Copy link
Contributor Author

I've rolled back to supporting Error.toString() only for the built-in error types as more general support for derived types will be available once #728 gets resolved.

});

test.each([
`"error string"`,
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be a bit cleaner to pass actual values there and convert them with util.valueToString

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, won't atm due to util.valueToString(undefined) = undefined (#733).

@Perryvw Perryvw merged commit bf9f609 into TypeScriptToLua:master Oct 9, 2019
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.

3 participants

X Tutup