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

set.status isn't working #415

Closed
mvolkmann opened this issue Jan 12, 2024 · 6 comments
Closed

set.status isn't working #415

mvolkmann opened this issue Jan 12, 2024 · 6 comments

Comments

@mvolkmann
Copy link

I have this endpoint definition that sets set.status to 400 and logs that it does so.
But the actual response status I see in Chrome DevTools is always 200.
Am I doing something wrong?

app.post('/account', ({body, set}: any) => {
  const {email, password} = body;
  const goodEmail = validEmail(email);
  const goodPassword = validPassword(password);
  const good = goodEmail && goodPassword;
  set.status = good ? 200 : 400;
  console.log('/account: set.status =', set.status);
  return <span>{good ? 'A new account was created.' : ''}</span>;
});
@eduwr
Copy link

eduwr commented Jan 12, 2024

@mvolkmann can you provide more information to reproduce the issue?

I created a similar code and it's working as expected even with different code numbers

here is my code:

const app = new Elysia()
  .get("/", () => "Hello Elysia")
  .post("/account", ({ body, set }: any) => {
    const { email, password } = body;
    const goodEmail = validEmail(email);
    const goodPassword = validPassword(password);
    const good = goodEmail && goodPassword;
    set.status = good ? 200 : 416;
    console.log("/account: set.status =", set.status);
    return {html: good ? 'A new account was created.' : ''};
  })
  .listen(3000);

and here is my request

fetch('http://localhost:3000/account', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    email: 'some email',
  }),
})
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error('Error:', error));

I noticed you are returning an html, are you using something to parse that html response?

@mvolkmann
Copy link
Author

In my case the request is coming from the browser using htmx. Htmx takes that HTML response and inserts it into the DOM. It doesn't seem though that htmx has any impact on this because I see in the DevTools Network tab that a POST request is sent and the response status is 200 even though I log on the server that it is 400.

@mvolkmann
Copy link
Author

I just tried changing my endpoint to return a string instead of HTML.
That fixes the problem with the response status code.
So something about returning HTML using JSX seems to break the functionality of set.status.

@eduwr
Copy link

eduwr commented Jan 17, 2024

@mvolkmann it looks like a bug on elysia-html package

I found a similar issue on their repository you can check there
elysiajs/elysia-html#56

@einar-hjortdal
Copy link

einar-hjortdal commented Apr 10, 2024

Oh I am experiencing the same issue. Is there a workaround?

    elysiaContext.set.status = 404
    console.log(elysiaContext)
    return
//  set: {
//    headers: {},
//    status: 404,
//  },

I am not returning html however.

@SaltyAom
Copy link
Member

Unable to reproduce in the latest Elysia version, closing as complete.

If the problem still persists, feel free to reopen the issue. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants