the way I've solved that a while ago ( https://code.google.com/p/formaldehyde/source/browse/trunk/formaldehyde.js )
`(ServerError.prototype = new Error).name = "ServerError";` Today I would do ```javascript function ServerError(message) { this.message = '' + message; } ServerError.prototype = Object.setPrototypeOf({ name: ServerError.name }, Error.prototype); ``` Cheers On Sat, Nov 23, 2013 at 10:41 AM, Domenic Denicola < dome...@domenicdenicola.com> wrote: > +1 to making this work seamlessly, if possible. But I am not sure it is > possible... > > From: es-discuss [mailto:es-discuss-boun...@mozilla.org] On Behalf Of > Kevin Smith > > > I could, of course, do this manually: > > > > class ZenError extends Error { > > constructor(msg) { super(msg); this.name = this.constructor.name; > } > > } > > Actually, this isn't even "correct" by ES spec standards---illustrating > how error-prone this kind of manual setup is. That is, the error types in > ES have `.prototype.name` properties on the constructor, not own `.name` > properties. So the correct manual setup is > > ```js > class ZenError extends Error { > } > ZenError.prototype.name = ZenError.name; > ``` > > The only way I can think of to fix this is to change the ` > Error.prototype.name` property to a getter that returns ` > this.constructor.name`. But now all existing code that does *not* set > `name` correctly has suddenly had its meaning, and observable side effects, > changed. Seems unlikely to be web compatible. (Plus you have to create a > setter and handle the cases when people *do* set it to something.) > > You then start thinking, well, maybe we should make a simple opt-in to the > more correct behavior. But, can you really make it more simple than ` > ZenError.prototype.name = ZenError.name`? I guess > `Error.fixSubclassName(ZenError)` is a bit more DRY, but not by much. > > Would be curious if anyone has any solutions, but I'm not sure there are > any :(. 100% correct Error subclassing might have to remain a mistake-prone > process. > _______________________________________________ > es-discuss mailing list > es-discuss@mozilla.org > https://mail.mozilla.org/listinfo/es-discuss >
_______________________________________________ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss