Effect CommunityEC
Effect Community3y ago
2 replies
attila

Unauthenticated Request to Non-Existent Route Fails

Let's say you have a router like so:
const router = pipe(
  Http.router.empty,
  Http.router.get("/", Http.response.text("Hello World")),
  Effect.catchTag("RouteNotFound", () =>
    Http.response.empty({ status: 404 });
  )
);

and assume you have an authentication middleware, then given:
import { runMain } from "@effect/platform-node/Runtime";

const HttpLive = pipe(
  router,
  authnMiddleware,
  Http.server.serve(Http.middleware.logger),
  Layer.scopedDiscard,
  Layer.use(ServerLive),
  Layer.use(NodeContext.layer),
  Layer.launch,
  Effect.tapErrorCause(Effect.logError),
  runMain
);

If you make an unauthenticated request to a non-existent route, it fails with a 401 instead of a 404. Is there a way to shuffle the router/middleware around so that RouteNotFound has precedence over the middleware execution?
Was this page helpful?