Closed ryanleecode closed 4 years ago
The default ono()
function does that. If the first argument is an instance of Error
(or any inherited type), then it will create an instance of the same class. You can also use this with your own custom error classes as well
Can you look at this codesandbox?
https://codesandbox.io/s/peaceful-tesla-fxy31?fontsize=14&hidenavigation=1&theme=dark
@ryanleecode Thanks for sending a code sample. You were just missing one line, which registers your custom error type with Ono:
ono.myError = new Ono(MyError);
Here's a complete working demo
Is there a way to do this elegantly in typescript without having to extend the .d.ts
file?
Not that I'm aware of. But perhaps someone who's more of a TypeScript pro will know a way
Is there a way to wrap an error so that the instance/prototype of the original error is not lost? This makes it easy to catch up the stack using
instanceof
calls or decorators that rely on using the class constructor.