elysiajs / elysia

Ergonomic Framework for Humans
https://elysiajs.com
MIT License
10.11k stars 214 forks source link

onError: For 'VALIDATION' type errors, status code change does not work (200 is always returned) #639

Closed nadeemramsing closed 4 months ago

nadeemramsing commented 4 months ago

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?

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).

bogeychan commented 4 months ago

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 commented 4 months ago

Thank you; I'll try that.

nadeemramsing commented 4 months ago

Working perfectly; thank you.

bogeychan commented 4 months ago

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.