MpvError: add copy of command for better context
All checks were successful
Build and test / test (push) Successful in 2m13s
Build and test / build (push) Successful in 10m26s
Build and test / docs (push) Successful in 11m19s
Build and test / check (push) Successful in 10m27s

This commit is contained in:
Oystein Kristoffer Tveit 2024-12-12 16:26:44 +01:00
parent b2a22a9a57
commit 81479d2f64
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
5 changed files with 44 additions and 24 deletions

View File

@ -8,8 +8,11 @@ use crate::{MpvDataType, Property};
/// Any error that can occur when interacting with mpv. /// Any error that can occur when interacting with mpv.
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum MpvError { pub enum MpvError {
#[error("MpvError: {0}")] #[error("Mpv returned error in response to command: {message}\nCommand: {command:#?}")]
MpvError(String), MpvError {
command: Vec<Value>,
message: String,
},
#[error("Error communicating over mpv socket: {0}")] #[error("Error communicating over mpv socket: {0}")]
MpvSocketConnectionError(String), MpvSocketConnectionError(String),
@ -53,7 +56,16 @@ pub enum MpvError {
impl PartialEq for MpvError { impl PartialEq for MpvError {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
match (self, other) { match (self, other) {
(Self::MpvError(l0), Self::MpvError(r0)) => l0 == r0, (
Self::MpvError {
command: l_command,
message: l_message,
},
Self::MpvError {
command: r_command,
message: r_message,
},
) => l_command == r_command && l_message == r_message,
(Self::MpvSocketConnectionError(l0), Self::MpvSocketConnectionError(r0)) => l0 == r0, (Self::MpvSocketConnectionError(l0), Self::MpvSocketConnectionError(r0)) => l0 == r0,
(Self::InternalConnectionError(l0), Self::InternalConnectionError(r0)) => l0 == r0, (Self::InternalConnectionError(l0), Self::InternalConnectionError(r0)) => l0 == r0,
(Self::JsonParseError(l0), Self::JsonParseError(r0)) => { (Self::JsonParseError(l0), Self::JsonParseError(r0)) => {

View File

@ -92,7 +92,7 @@ impl MpvIpc {
log::trace!("Received response: {:?}", response); log::trace!("Received response: {:?}", response);
parse_mpv_response_data(response?) parse_mpv_response_data(response?, command)
} }
pub(crate) async fn get_mpv_property( pub(crate) async fn get_mpv_property(
@ -197,7 +197,7 @@ impl MpvIpc {
/// This function does the most basic JSON parsing and error handling /// This function does the most basic JSON parsing and error handling
/// for status codes and errors that all responses from mpv are /// for status codes and errors that all responses from mpv are
/// expected to contain. /// expected to contain.
fn parse_mpv_response_data(value: Value) -> Result<Option<Value>, MpvError> { fn parse_mpv_response_data(value: Value, command: &[Value]) -> Result<Option<Value>, MpvError> {
log::trace!("Parsing mpv response data: {:?}", value); log::trace!("Parsing mpv response data: {:?}", value);
let result = value let result = value
.as_object() .as_object()
@ -225,7 +225,10 @@ fn parse_mpv_response_data(value: Value) -> Result<Option<Value>, MpvError> {
.and_then(|(error, data)| match error { .and_then(|(error, data)| match error {
"success" => Ok(data), "success" => Ok(data),
"property unavailable" => Ok(None), "property unavailable" => Ok(None),
err => Err(MpvError::MpvError(err.to_string())), err => Err(MpvError::MpvError {
command: command.to_owned(),
message: err.to_string(),
}),
}); });
match &result { match &result {

View File

@ -47,10 +47,13 @@ async fn test_get_unavailable_property() -> Result<(), MpvError> {
async fn test_get_nonexistent_property() -> Result<(), MpvError> { async fn test_get_nonexistent_property() -> Result<(), MpvError> {
let (mut proc, mpv) = spawn_headless_mpv().await.unwrap(); let (mut proc, mpv) = spawn_headless_mpv().await.unwrap();
let nonexistent = mpv.get_property::<f64>("nonexistent").await; let nonexistent = mpv.get_property::<f64>("nonexistent").await;
assert_eq!(
nonexistent, match nonexistent {
Err(MpvError::MpvError("property not found".to_string())) Err(MpvError::MpvError { message, .. }) => {
); assert_eq!(message, "property not found");
}
_ => panic!("Unexpected result: {:?}", nonexistent),
}
mpv.kill().await.unwrap(); mpv.kill().await.unwrap();
proc.kill().await.unwrap(); proc.kill().await.unwrap();

View File

@ -151,8 +151,8 @@ async fn test_get_property_simultaneous_requests() {
tokio::time::sleep(Duration::from_millis(2)).await; tokio::time::sleep(Duration::from_millis(2)).await;
let maybe_volume = mpv_clone_3.get_property::<f64>("nonexistent").await; let maybe_volume = mpv_clone_3.get_property::<f64>("nonexistent").await;
match maybe_volume { match maybe_volume {
Err(MpvError::MpvError(err)) => { Err(MpvError::MpvError { message, .. }) => {
assert_eq!(err, "property not found"); assert_eq!(message, "property not found");
} }
_ => panic!("Unexpected result: {:?}", maybe_volume), _ => panic!("Unexpected result: {:?}", maybe_volume),
} }

View File

@ -63,12 +63,12 @@ async fn test_set_property_wrong_type() -> Result<(), MpvError> {
let mpv = Mpv::connect_socket(server).await?; let mpv = Mpv::connect_socket(server).await?;
let maybe_volume = mpv.set_property::<bool>("volume", true).await; let maybe_volume = mpv.set_property::<bool>("volume", true).await;
assert_eq!( match maybe_volume {
maybe_volume, Err(MpvError::MpvError { message, .. }) => {
Err(MpvError::MpvError( assert_eq!(message, "unsupported format for accessing property");
"unsupported format for accessing property".to_string() }
)) _ => panic!("Unexpected result: {:?}", maybe_volume),
); }
join_handle.await.unwrap().unwrap(); join_handle.await.unwrap().unwrap();
@ -84,10 +84,12 @@ async fn test_get_property_error() -> Result<(), MpvError> {
let mpv = Mpv::connect_socket(server).await?; let mpv = Mpv::connect_socket(server).await?;
let maybe_volume = mpv.set_property("nonexistent", true).await; let maybe_volume = mpv.set_property("nonexistent", true).await;
assert_eq!( match maybe_volume {
maybe_volume, Err(MpvError::MpvError { message, .. }) => {
Err(MpvError::MpvError("property not found".to_string())) assert_eq!(message, "property not found");
); }
_ => panic!("Unexpected result: {:?}", maybe_volume),
}
join_handle.await.unwrap().unwrap(); join_handle.await.unwrap().unwrap();
@ -161,8 +163,8 @@ async fn test_set_property_simultaneous_requests() {
tokio::time::sleep(Duration::from_millis(2)).await; tokio::time::sleep(Duration::from_millis(2)).await;
let maybe_volume = mpv_clone_3.set_property("nonexistent", "a").await; let maybe_volume = mpv_clone_3.set_property("nonexistent", "a").await;
match maybe_volume { match maybe_volume {
Err(MpvError::MpvError(err)) => { Err(MpvError::MpvError { message, .. }) => {
assert_eq!(err, "property not found"); assert_eq!(message, "property not found");
} }
_ => panic!("Unexpected result: {:?}", maybe_volume), _ => panic!("Unexpected result: {:?}", maybe_volume),
} }