I have gotten my code to work, but with less than optimal error handling.
Here is my Template struct:
#[derive(Template)]
#[template(path = "bookmarks.html")]
pub struct ShowBookmark<'a> {
pub bookmarks: &'a Vec<Bookmark>
}
...
#[derive(Debug, Deserialize, Serialize, FromRow)]
pub struct Bookmark {
#[sqlx(rename = "title")]
pub description: String,
pub href: String,
pub tags: String,
pub extended: String,
#[sqlx(rename = "public")]
pub shared: String,
pub toread: String,
#[sqlx(rename = "created_at")]
pub time: chrono::DateTime<chrono::Utc>,
}
This handler works:
pub async fn read(
extract::State(pool): extract::State<PgPool>,
) -> Html<String> {
// Beware of the size of the result set! Consider using `LIMIT`
let res = sqlx::query_as::<_, Bookmark>("SELECT * FROM bookmarks")
.fetch_all(&pool)
.await;
let html = ShowBookmark { bookmarks: &res.unwrap() };
Html(html.render().unwrap())
}
As a newbie, I can see that there are, I think, 3 places my handler can panic:
- At
res
(database error) - When I
&res.unwrap()
to provide value for html.bookmarks - At
html.render().unwrap()
I have tried writing the function -> Result<Html<String>, askama_axum::Error>
and returning OK(Html(html.render()?)
), but compiler tells me that Error does not implement IntoResponse
trait. If I try
extract::reject::UnknownBodyError
instead of askama_axum::Error
I get a compiler error that tells me:
the trait
std::convert::From<askama_axum::Error>
is not implemented foraxum::extract::rejection::UnknownBodyError
.
There is other information in the diagnostic message but I do not understand most of it.
It seems to me that using askama_axum::Error
would be the most straightforward, but I need to understand how to implement IntoResponse
trait for this error type.