use knot_gitcore::CoreError; use knot_wire::WireError; #[derive(Debug, Clone)] pub enum KnotError { Wire(WireError), Git(CoreError), } 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, KnotError::Git(error) => error.into(), } } } 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), KnotError::Git(error) => error.fmt(f), } } } impl From for KnotError { fn from(error: WireError) -> Self { KnotError::Wire(error) } } impl From for KnotError { fn from(error: CoreError) -> Self { KnotError::Git(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()), } } }