Skip to content

error instanceof InvalidFileType is false when error.code is INVALID_FILE_TYPE inside of global onError handler #1500

@reaganmcf

Description

@reaganmcf

What version of Elysia is running?

elysia@1.4.11

What platform is your computer?

Linux 6.6.87.2-microsoft-standard-WSL2 x86_64 x86_64

What environment are you using

bun 1.3.0

Are you using dynamic mode?

No

What steps can reproduce the bug?

When creating a global onError handler, the error parameter is not behaving as expected particularly when the error variant is InvalidFileType.

import { Elysia, fileType } from "elysia";
import { z } from "zod";
import { InvalidFileType } from "elysia/error";
import { describe, it, expect } from "bun:test";

describe("example test", () => {
  it("test", async () => {
    const app = await new Elysia()
      .onError({ as: "global" }, ({ error, set }) => {
        if (error instanceof InvalidFileType) {
          set.status = 400;
          return {
            message: "My custom invalid file type error",
          };
        }
      })
      .post(
        "/upload",
        async ({ body }) => {
          await fileType(body.file, "application/json");
          return { success: true };
        },
        {
          body: z.object({
            file: z.file(),
          }),
        },
      );

    const jpegFile = new File(["dummy content"], "photo.jpg", {
      type: "image/jpeg",
    });

    const formData = new FormData();
    formData.append("file", jpegFile);
    const res = await app.handle(
      new Request("http://localhost/upload", {
        method: "POST",
        body: formData,
      }),
    );

    const text = await res.text();
    expect(res.status).toBe(400);
    expect(text).toBe('{"message":"My custom invalid file type error"}');
  });
});

If you inspect the value of error.constructor.name you will get InvalidFileType, and the code is INVALID_FILE_TYPE - so it is definitely a InvalidFileType instance but for some reason, unlike the other errors in elysia, instanceof does not work.

What is the expected behavior?

error instanceof InvalidFileType should be true, not false, in a global error handler.

What do you see instead?

error instanceof InvalidFileType returns false

Additional information

The other errors like ValidationError, NotFoundError, ParseError, etc. all work fine with this behavior.

Have you try removing the node_modules and bun.lockb and try again yet?

yes

Metadata

Metadata

Assignees

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