Effect CommunityEC
Effect Community2y ago
4 replies
David Novák

Best Practice for Extending Errors with HTTP Status in TypeScript

What is the best practice for extending errors?

Let's say we want to have a function that correctly sends error response in http server. Each "sendable" error should have http status defined.

I tried something like this:
export class ResponseErrorBase extends S.TaggedError<ResponseErrorBase>()(
  'ResponseErrorBase',
  {
    message: S.String,
    status: S.Number,
  }
) {}
export class BadRequestError extends ResponseErrorBase.extend<BadRequestError>(
  'BadRequestError'
)({}) {
  constructor(error?: ParseError) {
    super({status: 400, message: error.message})
  }
}


But the problem is, that the tag is not overridden when extending TaggedError.

That means this:
new BadRequestError()._tag === 'ResponseErrorBase'
Was this page helpful?