From c68f5b8913c3d9c64db7ec0bd8049bf51606503b Mon Sep 17 00:00:00 2001 From: Samuel Vanderwaal Date: Wed, 15 Jun 2022 22:26:19 -0800 Subject: [PATCH] refactor: add error handling --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 20 +++++++++++++++++--- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 49b8f09..a152c50 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -170,7 +170,7 @@ checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" [[package]] name = "wtf-is" -version = "0.4.0" +version = "0.4.1" dependencies = [ "phf", ] diff --git a/Cargo.toml b/Cargo.toml index 30f6cc4..ad52010 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wtf-is" -version = "0.4.0" +version = "0.4.1" edition = "2021" license = "MIT" description = "I was tired of looking up Metaplex program errors." diff --git a/src/main.rs b/src/main.rs index bcfbc29..44a59ed 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,18 +10,32 @@ pub struct FoundError { } fn main() { - let error_code = args().nth(1).unwrap(); + let error_code = match args().nth(1) { + Some(code) => code, + None => { + println!("Usage: {} ", args().next().unwrap()); + std::process::exit(1); + } + }; let parsed_error_code = if error_code.contains("0x") { error_code.replace("0x", "") } else { - format!("{:X}", error_code.parse::().unwrap()) + let parsed_code = match error_code.parse::() { + Ok(code) => code, + Err(_) => { + println!("Error: {error_code} is not a valid error code"); + std::process::exit(1); + } + }; + + format!("{:X}", parsed_code) }; let errors = find_errors(&parsed_error_code); if errors.is_empty() { - println!("No errors for found code: {parsed_error_code}"); + println!("No errors for found code: 0x{parsed_error_code}"); std::process::exit(1); }