Skip to content

NotFoundError doesn't call onAfterResponse hook with aot: true #713

@bogeychan

Description

@bogeychan

What version of Elysia.JS is running?

1.1.3

What platform is your computer?

WSL Ubuntu

What steps can reproduce the bug?

// test.test.ts
import {
  t,
  Elysia,
  ParseError,
  NotFoundError,
  ValidationError,
  InternalServerError,
} from "elysia";
import { describe, it, expect, beforeEach } from "bun:test";

let isOnResponseCalled: boolean;

class CustomError extends Error {}

const app = new Elysia()
  .onAfterResponse(() => {
    isOnResponseCalled = true;
  })
  .post("/", () => "yay", {
    body: t.Object({
      test: t.String(),
    }),
  })
  .get("/customError", () => {
    throw new CustomError("whelp");
  })
  .get("/internalError", () => {
    throw new InternalServerError("whelp");
  });

beforeEach(() => {
  isOnResponseCalled = false;
});

export const newReq = (params?: {
  path?: string;
  headers?: Record<string, string>;
  method?: string;
  body?: string;
}) => new Request(`http://localhost${params?.path ?? "/"}`, params);

describe("Error", () => {
  it.each([
    ["NotFoundError", newReq({ path: "/notFound" })],
    [
      "ParseError",
      newReq({
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: "",
      }),
    ],
    [
      "ValidationError",
      newReq({
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({}),
      }),
    ],
    ["CustomError", newReq({ path: "/customError" })],
    ["InternalServerError", newReq({ path: "/internalError" })],
  ])("%s should call onResponse", async (_name, request) => {
    expect(isOnResponseCalled).toBeFalse();
    await app.handle(request);
    expect(isOnResponseCalled).toBeTrue();
  });
});
bun test

What is the expected behavior?

all tests pass:

✓ Error > NotFoundError should call onResponse
✓ Error > ParseError should call onResponse
✓ Error > ValidationError should call onResponse
✓ Error > CustomError should call onResponse
✓ Error > InternalServerError should call onResponse

What do you see instead?

NotFoundError test fails:

✗ Error > NotFoundError should call onResponse
✓ Error > ParseError should call onResponse
✓ Error > ValidationError should call onResponse
✓ Error > CustomError should call onResponse
✓ Error > InternalServerError should call onResponse

Additional information

works as expected with aot: false

Metadata

Metadata

Labels

bugSomething isn't workingreviewWaiting for issue reporter approval

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions