Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onError code argument is sometimes a numbery string #1148

Open
ckiee opened this issue Mar 30, 2025 · 2 comments
Open

onError code argument is sometimes a numbery string #1148

ckiee opened this issue Mar 30, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@ckiee
Copy link

ckiee commented Mar 30, 2025

What version of Elysia is running?

1.2.24(@sinclair/[email protected])([email protected])([email protected])

What platform is your computer?

Linux 6.13.5 x86_64 unknown | NixOS 25.05 (Warbler)

What steps can reproduce the bug?

const app = new Elysia({ prefix: "/api" })
    .onError(({ error, code, set, server, request }) => {
//                                               This doesn't work:
        if (code == "INTERNAL_SERVER_ERROR" || code == "UNKNOWN" || typeof code == "number") {
            console.error(
                `Caught while handling request from ${server?.requestIP(request)}:\n`,
                error
            );
            set.status = 500;
            return { message: "Internal API error" };
        }
    })

What is the expected behavior?

code is as its type says, (parameter) code: number | "UNKNOWN" | "VALIDATION" | "NOT_FOUND" | "PARSE" | "INTERNAL_SERVER_ERROR" | "INVALID_COOKIE_SIGNATURE"

What do you see instead?

code can be one of the enums or a stringified number, so I have to attempt to cast it to check: !isNaN(+code))

Additional information

No response

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

nope

@ckiee ckiee added the bug Something isn't working label Mar 30, 2025
@ledihildawan
Copy link

ledihildawan commented Apr 2, 2025

It seems very unlikely that the code is a number. It's better to always keep ElysiaJS updated. If you want to send an internal error like this, it's better to do it this way. Here's the code:

  .onError(({ code }) => {
    if (code === 'INTERNAL_SERVER_ERROR') return 'Internal API error';
  })

@ckiee
Copy link
Author

ckiee commented Apr 3, 2025

It seems very unlikely that the code is a number. It's better to always keep ElysiaJS updated. If you want to send an internal error like this, it's better to do it this way. Here's the code:

The code can indeed be a number as per src/types.ts:

code: number

But the value of any caught error.code is trusted here at runtime, and never checked:

elysia/src/compose.ts

Lines 1918 to 1920 in a35b26d

'}else{' +
`c.code=error.code??error[ERROR_CODE]??"UNKNOWN"}`
else fnLiteral += `c.code=error.code??error[ERROR_CODE]??"UNKNOWN"\n`

Reproducer: https://github.com/ckiee/elysia-issue-1148-reproducer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants