//! Error types shared across the workspace. use serde::{Deserialize, Serialize}; use ts_rs::TS; /// Errors raised by the types themselves — parsing a window name, a malformed /// preimage. Deliberately small: this crate does no I/O, so it has few ways to /// fail. #[derive(Debug, thiserror::Error)] pub enum EntityError { /// A window name that is not one of the four. #[error("unknown window `{0}` (expected hour, six_hours, day or week)")] UnknownWindow(String), /// A miner id that is not 0x-prefixed 32-byte hex. #[error("`{0}` is not a 32-byte 0x-prefixed reward preimage")] MalformedMinerId(String), /// A chain name that is not configured. #[error("no chain named `{0}` is configured")] UnknownChain(String), } /// The JSON body every failing REST response carries. /// /// Shared with the frontend so error rendering is not a guess about what the /// backend emits. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)] #[ts(export, export_to = "ApiError.ts")] pub struct ApiError { /// A stable machine-readable code (`unknown_chain`, `not_found`, /// `chain_unavailable`). The frontend switches on this; `message` is for /// humans and may be reworded at any time. pub code: String, /// Human-readable explanation. pub message: String, } impl ApiError { /// Build one. pub fn new(code: &str, message: impl Into) -> Self { Self { code: code.to_owned(), message: message.into(), } } }