Skip to content

Commit 160c78c

Browse files
author
hatf0
committed
add proper error handling
1 parent a8ea002 commit 160c78c

File tree

6 files changed

+297
-159
lines changed

6 files changed

+297
-159
lines changed

.vscode/settings.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"rust-analyzer.check.command": "clippy",
3+
"rust-analyzer.check.extraArgs": [
4+
"--",
5+
"-W",
6+
"clippy::pedantic",
7+
"-W",
8+
"clippy::nursery"
9+
],
10+
}

Cargo.lock

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ description = "A simple, batteries-included way of hooking standard library func
66
license = "MIT"
77

88
[lib]
9-
crate-type = ["cdylib"]
109
bench = false
1110

1211
[dependencies]
12+
anyhow = "1.0.77"
1313
data-encoding = "2.5.0"
1414
dynasm = "2.0.0"
1515
dynasmrt = "2.0.0"
@@ -19,3 +19,4 @@ mach2 = "0.4.2"
1919
mach_object = "0.1.17"
2020
nom = "7.1.3"
2121
system_error = "0.1.1"
22+
thiserror = "1.0.52"

src/error.rs

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
use nom::error::ErrorKind;
2+
use nom::error::FromExternalError;
3+
use nom::error::ParseError as NomParseError;
4+
use system_error::OsCode;
5+
use thiserror::Error;
6+
use system_error::Error as SystemError;
7+
use system_error::KernelCode;
8+
use anyhow::Result as AnyhowResult;
9+
10+
pub type Result<T> = AnyhowResult<T, anyhow::Error>;
11+
12+
#[derive(Error, Debug)]
13+
pub enum Error<I = &'static [u8]> {
14+
#[error("Expected length greater than 0")]
15+
ZeroLength,
16+
#[error("Dynasm error")]
17+
DynasmError,
18+
#[error("Invalid UTF-8 data for string")]
19+
InvalidUtf8Data,
20+
#[error("Missing segment (segment name: {0:?})")]
21+
MissingSegment(String),
22+
#[error("Parsing error: {0:?}")]
23+
Parse(Parse<I>),
24+
#[error("{0:?}")]
25+
System(SystemError),
26+
}
27+
28+
impl Error<()> {
29+
pub const fn zero_length() -> Self {
30+
Self::ZeroLength
31+
}
32+
33+
pub const fn dynasm_error() -> Self {
34+
Self::DynasmError
35+
}
36+
37+
pub const fn invalid_utf8_data() -> Self {
38+
Self::InvalidUtf8Data
39+
}
40+
41+
pub fn missing_segment(segment: &str) -> Self {
42+
Self::MissingSegment(segment.to_string())
43+
}
44+
45+
pub fn from_kernel_error(code: KernelCode) -> Self {
46+
Self::System(SystemError::from_raw_kernel_error(code))
47+
}
48+
49+
pub fn from_last_os_error() -> Self {
50+
Self::System(SystemError::last_os_error())
51+
}
52+
53+
pub fn from_os_error(code: OsCode) -> Self {
54+
Self::System(SystemError::from_raw_os_error(code))
55+
}
56+
}
57+
58+
impl<I> From<nom::Err<nom::error::Error<I>>> for Error<I> {
59+
fn from(value: nom::Err<nom::error::Error<I>>) -> Self {
60+
todo!()
61+
}
62+
}
63+
64+
#[derive(Debug)]
65+
pub enum Parse<I> {
66+
InvalidUtf8Data,
67+
Nom(I, ErrorKind),
68+
}
69+
70+
impl<I> NomParseError<I> for Parse<I> {
71+
fn from_error_kind(input: I, kind: ErrorKind) -> Self {
72+
Self::Nom(input, kind)
73+
}
74+
75+
fn append(_: I, _: ErrorKind, other: Self) -> Self {
76+
other
77+
}
78+
}
79+
80+
impl<I, E> FromExternalError<I, E> for Parse<I> {
81+
fn from_external_error(input: I, kind: ErrorKind, _: E) -> Self {
82+
Self::Nom(input, kind)
83+
}
84+
}

0 commit comments

Comments
 (0)