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

Make the error type configurable #153

Merged
merged 9 commits into from
Jan 14, 2025
Merged

Make the error type configurable #153

merged 9 commits into from
Jan 14, 2025

Conversation

jorendorff
Copy link
Contributor

@jorendorff jorendorff commented Jan 13, 2025

So far, on the server side, we've required users to use a hard-coded error type, TwirpErrorResponse. The rationale was that we need an error type that the twirp machinery can convert into a syntactically valid Twirp response.

However:

  1. Practically, as a user, I just want to use my project's normal error type. TwirpErrorResponse is a pain.
  2. Users may want to set custom headers on the response. TwirpErrorResponse doesn't currently have any way to do that, and adding a mechanism would complicate the API.
  3. Users may want to set response extensions on the responses, e.g. to can pass information to middleware that applies uniformly to all methods.

So this PR makes the error type configurable, requiring the user to implement a new IntoTwirpResponse trait to make sure users don't accidentally try to use error types that don't generate valid Twirp responses.

@Copilot Copilot bot review requested due to automatic review settings January 13, 2025 18:56
@jorendorff jorendorff requested a review from a team as a code owner January 13, 2025 18:56
@jorendorff jorendorff requested a review from tclem January 13, 2025 18:57

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 8 out of 10 changed files in this pull request and generated no comments.

Files not reviewed (2)
  • crates/twirp/src/client.rs: Evaluated as low risk
  • crates/twirp/src/details.rs: Evaluated as low risk

Tip: If you use Visual Studio Code, you can request a review from Copilot before you push from the "Source Control" tab. Learn more

Copy link
Member

@tclem tclem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this idea a lot. I'm a little unsure about the IntoTwirpResponse as it's really not doing what it says (and isn't there an axum trait for IntoResponse already?).

crates/twirp/src/error.rs Outdated Show resolved Hide resolved
It's still possible to return a response with an invalid status code or headers, but less likely to
happen by accident.
@jorendorff jorendorff force-pushed the jorendorff/error-param branch from c4bbfd5 to 319d675 Compare January 13, 2025 20:26
Copy link
Member

@tclem tclem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@jorendorff jorendorff added this pull request to the merge queue Jan 14, 2025
Merged via the queue into main with commit b33e3a5 Jan 14, 2025
2 checks passed
@jorendorff jorendorff deleted the jorendorff/error-param branch January 14, 2025 20:22
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

Successfully merging this pull request may close these issues.

3 participants