use knot_wire::WireError; #[derive(Debug, Clone)] pub enum KnotError { Wire(WireError), } impl KnotError { pub fn internal(message: impl Into) -> Self { KnotError::Wire(WireError::internal(message)) } pub fn upstream(message: impl Into) -> Self { KnotError::Wire(WireError::upstream_unavailable(message)) } pub fn into_wire(self) -> WireError { match self { KnotError::Wire(error) => error, } } } impl std::fmt::Display for KnotError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { KnotError::Wire(error) => error.fmt(f), } } } impl From for KnotError { fn from(error: WireError) -> Self { KnotError::Wire(error) } } impl From for KnotError { fn from(error: worker::Error) -> Self { KnotError::internal(error.to_string()) } } impl From for KnotError { fn from(error: knot_atproto::AtprotoError) -> Self { use knot_atproto::AtprotoError; if error.is_transient() { return KnotError::Wire(WireError::upstream_unavailable(error.to_string())); } match error { AtprotoError::Resolve(_) => { KnotError::Wire(WireError::invalid_request(error.to_string())) } AtprotoError::PlcSubmit { .. } => { KnotError::Wire(WireError::bad_gateway(error.to_string())) } other => KnotError::internal(other.to_string()), } } } impl From for KnotError { fn from(error: knot_artifacts::ArtifactsError) -> Self { use knot_artifacts::ArtifactsError; match error { ArtifactsError::AlreadyExists(message) => KnotError::Wire(WireError::conflict(message)), ArtifactsError::NotFound(message) => KnotError::Wire(WireError::not_found(message)), ArtifactsError::Upstream(message) => KnotError::upstream(message), ArtifactsError::Binding(message) => KnotError::internal(message), } } }