use std::fmt; #[derive(Debug)] pub enum PullError { BadRequest(String), Unauthorized, Forbidden, NotFound, Conflict(String), RateLimited(String), Internal(String), } impl fmt::Display for PullError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::BadRequest(message) => write!(f, "{message}"), Self::Unauthorized => write!(f, "unauthorized"), Self::Forbidden => write!(f, "forbidden"), Self::NotFound => write!(f, "not found"), Self::Conflict(message) => write!(f, "{message}"), Self::RateLimited(message) => write!(f, "{message}"), Self::Internal(message) => write!(f, "{message}"), } } } impl std::error::Error for PullError {} auth::auth_error_conversion!(PullError); api::db_error_conversions!(PullError); api::json_payload_conversion!(PullError); api::page_error_conversion!(PullError, BadRequest); api::domain_error_passthrough!(PullError from issues::errors::IssueError); impl From for PullError { fn from(error: git::facade::GitError) -> Self { use git::facade::GitError as G; match error { G::InvalidArgument(msg) => Self::BadRequest(msg), G::NotFound => Self::NotFound, G::Unauthorized => Self::Unauthorized, G::Forbidden => Self::Forbidden, G::Conflict(msg) => Self::Conflict(msg), G::Cache(_) | G::Git(_) | G::Internal(_) | G::Unimplemented(_) => { Self::Internal(error.to_string()) } } } } api::api_error! { log: "pulls", pub enum ApiError { BadRequest(400, "request failed validation"), Unauthorized(401, "authentication required"), Forbidden(403, "operation not allowed"), NotFound(404, "resource not found"), Conflict(409, "resource conflict"), TooManyRequests(429, "rate limit exceeded"), Internal(500, "internal server error"), } } impl From for ApiError { fn from(error: PullError) -> Self { match error { PullError::BadRequest(msg) => Self::bad_request(msg), PullError::Unauthorized => Self::unauthorized(), PullError::Forbidden => Self::forbidden(), PullError::NotFound => Self::not_found("pull request not found"), PullError::Conflict(msg) => Self::conflict_with_code("merge_blocked", msg), PullError::RateLimited(msg) => Self::rate_limited(msg), PullError::Internal(message) => Self::internal(message), } } }