//! static XRPC error vocabulary, mirrored from knot-xrpc/src/error.rs //! plus the endpoint-specific errors. #[derive(Debug, Clone, PartialEq, Eq)] pub struct StaticXrpcError { pub status: u16, pub error: &'static str, pub message: String, } impl std::fmt::Display for StaticXrpcError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}: {} ({})", self.status, self.error, self.message) } } impl std::error::Error for StaticXrpcError {} impl StaticXrpcError { pub fn new(status: u16, error: &'static str, message: impl Into) -> Self { Self { status, error, message: message.into(), } } pub fn body(&self) -> serde_json::Value { serde_json::json!({ "error": self.error, "message": self.message }) } pub fn repo_not_found() -> Self { Self::new(404, "RepoNotFound", "repository not found on this knot") } pub fn ref_not_found() -> Self { Self::new(404, "RefNotFound", "git reference not found") } pub fn path_not_found() -> Self { Self::new(404, "PathNotFound", "path not found in repository tree") } pub fn file_not_found() -> Self { Self::new(404, "FileNotFound", "file not found at specified path") } pub fn branch_not_found() -> Self { Self::new(404, "BranchNotFound", "branch not found") } pub fn tag_not_found() -> Self { Self::new(400, "TagNotFound", "tag not found") } pub fn revision_not_found(rev: impl std::fmt::Display) -> Self { Self::new( 400, "RevisionNotFound", format!("error resolving revision {rev}"), ) } pub fn invalid_request(message: impl Into) -> Self { Self::new(400, "InvalidRequest", message) } pub fn disallowed_raw_mime() -> Self { Self::new( 403, "InvalidRequest", "only image, video, and text files can be accessed directly", ) } pub fn compare_error(message: impl Into) -> Self { Self::new(400, "CompareError", message) } pub fn blob_too_large() -> Self { Self::new(413, "BlobTooLarge", "file is too large to serve") } pub fn request_too_large(message: impl Into) -> Self { Self::new(413, "RequestTooLarge", message) } pub fn internal(message: impl Into) -> Self { Self::new(500, "InternalError", message) } } impl From for StaticXrpcError { fn from(err: knot_git::GitError) -> Self { Self::internal(err.to_string()) } }