-
Notifications
You must be signed in to change notification settings - Fork 32
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
Change exceptions to extend Error. #184
Open
markdoliner-doma
wants to merge
3
commits into
creditkarma:master
Choose a base branch
from
StatesTitle:mdoliner-st/Change_exceptions_to_extend_Error
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Change exceptions to extend Error. #184
markdoliner-doma
wants to merge
3
commits into
creditkarma:master
from
StatesTitle:mdoliner-st/Change_exceptions_to_extend_Error
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This changes exception classes generated for `--target thrift-server` so that they extend `ErrorThriftLike` instead of `ThriftLike`. There's a corresponding `thrift-server` change to add the `ErrorThriftLike` class. As mentioned in [the GitHub issue](creditkarma#178), this is useful because it causes Thrift exceptions to have stack traces and because some frameworks expect exceptions to derive from Error. The GitHub issue mentions graphql-js. Jest's `toThrow()` function would also benefit from this. Here's an example: ``` await expect(thriftClient.someFunction()).rejects.toThrow() ``` `toThrow` doesn't identify Thrift exceptions as exceptions because they don't derive from Error and so it will return false in such cases. Part of the blame could fall on Jest, because perhaps `toThrow` should return true for any rejected promise regardless of the type of object being throw, but I think some of the blame still falls on Thrift. A downside of extending Error is that there's a naming collision when developers define fields named `name`, `message`, `stack`, etc in their Thrift exceptions (because these are defined by the super class). I think this is an acceptable tradeoff. FYI `tsc` complains if someone defines a field that differs from the super class. For example, if someone has a Thrift exception with `1: required i32 message` (because message must be a string) or `1: optional string message` (because message is required). I tried to avoid the naming collisions by manually adding Error to the exception class's prototype chain. It might avoid the tsc compile errors when there's a naming collision but it doesn't really eliminate the problem of same-named fields being used for two different things. And so I think manually changing the prototype chain is a bad solution. Remaining work: - Fix tests. - Update README.md to mention that end-users need to require the new version of thrift-server which includes `ErrorThriftLike`. - Update README.me to mention the naming collision limitation. - `tsc` complains for naming collisions, and that's fine. Maybe the Thrift compiler should check for and report on collisions? Would probably be a friendly user experience. One challenge is [different JS implementations define different properties on their Error class](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#Instance_properties). - I didn't change the code generated for `--target apache`. It looks like those classes also don't derive from Error. They probably should.
I can clean this up a little (fix tests, update documentation as needed, etc), but wanted to get feedback from Credit Karma folks to make sure I'm heading in the right direction before spending more time on it. |
9 tasks
markdoliner-doma
added a commit
to StatesTitle/thrift-typescript
that referenced
this pull request
Sep 1, 2020
…to_extend_Error I'm merging this change into the all_states_title_changes branch. For the full PR description see [the PR for merging this change into Credit Karma's master branch](creditkarma#184).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This changes exception classes generated for
--target thrift-server
so that they extendErrorThriftLike
instead ofThriftLike
. There's a correspondingthrift-server
change to add theErrorThriftLike
class.As mentioned in the GitHub issue, this is useful because it causes Thrift exceptions to have stack traces and because some frameworks expect exceptions to derive from Error. The GitHub issue mentions graphql-js. Jest's
toThrow()
function would also benefit from this. Here's an example:toThrow
doesn't identify Thrift exceptions as exceptions because they don't derive from Error and so it will return false in such cases. Part of the blame could fall on Jest, because perhapstoThrow
should return true for any rejected promise regardless of the type of object being throw, but I think some of the blame still falls on Thrift.A downside of extending Error is that there's a naming collision when developers define fields named
name
,message
,stack
, etc in their Thrift exceptions (because these are defined by the super class). I think this is an acceptable tradeoff. FYItsc
complains if someone defines a field that differs from the super class. For example, if someone has a Thrift exception with1: required i32 message
(because message must be a string) or1: optional string message
(because message is required).I tried to avoid the naming collisions by manually adding Error to the exception class's prototype chain. It might avoid the tsc compile errors when there's a naming collision but it doesn't really eliminate the problem of same-named properties being used for two different things. And so I think manually changing the prototype chain is a bad solution.
The code generator could possibly try to detect naming collisions between fields in user-defined exceptions and Error, but it doesn't seem super important to me. One difficulty is that different JavaScript implementations set different properties on Error. It would be hard for the code generator to warn about all of them. But
message
andname
are obvious things to warn about.I didn't change the code generated for
--target apache
. Those classes also don't derive from Error. As mentioned in #178 the Apache thrift generator has exception types inheriting from thrift.TException. I think it would be great for our code generator to do the same when using--target apache
, but it seemed like a different enough change that it didn't make sense to do it as part of this commit.Remaining work:
ErrorThriftLike
.