Skip to main content

Handle the case where a GIF doesn’t exist

ID
9602917
date
2021-11-29 23:25:12+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
1b94473
message
Handle the case where a GIF doesn't exist
changed files
2 files, 17 additions, 1 deletion

Changed files

src/get_bytes.rs (3795) → src/get_bytes.rs (3921)

diff --git a/src/get_bytes.rs b/src/get_bytes.rs
index 12e79f2..0072217 100644
--- a/src/get_bytes.rs
+++ b/src/get_bytes.rs
@@ -34,7 +34,14 @@ pub fn get_bytes_for_image(path: &str) -> Vec<u8> {
 }
 
 pub fn get_bytes_for_gif(path: &str) -> Vec<u8> {
-    let f = File::open(path).unwrap();
+    let f = match File::open(path) {
+        Ok(im) => im,
+        Err(e) => {
+            eprintln!("{}", e);
+            std::process::exit(1);
+        },
+    };
+
     let decoder = GifDecoder::new(f).ok().unwrap();
 
     // If the GIF is animated, we want to make sure we look at multiple

src/main.rs (8278) → src/main.rs (8567)

diff --git a/src/main.rs b/src/main.rs
index c578100..7ae2e07 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -198,6 +198,15 @@ mod tests {
     }
 
     #[test]
+    fn it_fails_if_you_pass_an_nonexistent_gif() {
+        let output = get_failure(&["./doesnotexist.gif"]);
+
+        assert_eq!(output.exit_code, 1);
+        assert_eq!(output.stdout, "");
+        assert_eq!(output.stderr, "No such file or directory (os error 2)\n");
+    }
+
+    #[test]
     fn it_fails_if_you_pass_a_non_image_file() {
         let output = get_failure(&["./README.md"]);