about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-06-15 23:18:00 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-06-15 23:18:00 +0200
commitc4f8c14b5636055a2973afe0d5ef6494d97a1a76 (patch)
treef0d9ac6eabdbd22175349d46521736a6d2e606f1
parentbuild(treewide): Update (diff)
downloadyt-c4f8c14b5636055a2973afe0d5ef6494d97a1a76.zip
fix(yt_dlp/json_{cast,get}): Improve error reporting
-rw-r--r--crates/yt_dlp/src/lib.rs36
1 files changed, 27 insertions, 9 deletions
diff --git a/crates/yt_dlp/src/lib.rs b/crates/yt_dlp/src/lib.rs
index dd42fc6..1f859fe 100644
--- a/crates/yt_dlp/src/lib.rs
+++ b/crates/yt_dlp/src/lib.rs
@@ -22,19 +22,37 @@ pub mod progress_hook;
 
 #[macro_export]
 macro_rules! json_get {
-    ($value:expr, $name:literal, $into:ident) => {
-        $crate::json_cast!($value.get($name).expect("Should exist"), $into)
-    };
+    ($value:expr, $name:literal, $into:ident) => {{
+        match $value.get($name) {
+            Some(val) => $crate::json_cast!(val, $into),
+            None => panic!(
+                concat!(
+                    "Expected '",
+                    $name,
+                    "' to be a key for the'",
+                    stringify!($value),
+                    "' object: {:#?}"
+                ),
+                $value
+            ),
+        }
+    }};
 }
 
 #[macro_export]
 macro_rules! json_cast {
-    ($value:expr, $into:ident) => {
-        $value.$into().expect(concat!(
-            "Should be able to cast value into ",
-            stringify!($into)
-        ))
-    };
+    ($value:expr, $into:ident) => {{
+        match $value.$into() {
+            Some(result) => result,
+            None => panic!(
+                concat!(
+                    "Expected to be able to cast value ({:#?}) ",
+                    stringify!($into)
+                ),
+                $value
+            ),
+        }
+    }};
 }
 
 /// The core of the `yt_dlp` interface.