use std::net::SocketAddr;
use std::time::Duration;
use crate::protocol::DeviceError;
use crate::push::PushUnavailable;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("no reply from {addr} to `{method}` after {attempts} attempts in {elapsed:?}")]
Timeout {
method: String,
addr: SocketAddr,
attempts: u32,
elapsed: Duration,
},
#[error("udp error: {0}")]
Io(#[from] std::io::Error),
#[error("malformed json: {0}")]
Json(#[from] serde_json::Error),
#[error("push updates unavailable: {0}")]
PushUnavailable(#[from] PushUnavailable),
#[error("bulb rejected `{method}`: {message} (code {code})")]
Device {
method: String,
code: i64,
message: String,
},
#[error("`{method}` is not supported by this bulb")]
NotSupported {
method: String,
},
#[error("unknown model: {message}")]
UnknownModel {
message: String,
},
#[error("unknown scene: {message}")]
UnknownScene {
message: String,
},
#[error("invalid parameter: {message}")]
InvalidParam {
message: String,
},
}
impl Error {
pub(crate) fn from_device(method: &str, error: DeviceError) -> Self {
match error.code {
-32601 => Self::NotSupported {
method: method.to_owned(),
},
-32602 => Self::InvalidParam {
message: format!("bulb rejected the params of `{method}`: {}", error.message),
},
code => Self::Device {
method: method.to_owned(),
code,
message: error.message,
},
}
}
}