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

custom attribute panicked message: called Option::unwrap() on a None #78

Closed
ZimboPro opened this issue Mar 4, 2024 · 3 comments · Fixed by #79
Closed

custom attribute panicked message: called Option::unwrap() on a None #78

ZimboPro opened this issue Mar 4, 2024 · 3 comments · Fixed by #79

Comments

@ZimboPro
Copy link

ZimboPro commented Mar 4, 2024

I am using poem and poem-grants and copied the JWT example, but am getting the following error: custom attribute panicked message: called Option::unwrap() on a None on the #[poem_grants::protect("ADMIN")] macro. The code is:

#[poem_grants::protect("ADMIN")]
#[handler]
fn post_blog(mut pool: Data<&PgPool>, Json(blog): Json<NewBlog>) -> Response {
    let r = match blog.id {
        Some(id) => Blog::update(pool_handler(pool)?.borrow_mut(), id, blog.into()),
        None => Blog::create(pool_handler(pool)?.borrow_mut(), blog.into()),
    };

    match r {
        Ok(blog) => ResponseBuilder::builder()
            .status(StatusCode::OK)
            .body(json!({
                "blog": r.expect("Error")
            })),
        Err(e) => {
            eprintln!("Failed to save or update blog: {}", e);
            ResponseBuilder::builder()
                .status(StatusCode::SERVICE_UNAVAILABLE)
                .body(MessageResponse {
                    message: "Failed to update or insert".to_string(),
                })
        }
    }
}

...

pub fn api() -> JwtMiddlewareImpl<Route> {
    Route::new()
        .at("/getBlogs", get(get_blogs))
        .at("/getBlog", get(get_blog))
        .at("/updateBlog", post(post_blog))
        .at("/getFaq", get(get_faq))
        .at("/getTestimonial", get(get_testimonial))
        .with(JwtMiddleware)
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    tracing_subscriber::fmt::init();
    let connection_pool = db_helpers::establish_connection();
    let app = Route::new()
        .nest("/api/content", api())
        .with(AddData::new(connection_pool))
        .with(Tracing);
    Server::new(TcpListener::bind("0.0.0.0:3000"))
        .run_with_graceful_shutdown(
            app,
            async move {
                let _ = tokio::signal::ctrl_c().await;
            },
            Some(Duration::from_secs(1)),
        )
        .await?;
    Ok(())
}
@DDtKey
Copy link
Owner

DDtKey commented Mar 4, 2024

Hi @ZimboPro!

Your handler isn't async, is it expected? I see there is no any async calls, right
That's just wasn't supported as part of poem-grants

It's because of this line. unwrap can be used only during macro-expansion - i.e compile time only. And looks like we can handle this way better. Poem supports non async handlers

DDtKey added a commit that referenced this issue Mar 4, 2024
DDtKey added a commit that referenced this issue Mar 4, 2024
DDtKey added a commit that referenced this issue Mar 4, 2024
@DDtKey DDtKey closed this as completed in #79 Mar 4, 2024
DDtKey added a commit that referenced this issue Mar 4, 2024
@DDtKey
Copy link
Owner

DDtKey commented Mar 4, 2024

Thanks for the report @ZimboPro!

It was fixed, now non-async handlers are supported as well (poem only).

New version has been released. Please, let me know if you still experience any issues.

@ZimboPro
Copy link
Author

ZimboPro commented Mar 5, 2024

Thanks for the quick reply and solution, that solves the issue

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 a pull request may close this issue.

2 participants