socket-controllers
socket-controllers copied to clipboard
EmitOnFail Decorator for a specific error type
trafficstars
I forked the repo the other day and have been working on creating an additional decorator that allows you to emit a socket message when a specific type of Error has been thrown.
@EmitOnError("car-not-found", () => NotFoundError)
searchCar(@ConnectedSocket socket: any, @MessageBody() carSearch: any) {
// if this code throws a NotFoundError, "car-not-found" is emitted from the socket
}
A few notes about implementing this:
- The syntax
() => NotFoundErroris a little strange, but it's how you can pass a type as a parameter. TypeORM uses the same syntax in their decorators. - User's would not be able to inherit their Errors from the Error object. This is due to a breaking change in TypeScript 2.1. They only way they would be able to inherit from Error would be to add the following code in the constructor after the super() call.
Object.setPrototypeOf(this, MyError.prototype);
I've considered that these custom exceptions would be required to inherit from an abstract base exception class found in this library in order to alleviate any issues with Error inheritance, since it's valid to throw an object that is not an error. This approach would likely tie application logic to this library which could be less than desirable.
Would you be open to reviewing a pull request to incorporate this feature? I need some more time to finish the code, write tests, and add samples.