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

Proper way to access response code in onResponse hook? #88

Closed
jayden-chan opened this issue Aug 15, 2023 · 3 comments
Closed

Proper way to access response code in onResponse hook? #88

jayden-chan opened this issue Aug 15, 2023 · 3 comments

Comments

@jayden-chan
Copy link

I want to use the onResponse hook to print a log message every time a response is generated. Right now I am reading the value of set.status to determine the HTTP status code of the response. However in cases where Elysia responds early due to an error, the value of set.status is incorrect. For example if body validation fails, the server responds to the client with code 400 but the value of set.status is 500.

Is this the wrong way of determining info about the response? I did not see any other way in the docs or function signature. From what I can tell, the response itself isn't actually accessible in the onResponse hook.

@SaltyAom
Copy link
Member

This should be fixed with Elysia 0.6.11

Bug fix:

  • #86 Group prefix repeating on inline function callback
  • #88, onResponse hooks validation return non 400

Feels free to reopen the issue if the problem still persists.

@jayden-chan
Copy link
Author

jayden-chan commented Sep 9, 2023

@SaltyAom The problem persists. Failed hooks validation is not the only case where set.status differs from the actual returned response.

Here is a minimal reproduction example:

import { Elysia } from "elysia";

new Elysia()
  .onResponse(({ set }) => {
    console.log(`Response code: ${set.status}`);
  })
  .get("/", () => {
    return new Response("Not Found", {
      status: 404,
    });
  })
  .listen(3000);

Performing a curl request returns 404 as expected:

$ curl http://localhost:3000/ --verbose
* processing: http://localhost:3000/
*   Trying [::1]:3000...
* Connected to localhost (::1) port 3000
> GET / HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/8.2.1
> Accept: */*
>
< HTTP/1.1 404 Not Found
< content-type: text/plain;charset=utf-8
< Date: Sat, 09 Sep 2023 02:30:28 GMT
< Content-Length: 9
<
* Connection #0 to host localhost left intact
Not Found%

but the code prints:

Response code: 200

The issue is that set isn't always used to actually set the response code. I think for the onResponse API to make sense, the callback needs to have access to the actual Response object that was sent to the client. I'm not sure if this is possible but I think it makes the most sense.

(tested on Elysia 0.6.19)

@otherguy
Copy link

@SaltyAom can confirm on 0.6.24.

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

3 participants