Browse Source

Merge pull request #87 from rust3ds/feature/better-error-display

Use the human-readable error strings in the Error Display impl
pull/89/head
Mark Drobnak 2 years ago committed by GitHub
parent
commit
2bc31ca9f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      .github/workflows/ci.yml
  2. 20
      ctru-rs/src/error.rs

2
.github/workflows/ci.yml

@ -19,7 +19,7 @@ jobs:
matrix: matrix:
toolchain: toolchain:
# Run against a "known good" nightly # Run against a "known good" nightly
- nightly-2022-07-18 - nightly-2023-01-13
# Check for breakage on latest nightly # Check for breakage on latest nightly
- nightly - nightly
# But if latest nightly fails, allow the workflow to continue # But if latest nightly fails, allow the workflow to continue

20
ctru-rs/src/error.rs

@ -101,13 +101,17 @@ impl fmt::Debug for Error {
} }
} }
// TODO: Expand libctru result code into human-readable error message. These should be useful:
// https://www.3dbrew.org/wiki/Error_codes
// https://github.com/devkitPro/libctru/blob/master/libctru/include/3ds/result.h
impl fmt::Display for Error { impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
&Self::Os(err) => write!(f, "libctru result code: 0x{err:08X}"), &Self::Os(err) => write!(
f,
"libctru result code 0x{err:08X}: [{} {}] {}: {}",
result_code_level_str(err),
result_code_module_str(err),
result_code_summary_str(err),
result_code_description_str(err)
),
Self::Libc(err) => write!(f, "{err}"), Self::Libc(err) => write!(f, "{err}"),
Self::ServiceAlreadyActive => write!(f, "Service already active"), Self::ServiceAlreadyActive => write!(f, "Service already active"),
Self::OutputAlreadyRedirected => { Self::OutputAlreadyRedirected => {
@ -135,7 +139,7 @@ fn result_code_level_str(result: ctru_sys::Result) -> Cow<'static, str> {
RL_PERMANENT => "permanent", RL_PERMANENT => "permanent",
RL_TEMPORARY => "temporary", RL_TEMPORARY => "temporary",
RL_STATUS => "status", RL_STATUS => "status",
code => return Cow::Owned(format!("(unknown: {code:#x})")), code => return Cow::Owned(format!("(unknown level: {code:#x})")),
}) })
} }
@ -160,7 +164,7 @@ fn result_code_summary_str(result: ctru_sys::Result) -> Cow<'static, str> {
RS_STATUSCHANGED => "status_changed", RS_STATUSCHANGED => "status_changed",
RS_INTERNAL => "internal", RS_INTERNAL => "internal",
RS_INVALIDRESVAL => "invalid_res_val", RS_INVALIDRESVAL => "invalid_res_val",
code => return Cow::Owned(format!("(unknown: {code:#x})")), code => return Cow::Owned(format!("(unknown summary: {code:#x})")),
}) })
} }
@ -200,7 +204,7 @@ fn result_code_description_str(result: ctru_sys::Result) -> Cow<'static, str> {
RD_NOT_AUTHORIZED => "not_authorized", RD_NOT_AUTHORIZED => "not_authorized",
RD_TOO_LARGE => "too_large", RD_TOO_LARGE => "too_large",
RD_INVALID_SELECTION => "invalid_selection", RD_INVALID_SELECTION => "invalid_selection",
code => return Cow::Owned(format!("(unknown: {code:#x})")), code => return Cow::Owned(format!("(unknown description: {code:#x})")),
}) })
} }
@ -317,6 +321,6 @@ fn result_code_module_str(result: ctru_sys::Result) -> Cow<'static, str> {
RM_NFP => "nfp", RM_NFP => "nfp",
RM_APPLICATION => "application", RM_APPLICATION => "application",
RM_INVALIDRESVAL => "invalid_res_val", RM_INVALIDRESVAL => "invalid_res_val",
code => return Cow::Owned(format!("(unknown: {code:#x})")), code => return Cow::Owned(format!("(unknown module: {code:#x})")),
}) })
} }

Loading…
Cancel
Save