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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
//! Error handling //! //! Errors in Horizon/NX follow a specific format. They are encoded on a 32-bit //! integer, where the bottom 9 bits represent the module, while the top bits //! represent the "description". A Module is usually a sysmodule, with a few //! additional modules for specific libraries (Module 168 is userland crash, for //! instance). See the [switchbrew Error Codes page] for more information. //! //! Such errors are nice, but have one small problem: they're missing backtraces. //! In libuser, we opted to use a failure enum. We have a different enum for //! every module, listing all of their error descriptions, and a big enum //! over all those module errors. This fine-grained approach makes error handling //! code nicer. For instance, writing a function returning an Error: //! //! ``` //! use sunrise_libuser::error::{KernelError, SmError, Error}; //! fn ret_err() -> Result<(), Error> { //! // Will automatically be converted to Error, the backtrace filled //! let _ = Err(KernelError::PortRemoteDead)?; //! let _ = Err(SmError::PermissionDenied)?; //! Ok(()) //! } //! ``` //! //! Matching on an error is similarly convenient: //! //! ``` //! use sunrise_libuser::error::{KernelError, Error}; //! # let err: Result<(), Error> = Ok(()); //! match err { //! Ok(_) => (), //! Err(Error::Kernel(KernelError::PortRemoteDead, _)) => (), //! _ => () //! } //! ``` //! //! [switchbrew Error Codes page]: https://switchbrew.org/w/index.php?title=Error_codes pub use sunrise_libkern::error::KernelError; use failure::Backtrace; use core::fmt; /// The global error type. Every error defined here can be downcasted to this /// type. A Backtrace will be created when casting an error to this type. #[derive(Debug)] pub enum Error { /// A Kernel Error. Usually returned by syscalls. Kernel(KernelError, Backtrace), /// Loader error. Loader(LoaderError, Backtrace), /// Process Manager error. Pm(PmError, Backtrace), /// Service Manager error. Sm(SmError, Backtrace), /// Vi Error Vi(ViError, Backtrace), /// Internal Libuser error. Libuser(LibuserError, Backtrace), /// Ahci driver error. Ahci(AhciError, Backtrace), /// Time errors Time(TimeError, Backtrace), /// Filesystem driver error FileSystem(FileSystemError, Backtrace), /// HID errors Hid(HidError, Backtrace), /// Twili Pipe errors Twili(TwiliError, Backtrace), /// An unknown error type. Either someone returned a custom error, or this /// version of libuser is outdated. Unknown(u32, Backtrace) } impl Error { /// Create an Error from a packed error code, creating a backtrace at this /// point. pub fn from_code(errcode: u32) -> Error { let module = errcode & 0x1FF; let description = errcode >> 9; match Module(module) { Module::Kernel => Error::Kernel(KernelError::from_description(description), Backtrace::new()), Module::FileSystem => Error::FileSystem(FileSystemError(description), Backtrace::new()), Module::Loader => Error::Loader(LoaderError(description), Backtrace::new()), Module::Pm => Error::Pm(PmError(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()), Module::Time => Error::Time(TimeError(description), Backtrace::new()), Module::Ahci => Error::Ahci(AhciError(description), Backtrace::new()), Module::Hid => Error::Hid(HidError(description), Backtrace::new()), _ => Error::Unknown(errcode, Backtrace::new()) } } /// Pack this error into an error code. Note that the returned error code /// won't have any tracing information associated with it. If possible, to /// assist in debugging, a way to pass the backtrace should be provided. pub fn as_code(&self) -> u32 { match *self { Error::Kernel(err, ..) => err.description() << 9 | Module::Kernel.0, Error::FileSystem(err, ..) => err.0 << 9 | Module::FileSystem.0, Error::Loader(err, ..) => err.0 << 9 | Module::Loader.0, Error::Pm(err, ..) => err.0 << 9 | Module::Pm.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::Ahci(err, ..) => err.0 << 9 | Module::Ahci.0, Error::Time(err, ..) => err.0 << 9 | Module::Time.0, Error::Hid(err, ..) => err.0 << 9 | Module::Hid.0, Error::Twili(err, ..) => err.0 << 9 | Module::Twili.0, Error::Unknown(err, ..) => err, } } } enum_with_val! { #[derive(PartialEq, Eq, Clone, Copy)] struct Module(u32) { Kernel = 1, FileSystem = 2, Loader = 9, Pm = 15, Sm = 21, Vi = 114, Time = 116, Hid = 202, Libuser = 415, Ahci = 416, Twili = 417, } } 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! { /// FileSystem driver errors. #[derive(PartialEq, Eq, Clone, Copy)] pub struct FileSystemError(u32) { /// Unknown error. Unknown = 0, /// The given resource couldn't be found. PathNotFound = 1, /// A resource at the given path already exist. PathExists = 2, /// Resource already in use. InUse = 7, /// There isn't enough space for a resource to be stored. NoSpaceLeft = 39, /// The partition wasn't used as it's invalid. InvalidPartition = 1001, /// Specified value is out of range. OutOfRange = 3005, /// A writing operation failed on the attached storage device. WriteFailed = 4002, /// A read operation failed on the attached storage device. ReadFailed = 4003, /// The given partition cannot be found. PartitionNotFound = 4004, /// The given input wasn't valid. InvalidInput = 6001, /// The given path is too long to be resolved. PathTooLong = 6003, /// Attempted to modify a read-only filesystem. ReadOnlyFileSystem = 6369, /// The access to a given resource has been denied. AccessDenied = 6400, /// The requested file wasn't found. FileNotFound = 6602, /// The requested operation isn't supported by the detail. UnsupportedOperation = 6300, /// The requested directory wasn't found. DirectoryNotFound = 6603, /// The given resource cannot be represented as a file. NotAFile = 8005, /// The given resource cannot be represented as a directory. NotADirectory = 8006, /// The given disk id doesn't correspond to a any known disk. DiskNotFound = 8007, } } impl From<FileSystemError> for Error { fn from(error: FileSystemError) -> Self { Error::FileSystem(error, Backtrace::new()) } } enum_with_val! { /// Internal libuser errors. #[derive(PartialEq, Eq, Clone, Copy)] pub struct LibuserError(u32) { /// An attempt to find available space failed. AddressSpaceExhausted = 1, /// Too many move handles were passed to an IPC message. InvalidMoveHandleCount = 2, /// Too many copy handles were passed to an IPC message. InvalidCopyHandleCount = 3, /// Attempted to read PID from an IPC message containing none. PidMissing = 4, /// Not enough IPC buffers were passed to an IPC message. InvalidIpcBufferCount = 5, /// Invalid IPCBuffer InvalidIpcBuffer = 6, /// Invalid IPC request InvalidIpcRequest = 7, } } impl From<LibuserError> for Error { fn from(error: LibuserError) -> Self { Error::Libuser(error, Backtrace::new()) } } enum_with_val! { /// Service Manager errors. #[derive(PartialEq, Eq, Clone, Copy)] pub struct SmError(u32) { /// Too many processes spawned. OutOfProcesses = 1, /// Attempted to use the service manager without initializing it. NotInitialized = 2, /// This service already reached the maximum amount of sessions allowed to connect to it. MaxSessions = 3, /// Attempted to register a service that already exists. ServiceAlreadyRegistered = 4, /// Too many services have been created. OutOfServices = 5, /// The name is too long. Make sure it's only 7 characters and ends with /// a \0. InvalidName = 6, /// Attempted to unregister a service that was not previously registered. ServiceNotRegistered = 7, /// Process SACs do not allow accessing or hosting this service. PermissionDenied = 8, /// The provided SACs are too big. ServiceAccessControlTooBig = 9, } } impl From<SmError> for Error { fn from(error: SmError) -> Self { Error::Sm(error, Backtrace::new()) } } enum_with_val! { /// AHCI driver errors. #[derive(PartialEq, Eq, Clone, Copy)] pub struct AhciError(u32) { /// Passed argument were found to be illegal. InvalidArg = 1, /// Passed buffer for DMA is too physically scattered. This can only happen for read/writes /// of 1985 sectors or more. BufferTooScattered = 2, /// The hardware reported an error. IoError = 3, } } impl From<AhciError> for Error { fn from(error: AhciError) -> Self { Error::Ahci(error, Backtrace::new()) } } enum_with_val! { /// Time errors. #[derive(PartialEq, Eq, Clone, Copy)] pub struct TimeError(u32) { /// The given calendar timestamp couldn't be computed. TimeNotFound = 200, /// Signed overflow/underflow happened. Overflow = 201, /// The given input value was out of the timezone rule range. OutOfRange = 902, /// Something when wrong during timezone conversion. TimeZoneConversionFailed = 903, /// The requested timezone wasn't found TimeZoneNotFound = 989, } } impl From<TimeError> for Error { fn from(error: TimeError) -> Self { Error::Time(error, Backtrace::new()) } } enum_with_val! { /// Loader errors. #[derive(PartialEq, Eq, Clone, Copy)] pub struct LoaderError(u32) { /// KACs are invalid. InvalidKacs = 4, /// Invalid path read. InvalidPath = 6, /// Tried to launch a title that does not exist. ProgramNotFound = 8, /// The ELF is corrupted. InvalidElf = 9, } } impl From<LoaderError> for Error { fn from(error: LoaderError) -> Self { Error::Loader(error, Backtrace::new()) } } enum_with_val! { /// PM Errors. #[derive(PartialEq, Eq, Clone, Copy)] pub struct PmError(u32) { /// Pid not found PidNotFound = 1, } } impl From<PmError> for Error { fn from(error: PmError) -> Self { Error::Pm(error, Backtrace::new()) } } enum_with_val! { /// HID driver errors. #[derive(PartialEq, Eq, Clone, Copy)] pub struct HidError(u32) { /// The keyboard was idle and no new data can be provided. NoKeyboardStateUpdate = 999, } } impl From<HidError> for Error { fn from(error: HidError) -> Self { Error::Hid(error, Backtrace::new()) } } enum_with_val! { /// Vi driver errors. #[derive(PartialEq, Eq, Clone, Copy)] pub struct ViError(u32) { /// The given string is not UTF-8. InvalidUtf8 = 1, } } impl From<ViError> for Error { fn from(error: ViError) -> Self { Error::Vi(error, Backtrace::new()) } } enum_with_val! { /// Twili Pipe errors. #[derive(PartialEq, Eq, Clone, Copy)] pub struct TwiliError(u32) { /// Attempted to read on the write-side, or write on the read-side, of /// a pipe. OperationUnsupported = 1 } } impl From<TwiliError> for Error { fn from(error: TwiliError) -> Self { Error::Twili(error, Backtrace::new()) } }