Skip to content

Commit

Permalink
Improved Error Handling
Browse files Browse the repository at this point in the history
Added error types that will be matchable in many situations, allowing
for better error handling.
  • Loading branch information
TheEmeraldBee committed Jun 1, 2024
1 parent b7de4e5 commit 998b602
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ package.version = "0.0.0"
anyhow = "1.0.75"
crossterm = "0.27.0"
ratatui = "0.26.2"
thiserror = "1.0.61"
tui-helper-proc-macro = { path = "tui-helper-proc-macro", version = "0.0.0" }
6 changes: 4 additions & 2 deletions src/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use ratatui::prelude::Rect;

use crate::*;

use self::widget::WidgetError;

/// The default system of storage for the system.
#[derive(Default, State)]
pub struct Chunks {
Expand All @@ -28,10 +30,10 @@ impl Chunks {

/// Returns a rect if the type id is within the chunk,
/// an error is thrown if it isn't registered.
pub fn get_chunk<T: Any>(&self) -> Result<Rect, Box<dyn Error>> {
pub fn get_chunk<T: Any>(&self) -> Result<Rect, WidgetError> {
match self.chunks.get(&TypeId::of::<T>()).cloned() {
Some(chunk) => Ok(chunk),
None => Err(anyhow!("Chunk doesn't exist").into()),
None => Err(WidgetError::ChunkError),
}
}
}
14 changes: 12 additions & 2 deletions src/widget/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,21 @@ use crate::{states::States, WidgetFrame};
use std::{
any::{Any, TypeId},
collections::HashMap,
error::Error,
};
use thiserror::Error;

#[derive(Error, Debug)]
pub enum WidgetError {
#[error(transparent)]
Io(#[from] std::io::Error),
#[error("Chunk doesn't exist")]
ChunkError,
#[error(transparent)]
Misc(#[from] anyhow::Error),
}

/// The main result that a widget will always return.
pub type WidgetResult = Result<(), Box<dyn Error>>;
pub type WidgetResult = Result<(), WidgetError>;

/// A widget that can be called.
pub trait Widget {
Expand Down

0 comments on commit 998b602

Please sign in to comment.