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: For 'VALIDATION' type errors, status code change does not work (200 is always returned) #639

Closed
nadeemramsing opened this issue May 14, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@nadeemramsing
Copy link

nadeemramsing commented May 14, 2024

What version of Elysia.JS is running?

"version": "1.0.17"

What platform is your computer?

Linux 5.15.0-88-generic x86_64 x86_64

What steps can reproduce the bug?

  • Create a route /files POST
  • Use type: {body: t.Object({file: t.File()})}
  • In onError function, when code === 'Validation', set status to 400.
  • Run app
  • From Postman, do a request on the route with a form-data body without "file"

What is the expected behavior?

Return error and 400 status code.

What do you see instead?

Error is returned but status code remains 200.

Additional information

In fact, logger (HoltLogger) says "unknown status":
image

In my case, setting status code and even return a value (e.g. "TEST") does not work; 200 and Validation Error by type lib is returned. Execution gets there (console.log inserted and called just before the setting of status and return).

@nadeemramsing nadeemramsing added the bug Something isn't working label May 14, 2024
@bogeychan
Copy link
Contributor

You can overrite error.status or return a raw Response object as a workaround:

.onError(({ code, error }) => {
  if (code === "VALIDATION") {
    // @ts-ignore
    // error.status = 400;
    return new Response(error.message, {
      status: 400,
      headers: { "Content-Type": "application/json" },
    });
  }
})

@nadeemramsing
Copy link
Author

Thank you; I'll try that.

@nadeemramsing
Copy link
Author

Working perfectly; thank you.

@bogeychan
Copy link
Contributor

Please leave the issue open as it's a bug and needs to be fixed somehow.

set.status = 400 must either work or not be made accessible.

I gave you a workaround to make it work for you right away.

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