opds error handling

This commit is contained in:
Sebastian Hugentobler 2024-05-09 08:39:46 +02:00
parent faea154ff5
commit 93aeb80c56
Signed by: shu
GPG key ID: BB32CF3CA052C2F0
12 changed files with 94 additions and 47 deletions

View file

@ -9,7 +9,7 @@ use poem::{
use tera::Context;
use crate::{
app_state::AppState, data::book::Book, handlers::error::SqliteError, templates::TEMPLATES,
app_state::AppState, data::book::Book, handlers::error::HandlerError, templates::TEMPLATES,
};
#[handler]
@ -17,11 +17,14 @@ pub async fn handler(
id: Path<u64>,
state: Data<&Arc<AppState>>,
) -> Result<Html<String>, poem::Error> {
let author = state.calibre.scalar_author(*id).map_err(SqliteError)?;
let author = state
.calibre
.scalar_author(*id)
.map_err(HandlerError::DataError)?;
let books = state
.calibre
.author_books(*id, u32::MAX.into(), None, SortOrder::ASC)
.map_err(SqliteError)?;
.map_err(HandlerError::DataError)?;
let books = books
.iter()
.filter_map(|x| Book::full_book(x, &state))

View file

@ -7,14 +7,17 @@ use poem::{
IntoResponse,
};
use crate::{app_state::AppState, handlers::error::SqliteError};
use crate::{app_state::AppState, handlers::error::HandlerError};
#[handler]
pub async fn handler(
id: Path<u64>,
state: Data<&Arc<AppState>>,
) -> Result<WithContentType<Vec<u8>>, poem::Error> {
let book = state.calibre.scalar_book(*id).map_err(SqliteError)?;
let book = state
.calibre
.scalar_book(*id)
.map_err(HandlerError::DataError)?;
let cover_path = state.config.library_path.join(book.path).join("cover.jpg");
let mut cover = File::open(cover_path).map_err(|_| NotFoundError)?;

View file

@ -7,14 +7,17 @@ use poem::{
IntoResponse,
};
use crate::{app_state::AppState, data::book::Book, handlers::error::SqliteError};
use crate::{app_state::AppState, data::book::Book, handlers::error::HandlerError};
#[handler]
pub async fn handler(
Path((id, format)): Path<(u64, String)>,
state: Data<&Arc<AppState>>,
) -> Result<WithHeader<WithContentType<Vec<u8>>>, poem::Error> {
let book = state.calibre.scalar_book(id).map_err(SqliteError)?;
let book = state
.calibre
.scalar_book(id)
.map_err(HandlerError::DataError)?;
let book = Book::full_book(&book, &state).ok_or(NotFoundError)?;
let format: &str = format.as_str();
let file_name = book.formats.get(format).ok_or(NotFoundError)?;

View file

@ -1,32 +1,40 @@
use calibre_db::data::error::DataStoreError;
use poem::{error::ResponseError, http::StatusCode, Body, Response};
use thiserror::Error;
use tracing::error;
use uuid::Uuid;
#[derive(Debug, thiserror::Error)]
#[error("sqlite error")]
pub struct SqliteError(pub DataStoreError);
use crate::opds::error::OpdsError;
impl From<DataStoreError> for SqliteError {
fn from(item: DataStoreError) -> Self {
SqliteError(item)
}
#[derive(Error, Debug)]
#[error("opds error")]
pub enum HandlerError {
#[error("opds error")]
OpdsError(#[from] OpdsError),
#[error("data error")]
DataError(#[from] DataStoreError),
}
impl ResponseError for SqliteError {
impl ResponseError for HandlerError {
fn status(&self) -> StatusCode {
match &self.0 {
DataStoreError::NoResults(_) => StatusCode::NOT_FOUND,
_ => StatusCode::INTERNAL_SERVER_ERROR,
match &self {
HandlerError::OpdsError(e) => StatusCode::INTERNAL_SERVER_ERROR,
HandlerError::DataError(e) => match e {
DataStoreError::NoResults(_) => StatusCode::NOT_FOUND,
_ => StatusCode::INTERNAL_SERVER_ERROR,
},
}
}
fn as_response(&self) -> Response {
let id = Uuid::new_v4();
let internal_msg = format!("{:?}", self);
let external_msg = match &self.0 {
DataStoreError::NoResults(_) => "item not found",
_ => "internal server error",
let external_msg = match &self {
HandlerError::OpdsError(e) => "internal server error",
HandlerError::DataError(e) => match e {
DataStoreError::NoResults(_) => "item not found",
_ => "internal server error",
},
};
error!("{id}: {internal_msg}");

View file

@ -2,14 +2,14 @@ use std::sync::Arc;
use poem::{
handler,
web::{headers::ContentType, Data, WithContentType},
web::{Data, WithContentType},
IntoResponse,
};
use quick_xml::se::to_string;
use time::macros::datetime;
use time::OffsetDateTime;
use crate::{
app_state::AppState,
handlers::error::HandlerError,
opds::{
author::Author, content::Content, entry::Entry, feed::Feed, link::Link,
media_type::MediaType, relation::Relation,
@ -18,6 +18,8 @@ use crate::{
#[handler]
pub async fn handler(state: Data<&Arc<AppState>>) -> Result<WithContentType<String>, poem::Error> {
let now = OffsetDateTime::now_utc();
let author = Author {
name: "Thallian".to_string(),
uri: "https://code.vanwa.ch/shu/rusty-library".to_string(),
@ -40,7 +42,7 @@ pub async fn handler(state: Data<&Arc<AppState>>) -> Result<WithContentType<Stri
let books_entry = Entry {
title: "Books".to_string(),
id: "rusty:books".to_string(),
updated: datetime!(2024-05-06 19:14:54 UTC),
updated: now,
content: Content {
media_type: MediaType::Text,
content: "Index of all books".to_string(),
@ -57,12 +59,13 @@ pub async fn handler(state: Data<&Arc<AppState>>) -> Result<WithContentType<Stri
let feed = Feed {
title: "rusty-library".to_string(),
id: "rusty:catalog".to_string(),
updated: datetime!(2024-05-06 19:14:54 UTC),
updated: now,
icon: "favicon.ico".to_string(),
author,
links: vec![home_link, self_link],
entries: vec![books_entry],
};
let xml = feed.as_xml();
let xml = feed.as_xml().map_err(HandlerError::OpdsError)?;
Ok(xml.with_content_type("application/atom+xml"))
}

View file

@ -7,7 +7,7 @@ use tera::Context;
use crate::templates::TEMPLATES;
use super::error::SqliteError;
use super::error::HandlerError;
pub fn render<T: Serialize + Debug, F, S, P, M>(
template: &str,
@ -22,7 +22,7 @@ where
P: Fn(&str) -> Result<bool, DataStoreError>,
M: Fn(&str) -> Result<bool, DataStoreError>,
{
let items = fetcher().map_err(SqliteError)?;
let items = fetcher().map_err(HandlerError::DataError)?;
let mut context = Context::new();
@ -32,8 +32,8 @@ where
let (backward_cursor, forward_cursor) = (sort_field(first_item), sort_field(last_item));
let has_previous = has_previous(&backward_cursor).map_err(SqliteError)?;
let has_more = has_more(&forward_cursor).map_err(SqliteError)?;
let has_previous = has_previous(&backward_cursor).map_err(HandlerError::DataError)?;
let has_more = has_more(&forward_cursor).map_err(HandlerError::DataError)?;
context.insert("has_previous", &has_previous);
context.insert("has_more", &has_more);

View file

@ -8,12 +8,15 @@ use poem::{
use tera::Context;
use crate::{
app_state::AppState, data::book::Book, handlers::error::SqliteError, templates::TEMPLATES,
app_state::AppState, data::book::Book, handlers::error::HandlerError, templates::TEMPLATES,
};
#[handler]
pub async fn handler(state: Data<&Arc<AppState>>) -> Result<Html<String>, poem::Error> {
let recent_books = state.calibre.recent_books(25).map_err(SqliteError)?;
let recent_books = state
.calibre
.recent_books(25)
.map_err(HandlerError::DataError)?;
let recent_books = recent_books
.iter()
.filter_map(|x| Book::full_book(x, &state))

View file

@ -8,7 +8,7 @@ use poem::{
use tera::Context;
use crate::{
app_state::AppState, data::book::Book, handlers::error::SqliteError, templates::TEMPLATES,
app_state::AppState, data::book::Book, handlers::error::HandlerError, templates::TEMPLATES,
};
#[handler]
@ -16,8 +16,14 @@ pub async fn handler(
id: Path<u64>,
state: Data<&Arc<AppState>>,
) -> Result<Html<String>, poem::Error> {
let series = state.calibre.scalar_series(*id).map_err(SqliteError)?;
let books = state.calibre.series_books(*id).map_err(SqliteError)?;
let series = state
.calibre
.scalar_series(*id)
.map_err(HandlerError::DataError)?;
let books = state
.calibre
.series_books(*id)
.map_err(HandlerError::DataError)?;
let books = books
.iter()
.filter_map(|x| Book::full_book(x, &state))