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

The statusText property of the response object in the endpoint should be preserved. #9401

Closed
1 task
izure1 opened this issue Dec 11, 2023 · 3 comments · Fixed by #12105
Closed
1 task

The statusText property of the response object in the endpoint should be preserved. #9401

izure1 opened this issue Dec 11, 2023 · 3 comments · Fixed by #12105
Labels
- P3: minor bug An edge case that only affects very specific usage (priority) feat: error pages Related to 404 and 500 handling (scope) requires refactor Bug, may take longer as fixing either requires refactors, breaking changes, or considering tradeoffs

Comments

@izure1
Copy link

izure1 commented Dec 11, 2023

Astro Info

Astro                    v4.0.3
Node                     v18.18.2
System                   Windows (x64)
Package Manager          npm
Output                   hybrid
Adapter                  @astrojs/node
Integrations             @astrojs/react
                         @astrojs/tailwind

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

The statusText value of the new Response returned from the endpoint is not preserved. Here is an example code:

// /api/custom.ts
const { message } = error
return new Response(null, {
  status: 500,
  statusText: 'custom error message'
})

Afterwards, when checking the endpoint value with fetch in the browser, the statusText property of the response is changed to Internal Server Error.

I'm not sure if this is the intended behavior.

What's the expected result?

The specified statusText value provided by the user should be displayed.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-1dagae?file=src%2Fpages%2Findex.astro

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Dec 11, 2023
@lilnasy lilnasy added - P3: minor bug An edge case that only affects very specific usage (priority) feat: error pages Related to 404 and 500 handling (scope) requires refactor Bug, may take longer as fixing either requires refactors, breaking changes, or considering tradeoffs labels Dec 11, 2023
@github-actions github-actions bot removed the needs triage Issue needs to be triaged label Dec 11, 2023
@lilnasy
Copy link
Contributor

lilnasy commented Dec 11, 2023

Implicit rerouting (basically returning any response with a status code of 404 or 500 implicitly reroutes) has several caveats. This is one of them. If you can't avoid using it, I'd recommend using locals to pass data to the 500 route instead.

Docs: Storing data in context.locals

@izure1
Copy link
Author

izure1 commented Dec 11, 2023

Thank you for your feedback :)

I am currently writing code using the fetch API in the browser to asynchronously receive data from an endpoint.
If the response.ok value is false, I intend to output the response.statusText value to the user as an error message.
Therefore, I expect that this function can be easily implemented as long as the statusText attribute is maintained.

@lilnasy
Copy link
Contributor

lilnasy commented Dec 14, 2023

Thanks for explaining your use case, it really helps! You might run into issues after deploying to production because statusText does not exist in http 2 or 3. If your platform hosts your app on these newer protocols, statusText will be removed. For your use case a response header might be better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P3: minor bug An edge case that only affects very specific usage (priority) feat: error pages Related to 404 and 500 handling (scope) requires refactor Bug, may take longer as fixing either requires refactors, breaking changes, or considering tradeoffs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants