socket-controllers icon indicating copy to clipboard operation
socket-controllers copied to clipboard

feature: emit different error type for different errors

Open NoNameProvided opened this issue 4 years ago • 0 comments

Description

The error handling usually more complex than just replying with an error that happened to every invalid scenario. The @EmitOnFail decorator should support defining different messages to emit for different error types.

Proposed solution

The @EmitOnFail decorator should be extended to accept a secondary parameter returning the type of the expected error constructor which will be checked via the instanceof operator.

@EmitOnFail(messageName: string, errorType => MyErrorClass)

Multiple operators should be stackable:

@SocketController()
export class MyController {

  @OnMessage('example-scope:operation')
  @EmitOnFail('BadRequestError', errorType => BadRequestError) 
  @EmitOnFail('SomeCustomError', errorType => SomeCustomError)
  @EmitOnFail('UnhandledError') // will return this if none of the known errors had matched
  doStuff() {
    // do stuff here
  }
}


NoNameProvided avatar Aug 22 '20 21:08 NoNameProvided