use serde_json::Value;
use wick_interface_types::Type;
use crate::PacketError;
#[derive(Debug, thiserror::Error, PartialEq, Clone)]
#[non_exhaustive]
pub enum Error {
#[error("No stream found for port '{0}'")]
PortMissing(String),
#[error("Error deserializing payload '{}': {}",.as_json,.error)]
Decode { as_json: String, error: String },
#[error("Error JSON-ifying payload: {0}")]
Jsonify(String),
#[error("Error communicating over a stream or channel: {0}")]
Channel(String),
#[error("{0}")]
Component(String),
#[error("No data in payload")]
NoData,
#[error("{}", .0.msg())]
PayloadError(PacketError),
#[error("Got a Done signal in an unexpected context.")]
UnexpectedDone,
#[error("Could not retrieve configuration item '{0}'")]
ContextKey(String),
#[error("Can only convert JSON Objects to a operation and component configuration, got '{0}'")]
BadJson(Value),
#[error("Could not retrieve a complete set of packets. Stream '{0}' failed to provide a packet: '{1}'")]
StreamMapError(String , String ),
#[error("Could not retrieve a complete set of packets. Stream '{0}' completed or failed before providing a packet.")]
StreamMapMissing(String ),
#[error("Configuration provided for component '{0}' does not match expected signature, {1}")]
Signature(String, String),
#[cfg(feature = "datetime")]
#[error("Error parsing date '{0}', date must be an RFC 3339 formatted string")]
ParseDate(String),
#[cfg(feature = "datetime")]
#[error("Error parsing date '{0}', date must be milliseconds from the UNIX epoch")]
ParseDateMillis(u64),
#[error("Could not coerce value {value} to a {desired}")]
Coersion { value: Value, desired: Type },
}
impl Error {
pub fn component_error<T: Into<String>>(msg: T) -> Self {
Self::Component(msg.into())
}
}
impl From<wasmrs_rx::Error> for Error {
fn from(value: wasmrs_rx::Error) -> Self {
Self::Channel(value.to_string())
}
}
impl From<Box<dyn std::error::Error>> for Error {
fn from(value: Box<dyn std::error::Error>) -> Self {
Self::Component(value.to_string())
}
}
#[derive(thiserror::Error, Debug)]
#[non_exhaustive]
pub enum ParseError {
#[error("Invalid scheme {0}")]
Scheme(String),
#[error("Missing path")]
MissingPath,
#[error("Missing authority/host")]
Authority,
#[error("Invalid authority/host '{0}', missing separator '.'")]
InvalidAuthority(String),
#[error("Invalid authority/host kind '{0}'")]
InvalidAuthorityKind(String),
#[error("{0}")]
Parse(url::ParseError),
#[error(transparent)]
Conversion(Box<dyn std::error::Error + Send + Sync>),
}