-
Notifications
You must be signed in to change notification settings - Fork 169
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 GrpcErrorsAsFailures
more flexible
#189
Conversation
@tamuhey you filed the original issue about this. What do you think about this API? |
@hawkw you were quite involved in the original design of tower-http's response classification stuff. Wanna give this a look? |
One option so as to not expose the bitmask is just to keep that internal. Allow people to call I'm a fan of this because I also feel like bit flags aren't the most intuitive thing to use for many programmers. At least I've been confused a few times whether I want to use |
Yeah thats true 🤔 Although that would require an enum with each variants for each, although that wouldn't be too bad. I'm personally fine with bitflags but I've also used it a bunch in axum so maybe I'm just used to it. |
No, you could use bit flags internally, just let the user pass in a Like for a user: layer
.with_success(Code::OK)
.with_success(Code::CANCELED) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! I personally think the exposed bitflags actually clearer but that's just me. Actually now that I think about it &
would just be a fotgun if the bitflags are exposed since its impossible for a status to match two different codes.
Yep that also what I meant. I'll play around with the API a bit today. |
I've changed it and the bitmask is no longer public. API is now: use tower_http::classify::{GrpcErrorsAsFailures, GrpcCode};
let classifier = GrpcErrorsAsFailures::new()
.with_success(GrpcCode::InvalidArgument)
.with_success(GrpcCode::NotFound); |
@davidpdrsn Looks perfect for me, thanks! |
Previously
GrpcErrorsAsFailures
would only considerOk
as a success. However for servers it might be useful to also considerNot Found
orInvalid Arguments
as success (or at least not consider them failures) since they're likely caused by the client.This PR adds
GrcpErrorsAsFailures::success_codes
which takes a bitmask of the codes that are considered successes.Example usage:
I went with the name
GrpcCodeBitmask
because I think it communicates quite somewhat clearly what it does. At least to people who know what a bitmask is. At first I considered just calling itGrpcCode
but that might be confusing since its used to match multiple codes and doesn't just represent a single code.Maybe the name
GrpcErrorsAsFailures
isn't very accurate anymore 🤔 Maybe justGrpcClassifier
would be better. Although its more broad.It does add a new public dependency on
bitflags
but I'm okay with that. The crate is maintained by people from the Rust team, doesn't have dependencies, and has been 1.0 for over 4 years. axum also has a public dependency on it.Fixes #126