From fe279c4d850a026ed1caf9a2f7d73eec0a79071f Mon Sep 17 00:00:00 2001 From: Ry Date: Fri, 11 Mar 2022 11:03:27 -0800 Subject: [PATCH] fox32: Introduce better error handling Instead of panicing, we now just print an error and exit in some situations where a panic is too verbose. Maybe we should just change the panic handler instead though? --- src/main.rs | 13 +++++++++++-- src/memory.rs | 8 ++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8c98734..079ff7d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,7 @@ use mouse::Mouse; use std::env; use std::fs::{File, read}; -//use std::process::exit; +use std::process::exit; use std::sync::{Arc, mpsc, Mutex}; use std::thread; @@ -47,11 +47,20 @@ fn read_rom() -> Vec { read("fox32.rom").unwrap_or_else(|_| { println!("fox32.rom not found, attempting to open ../fox32rom/fox32.rom instead"); read("../fox32rom/fox32.rom").unwrap_or_else(|_| { - panic!("oh fuck"); + error("fox32.rom not found!"); }) }) } +pub fn error(message: &str) -> ! { + println!("Error: {}", message); + exit(1); +} + +pub fn warn(message: &str) { + println!("Warning: {}", message); +} + fn main() { let version_string = format!("fox32 {} ({})", env!("VERGEN_BUILD_SEMVER"), env!("VERGEN_GIT_SHA_SHORT")); println!("{}", version_string); diff --git a/src/memory.rs b/src/memory.rs index 35a5ff6..7e25702 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -1,6 +1,6 @@ // memory.rs -use crate::Overlay; +use crate::{Overlay, warn}; use std::sync::{Arc, Mutex}; @@ -50,7 +50,7 @@ impl Memory { } else if address >= rom_bottom_address && address <= rom_top_address { self.rom[address - rom_bottom_address] } else { - println!("Warning: attempting to read unmapped memory address: {:#010X}", address); + warn(&format!("attempting to read unmapped memory address: {:#010X}", address)); 0 } } @@ -82,9 +82,9 @@ impl Memory { let mut shared_memory_lock = self.shared_memory.lock().unwrap(); shared_memory_lock[address - shared_bottom_address] = byte; } else if address >= rom_bottom_address && address <= rom_top_address { - println!("Warning: attempting to write to ROM address: {:#010X}", address); + warn(&format!("attempting to write to ROM address: {:#010X}", address)); } else { - println!("Warning: attempting to write to unmapped memory address: {:#010X}", address); + warn(&format!("attempting to write to unmapped memory address: {:#010X}", address)); } } pub fn write_16(&mut self, address: u32, half: u16) {