-
-
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
Typed Error Catchers, Fairings #749
Comments
The pain point you are encountering is that request Fairings cannot alter the request handling flow by responding or by returning some kind of error type. This is a known issue that can and should be available in Rocket's API eventually but I don't see an open issue for it. Adding a request guard to every route handler is annoying, but does make it more obvious which routes need authentication and which don't. If you do add a request guard to every route indicating its privilege level, you can require instances of those request guards in database or API code as a compile-time assurance that the necessary security properties are enforced, as well. But if "every" route requires authentication, it can be annoying to do that. You can work around the fairing response situation by altering the request uri in the fairing, thereby changing which route it ends up going to. |
From what I hear from you this issue should be a feature request than a question. I pretty much need everything except login screen to be authenticated. I think this is very common scenario. |
@mmrath I'm with you. We have a feature in mind that would resolve this problem, but alas, it is as of now unimplementable due to lacking features in Rust. |
@SergioBenitez would be interesting to know what features Rust would need so this could be implemented. |
@SergioBenitez Would you be able to give a bit more information on how the solution might look like? |
@NoUsername We'd need @mmrath The gist, from my design doc, is:
|
@SergioBenitez Thanks for the insight. I am not sure I understood fully, but is there no alternate design to implement this with current Rust? |
@mmrath No, I don't believe so. I'm not sure what you mean by "does not appear to be available soon". It seems that all relevant parties are interested in having the feature be implemented. What's missing is a bit of elbow grease to fix a compiler bug that's preventing the implementation. |
The |
it's annoying cause that mean I can't return a descriptive error using guard, that very unfortunate to just return a "303" when I could be very precise about error for example a revoked jwt. |
For anyone having this problem, I'm currently solving this by pushing the error data onto the local_cache of the request and reading it at the catcher. |
It's not perfect, but it works and it seems to be easy to understand what's going on. :) |
hi folks, is this 422 error catching into json response proposed by #1234 (comment) already integrated in any form into rocket API, or any form to catch this error thrown on html by default into json? |
Hey guys, I'm really confused now about how I should provide details about the error(e.g. 422 validation errors). It seems like an essential part of a web server. So any news about it? |
it's a core design problem, that not like someone can fix it with a small commit :p |
If this is still stuck on use std::any::TypeId;
use std::marker::PhantomData;
use std::mem;
pub fn non_static_type_id<T: ?Sized>() -> TypeId {
trait NonStaticAny {
fn get_type_id(&self) -> TypeId where Self: 'static;
}
impl<T: ?Sized> NonStaticAny for PhantomData<T> {
fn get_type_id(&self) -> TypeId where Self: 'static {
TypeId::of::<T>()
}
}
let phantom_data = PhantomData::<T>;
NonStaticAny::get_type_id(unsafe {
mem::transmute::<&dyn NonStaticAny, &(dyn NonStaticAny + 'static)>(&phantom_data)
})
} This produces fn assert_t_is_str<T>() {
assert_eq!(non_static_type_id::<T>(), TypeId::of::<&str>());
}
fn main() {
assert_t_is_str::<&str>();
} |
Can you please provide a snippet of what should be done so newbies like me can get a hint at the implementation until a canonical way is established? |
When I encountered this problem, I was seeking a solution for this project. You can search for |
Please feel free to submit a PR with an implementation! I'd be more than happy to review it. |
I've been working on this recently in a push to get 0.6 out sooner than later. There are ~two difficult problems that we need to solve to make this happen. As far as I can tell, there's no evidence to suggest that a solution to the latter exists. They are:
As a concrete example, consider the #[post("/", data = "<json>")]
fn submit(json: Json<Foo>) { .. }
#[catch(default)]
fn json<'r>(error: json::Error<'r>) -> MyError<'r> { .. } As mentioned before, if the So far, this is well typed and sound, and we can accomplish this today. However, consider the following version of #[catch(default)]
fn json(error: json::Error<'static>) -> MyError<'static> { .. } It would be unsound for Rocket to take the As of now, I do not know how to do this. |
I'm pretty sure this isn't possible in Rust right now. Lifetimes are erased at compile time, before On the other hand, I'm not sure how to go about restricting the input lifetime of a type to a specific (non-static) lifetime. The catcher signature would look something like: |
We could potentially enforce a variant of this via and ouroboros-like implementation, where the error type is |
What I want is to inspect every request and if it is anything but POST to /api/auth/register or /api/auth/login then check for for auth header. If header is not present, then reply 401. I looked at Fairing - but Fairing cant respond to requests. I looked at request guard, it looks like for them I need to add a param to every route handler.
Questions
Any questions must include:
The version of Rocket this question is based on, if any.
master
What steps you've taken to answer the question yourself.
Looked at Fairing and request guard docs
What documentation you believe should include an answer to this question.
not sure
The text was updated successfully, but these errors were encountered: