I'd like to create my own error class in TypeScript, extending core Error
to provide better error handling and customized reporting. For example, I want to create an HttpRequestError
class with url, response and body passed into its constructor, which reponds with Http request to http://example.com failed with status code 500 and message: Something went wrong and proper stack trace.
How to extend core Error class in TypeScript? I've already found post in SO: How do I extend a host object (e.g. Error) in TypeScript but this solution doesn't work for me. I use TypeScript 1.5.3
Any ideas?
TypeScript 2.1 had a breaking changes regarding Extending built-ins like Error.
From the TypeScript breaking changes documentation
Then you can use:
I am using TypeScript 1.8 and this is how I use custom error classes:
UnexpectedInput.ts
MyApp.ts
For TypeScript versions older than 1.8, you need to declare
Error
:Until 1.6 rolls around, I've just been making my own extendable classes.