-
Notifications
You must be signed in to change notification settings - Fork 13
/
error.rs
99 lines (88 loc) · 2.77 KB
/
error.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
pub use kfs_libkern::error::KernelError;
use failure::Backtrace;
use core::fmt;
#[derive(Debug, Fail)]
pub enum Error {
Kernel(KernelError, Backtrace),
Sm(SmError, Backtrace),
//Vi(ViError, Backtrace),
Libuser(LibuserError, Backtrace),
Unknown(u32, Backtrace)
}
impl Error {
pub fn from_code(errcode: u32) -> Error {
let module = errcode & 0x1F;
let description = errcode >> 9;
match Module(module) {
Module::Kernel => Error::Kernel(KernelError::from_description(description), Backtrace::new()),
Module::Sm => Error::Sm(SmError(description), Backtrace::new()),
//Module::Vi => Error::Vi(ViError(description), Backtrace::new()),
Module::Libuser => Error::Libuser(LibuserError(description), Backtrace::new()),
_ => Error::Unknown(errcode, Backtrace::new())
}
}
pub fn into_code(&self) -> u32 {
match *self {
Error::Kernel(err, ..) => err.description() << 9 | Module::Kernel.0,
Error::Sm(err, ..) => err.0 << 9 | Module::Sm.0,
//Error::Vi(err, ..) => err.0 << 9 | Module::Vi.0,
Error::Libuser(err, ..) => err.0 << 9 | Module::Libuser.0,
Error::Unknown(err, ..) => err,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// TODO: Better Display implementation for libuser::Error
// BODY: Right now, the libuser::Error Display just shims back to the Debug implementation.
// BODY: It'd be nice if it delegated the display to the underlying Error types.
write!(f, "Error: {:?}", self)
}
}
impl From<KernelError> for Error {
fn from(error: KernelError) -> Self {
Error::Kernel(error, Backtrace::new())
}
}
enum_with_val! {
#[derive(PartialEq, Eq, Clone, Copy)]
struct Module(u32) {
Kernel = 1,
Sm = 21,
Vi = 114,
Libuser = 115
}
}
enum_with_val! {
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct LibuserError(u32) {
AddressSpaceExhausted = 1,
InvalidMoveHandleCount = 2,
InvalidCopyHandleCount = 3,
PidMissing = 4,
}
}
impl From<LibuserError> for Error {
fn from(error: LibuserError) -> Self {
Error::Libuser(error, Backtrace::new())
}
}
enum_with_val! {
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct SmError(u32) {
OutOfProcesses = 1,
NotInitialized = 2,
MaxSessions = 3,
ServiceAlreadyRegistered = 4,
OutOfServices = 5,
InvalidName = 6,
ServiceNotRegistered = 7,
PermissionDenied = 8,
ServiceAccessControlTooBig = 9,
}
}
impl From<SmError> for Error {
fn from(error: SmError) -> Self {
Error::Sm(error, Backtrace::new())
}
}