Effect-TS / effect

An ecosystem of tools to build robust applications in TypeScript
https://effect.website
MIT License
7.74k stars 246 forks source link

`@effect/platform` middleware only allows one failure schema #3870

Closed skoshx closed 4 weeks ago

skoshx commented 4 weeks ago

What version of Effect is running?

"effect": "^3.10.6",

What steps can reproduce the bug?

Following docs, we can't create multiple failure schemas for middleware, although that should be the case. For instance, the authorization middleware should be able to return either Unauthorized OR InternalServerError, for instance if something goes wrong with the DB query (db is down etc)

export class AuthorizationMiddleware extends HttpApiMiddleware.Tag<AuthorizationMiddleware>()(
  "Authorization",
  {
    failure: Unauthorized,
    provides: ApiKeyService,
    security: { bearer: HttpApiSecurity.bearer },
  }
) {}

What is the expected behavior?

It should be possible to define multiple failure schemas, since realistically middleware can have internal server errors, and the intended errors like Unauthorized etc.

What do you see instead?

I can only provide one failure schema, which makes it so that if my DB is down, I still have to return Unauthorized with 401 status code which isn't correct. This makes it confusing to the user what went wrong. Also, we can't monitor for Internal Server Errors, since the middleware can return only Unauthorized, even if something went horribly wrong.

Additional information

No response

KhraksMamtsov commented 4 weeks ago

I'm not sure, but you've tried Schema.Union?

skoshx commented 4 weeks ago

Wow I feel dumb 😓 Yeah thanks it works with Schema.Union... I had tried with .extend but I guess Union is the correct one.