use thiserror::Error;
use crate::result::{ClassifyError, ErrorKind};
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum PaginationError {
#[error("default page_size must be greater than zero")]
DefaultPageSizeZero,
#[error("max page_size must be greater than zero")]
MaxPageSizeZero,
#[error("default page_size {default} must be less than or equal to max page_size {max}")]
DefaultPageSizeTooLarge {
#[doc = "The configured default."]
default: u32,
#[doc = "The configured upper bound."]
max: u32,
},
#[error("max cursor length must be greater than zero")]
MaxCursorLengthZero,
#[error("page must be greater than zero")]
PageZero,
#[error("page_size must be greater than zero")]
PageSizeZero,
#[error("page_size must be less than or equal to {max}")]
PageSizeTooLarge {
#[doc = "The configured upper bound."]
max: u32,
},
#[error("cursor is too large; maximum length is {max} bytes")]
CursorTooLarge {
#[doc = "The configured upper bound."]
max: usize,
},
#[error("cursor must not be empty")]
CursorEmpty,
#[error("cursor contains unsupported characters")]
CursorInvalidCharacters,
#[error("page contains {items} items, which exceeds page_size {page_size}")]
PageItemsExceedPageSize {
#[doc = "The number of returned items."]
items: usize,
#[doc = "The configured page size."]
page_size: u32,
},
#[error("next_cursor is required when has_more is true")]
CursorRequiredForMoreResults,
#[error("next_cursor must be absent when has_more is false")]
CursorUnexpectedWithoutMoreResults,
#[error("total {total} is less than returned item count {items}")]
TotalLessThanItems {
#[doc = "The total number of items."]
total: u64,
#[doc = "The number of returned items."]
items: usize,
},
}
impl ClassifyError for PaginationError {
fn error_kind(&self) -> ErrorKind {
ErrorKind::Validation
}
fn error_code(&self) -> crate::result::ErrorCode {
crate::result::ErrorCode::internal(match self {
Self::DefaultPageSizeZero => "pagination_default_page_size_zero",
Self::MaxPageSizeZero => "pagination_max_page_size_zero",
Self::DefaultPageSizeTooLarge { .. } => "pagination_default_page_size_too_large",
Self::MaxCursorLengthZero => "pagination_max_cursor_length_zero",
Self::PageZero => "pagination_page_zero",
Self::PageSizeZero => "pagination_page_size_zero",
Self::PageSizeTooLarge { .. } => "pagination_page_size_too_large",
Self::CursorTooLarge { .. } => "pagination_cursor_too_large",
Self::CursorEmpty => "pagination_cursor_empty",
Self::CursorInvalidCharacters => "pagination_cursor_invalid_characters",
Self::PageItemsExceedPageSize { .. } => "pagination_page_items_exceed_page_size",
Self::CursorRequiredForMoreResults => "pagination_cursor_required_for_more_results",
Self::CursorUnexpectedWithoutMoreResults => {
"pagination_cursor_unexpected_without_more_results"
}
Self::TotalLessThanItems { .. } => "pagination_total_less_than_items",
})
}
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum CursorError {
#[error("cursor must not be empty")]
Empty,
#[error("cursor is too large; maximum length is {max} bytes")]
TooLarge {
#[doc = "The configured upper bound."]
max: usize,
},
#[error("cursor encoding is invalid")]
InvalidEncoding,
#[error("cursor payload is invalid")]
InvalidPayload(#[from] serde_json::Error),
#[error("cursor signature is invalid")]
InvalidSignature,
#[error("cursor secret must contain at least {min_bytes} bytes")]
InvalidSecret {
#[doc = "The minimum number of bytes."]
min_bytes: usize,
},
#[error("cursor kind is invalid")]
InvalidKind,
#[error("cursor kind mismatch: expected {expected}, got {actual:?}")]
KindMismatch {
expected: String,
actual: Option<String>,
},
#[error("cursor version {actual} is not supported")]
UnsupportedVersion {
#[doc = "The value that was encountered."]
actual: u8,
},
}
impl ClassifyError for CursorError {
fn error_kind(&self) -> ErrorKind {
ErrorKind::Validation
}
fn error_code(&self) -> crate::result::ErrorCode {
crate::result::ErrorCode::internal(match self {
Self::Empty => "cursor_empty",
Self::TooLarge { .. } => "cursor_too_large",
Self::InvalidEncoding => "cursor_invalid_encoding",
Self::InvalidPayload(_) => "cursor_invalid_payload",
Self::InvalidSignature => "cursor_invalid_signature",
Self::InvalidSecret { .. } => "cursor_invalid_secret",
Self::InvalidKind => "cursor_invalid_kind",
Self::KindMismatch { .. } => "cursor_kind_mismatch",
Self::UnsupportedVersion { .. } => "cursor_unsupported_version",
})
}
}