From 5fca81c6bf465b4c9fb6d28f0a53e8dbb9785931 Mon Sep 17 00:00:00 2001 From: Marto Date: Sat, 18 Jan 2025 23:42:10 +0100 Subject: [PATCH] fixed panic messages --- src/json.rs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/json.rs b/src/json.rs index d998144..01199a7 100644 --- a/src/json.rs +++ b/src/json.rs @@ -1,25 +1,20 @@ use std::path::Path; use std::fs::File; use std::io::{ErrorKind, Read}; -use std::process::exit; use std::collections::HashMap; pub fn get_json(path: &Path) -> HashMap { let mut data = HashMap::new(); let json_data = read_file(path); - let parsed_data = json::parse(json_data.as_str()).unwrap_or_else(|e| { - eprintln!("[ERROR] Invalid JSON: {e}"); - exit(1); - }); + let parsed_data = json::parse(json_data.as_str()).expect("[ERROR] Invalid JSON"); let ha_url = parsed_data["ha_url"].clone(); let access_token = parsed_data["access_token"].clone(); let light_entity_id = parsed_data["light_entity_id"].clone(); if ha_url.is_null() || access_token.is_null() || light_entity_id.is_null() { - eprintln!("[ERROR] JSON data is NULL"); - exit(1); + panic!("[ERROR] JSON data is NULL"); } data.insert("ha_url".to_string(), json::stringify(ha_url)); @@ -44,11 +39,8 @@ fn read_file(path: &Path) -> String { } }, Err(err) => match err.kind() { - ErrorKind::NotFound => { - eprintln!("[ERROR] The file '{}' does not exist", path.display()); - exit(1); - }, - _ => panic!("[ERROR] Unexpected error, closing ..."), + ErrorKind::NotFound => panic!("[ERROR] The file '{}' does not exist", path.display()), + _ => panic!("[ERROR] Unexpected error reading the JSON"), } } } \ No newline at end of file