-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
RequestGuard return Custom Response on Failure #1232
Comments
I was just coming to raise exactly this. One option is to write a Catcher for your status code, but as best I can tell you've then got to work out the problem again to return the correct payload. |
Indeed, my current approach is to return a custom status code (e.g. 10000, 100001 and 10002) from the RequestGuard. In the catcher (create manually since the macro has a limit at 509) I change the status back to 401 and a specific error is returned (e.g. 100001 maps to {"code": "311", "message": "Too many authorization headers"}). |
AFAICT this question is the same as #749 but for request guards. There are a few different things at play here:
Much of this applies to fairings and data guards as well.
You can currently do something like this to reduce the boilerplate, but I agree it is not ideal: #[get("/whoami")]
fn whoami(db: DbConn, auth_token: Result<AuthToken, JSONError>) -> Result<JsonValue, JSONError> {
let user = auth_token?.username;
json!({ "name": db.get_user(user).full_name })
} In particular, the |
Hm, actix-web does it quite well with the FromRequest trait for extractors. There I can return my ApiError with any data I like and it will be used as a Response. If I need the error, I can also wrap it in an Option and get access to the error type (are there other use-cases then these in rocket?). Maybe middleware would be something that wouldn't hurt other use-cases. |
#749 is closed, as well as this one, as well as another one. Does this mean that this is complete? Is it possible in a guard now, or would I need to use a fairing? |
I try to achieve the following:
I've created a RequestGuard as per https://api.rocket.rs/v0.4/rocket/request/trait.FromRequest.html#example-1
I've the requirement in rocket 0.4.2+ to return a JSON when the Authorization in that RequestGuard fails, with a custom code and message.
For Example { "code": "9001", message: "No header present" } or { "code": "9007", message: "Multiple header present"}
The only thing I can return is a Failure with a Status and my custom Error.
One solution as per documentation is to use Result in every route and match on that. But since the Authorization Request Guard is basically used in every route, that's tons of repetition for every route.
Is there anything I missed? (I'm fine with adding that RequestGuard to every route, but I'd like it to automatically generate my JSONError on failure without me needing to wrap it every time in a Result and match on it).
I'm not 100% sure if #749 is the same or similar. I'd still like to use RequestGuard
The text was updated successfully, but these errors were encountered: