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

docs: invalid status code in middleware #4212

Closed
1 task done
alishi973 opened this issue Apr 18, 2023 · 4 comments
Closed
1 task done

docs: invalid status code in middleware #4212

alishi973 opened this issue Apr 18, 2023 · 4 comments
Labels
👋 free for all Feel free to pick this up and fix it if you want

Comments

@alishi973
Copy link

Area of Improvement

The TRPC documentation suggests that middleware can throw a TRPCError instance with a specific status code set to return an appropriate error response to the client.

However, there is a bug (or maybe using invalid tools) where all errors thrown from middleware are caught by TRPC and returned as a generic "Internal Server Error" response with a 500 status code, regardless of the specific status code set in the TRPCError instance.

IMO, This behavior is incorrect and may be confusing for developers and end-users.

Could you provide guidance on how to work around this issue until it is resolved, or suggest an alternative solution that would allow middleware to return appropriate error responses with specific status codes?

Link to related docs

https://trpc.io/docs/server/middlewares

Additional information

No response

👨‍👧‍👦 Contributing

  • 🙋‍♂️ Yes, I'd be down to file a PR implementing the suggested changes!
@alishi973 alishi973 added the 📚 documentation / examples Improvements or additions to documentation label Apr 18, 2023
@Nick-Lucas
Copy link
Contributor

This is an interesting point. Feel free to open a PR proposing a fix as it should be quite a simple piece, we have #3496 (drop a note in there too) coming up and it seems a very sensible breaking change to me

for now you can work around the behaviour in errorFormatter

@Nick-Lucas Nick-Lucas added 🐛 bug: unconfirmed 👋 free for all Feel free to pick this up and fix it if you want and removed 📚 documentation / examples Improvements or additions to documentation labels Apr 18, 2023
@KATT
Copy link
Member

KATT commented Apr 19, 2023

  • If you throw a TRPCError in a middleware, it should go straight through without us touching it.
  • If you throw anything else, it'll be an INTERNAL_SERVER_ERROR with the original error as error.cause in the error formatter (where you can remap it to whatever you want)

If the above doesn't align what we do in practice or in the docs, we should fix it ☺

@Nick-Lucas
Copy link
Contributor

So it sounds like we may need a reproduction @alishi973, are you able to share any extra details or code?

@alishi973
Copy link
Author

Yeah, that's right, I've used TRPCError that was imported from the trpc library which is not the correct tool, and must use @trpc/server instead.

is it possible to rename or change that to avoid any confusion?

BTW, Thanks for your time ✌️

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
👋 free for all Feel free to pick this up and fix it if you want
Projects
None yet
Development

No branches or pull requests

3 participants