fixed panic messages
This commit is contained in:
		
							
								
								
									
										16
									
								
								src/json.rs
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								src/json.rs
									
									
									
									
									
								
							@@ -1,25 +1,20 @@
 | 
				
			|||||||
use std::path::Path;
 | 
					use std::path::Path;
 | 
				
			||||||
use std::fs::File;
 | 
					use std::fs::File;
 | 
				
			||||||
use std::io::{ErrorKind, Read};
 | 
					use std::io::{ErrorKind, Read};
 | 
				
			||||||
use std::process::exit;
 | 
					 | 
				
			||||||
use std::collections::HashMap;
 | 
					use std::collections::HashMap;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn get_json(path: &Path) -> HashMap<String, String> {
 | 
					pub fn get_json(path: &Path) -> HashMap<String, String> {
 | 
				
			||||||
    let mut data = HashMap::new();
 | 
					    let mut data = HashMap::new();
 | 
				
			||||||
    let json_data = read_file(path);
 | 
					    let json_data = read_file(path);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let parsed_data = json::parse(json_data.as_str()).unwrap_or_else(|e| {
 | 
					    let parsed_data = json::parse(json_data.as_str()).expect("[ERROR] Invalid JSON");
 | 
				
			||||||
        eprintln!("[ERROR] Invalid JSON: {e}");
 | 
					 | 
				
			||||||
        exit(1);
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let ha_url = parsed_data["ha_url"].clone();
 | 
					    let ha_url = parsed_data["ha_url"].clone();
 | 
				
			||||||
    let access_token = parsed_data["access_token"].clone();
 | 
					    let access_token = parsed_data["access_token"].clone();
 | 
				
			||||||
    let light_entity_id = parsed_data["light_entity_id"].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() {
 | 
					    if ha_url.is_null() || access_token.is_null() || light_entity_id.is_null() {
 | 
				
			||||||
        eprintln!("[ERROR] JSON data is NULL");
 | 
					        panic!("[ERROR] JSON data is NULL");
 | 
				
			||||||
        exit(1);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    data.insert("ha_url".to_string(), json::stringify(ha_url));
 | 
					    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() {
 | 
					        Err(err) => match err.kind() {
 | 
				
			||||||
            ErrorKind::NotFound => {
 | 
					            ErrorKind::NotFound => panic!("[ERROR] The file '{}' does not exist", path.display()),
 | 
				
			||||||
                eprintln!("[ERROR] The file '{}' does not exist", path.display());
 | 
					            _ => panic!("[ERROR] Unexpected error reading the JSON"),
 | 
				
			||||||
                exit(1);
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
            _ => panic!("[ERROR] Unexpected error, closing ..."),
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user