| 1 |
/** |
| 2 |
* Used to create Error subclasses until the community moves away from ES5. |
| 3 |
* |
| 4 |
* This is because compiling from TypeScript down to ES5 has issues with subclassing Errors |
| 5 |
* as well as other built-in types: https://github.com/Microsoft/TypeScript/issues/12123 |
| 6 |
* |
| 7 |
* @param createImpl A factory function to create the actual constructor implementation. The returned |
| 8 |
* function should be a named function that calls `_super` internally. |
| 9 |
*/ |
| 10 |
export function createErrorClass<T>(createImpl: (_super: any) => any): T { |
| 11 |
const _super = (instance: any) => { |
| 12 |
Error.call(instance); |
| 13 |
instance.stack = new Error().stack; |
| 14 |
}; |
| 15 |
|
| 16 |
const ctorFunc = createImpl(_super); |
| 17 |
ctorFunc.prototype = Object.create(Error.prototype); |
| 18 |
ctorFunc.prototype.constructor = ctorFunc; |
| 19 |
return ctorFunc; |
| 20 |
} |