-
Notifications
You must be signed in to change notification settings - Fork 3k
refactor(Error): Rewrite custom derived errors with Object.create() #2217
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
refactor(Error): Rewrite custom derived errors with Object.create() #2217
Conversation
@kwonoj how about this? |
579cc46
to
49127be
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this seems a working solution. I'll leave opened to get second eye for review.
Personally, I'd like to land #2202 (and following PR) prior to any-targeting-master changes to enable type inference-validation on each changes. Though this looks solid (and local verification works), always better to have test supports. |
2facaf9
to
b35e05a
Compare
I added some test cases for this change. |
Interesting. I played around with this same approach for a few minutes and TS 2.1 didn't like it when I tried. Heh. Looks good to me, though. |
For the record, you can create an intermediate class that "does the right thing" by not explicitly returning from the constructor itself. You could write it as an ES5-style class (much the same way the old code would be compiled), and then derive your classes from that class. That way, much less of the code actually has to be rewritten. var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
export interface ShimErrorStatic {
new (message?: string): ShimError;
}
export interface ShimError extends Error {}
export const ShimError: ShimErrorStatic = (function (_super) {
__extends(ShimError, _super);
function ShimError(message) {
_super.apply(this, arguments);
// ensure that 'message' gets set correctly - wasn't case with old implementation.
this.message = message;
}
return ShimError;
}(Error)) as any; The only downside is that if you're compiling to ES2015, this winds up being an extra layer that you may find undesirable. |
@DanielRosenwasser would you able to share how much will it be? I expect ES15 package will be published eventually, so if those are unacceptable amount probably would better to go with existing changes in this PR. |
Well you'd have to add the |
Actually, I wonder if you could get away with this, which doesn't need the export interface RxErrorStatic {
new (message?: string): RxError;
}
export interface RxError extends Error {}
export const RxError: RxErrorStatic = function RxError(this: Error, message: string) {
const err = Error.call(this, message);
this.message = message;
this.stack = err.stack;
return err;
} as any;
RxError.prototype = Object.create(Error.prototype, {
constructor: {
value: RxError,
enumerable: false,
writable: true,
configurable: true,
},
}); |
What should I do about this? I feel the current patch is not different from @DanielRosenwasser proposed one. |
We solved this issue a while back, but I think you were involved with that. Either way, thanks for the contribution, @saneyuki. Closing this one for now. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Description:
Object.create()
's classical inheritance by hand.Related issue (if exists):
#2178