Skip to content

Commit

Permalink
Update error tests
Browse files Browse the repository at this point in the history
  • Loading branch information
khvzak committed Oct 16, 2024
1 parent 735aa22 commit 084a85c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 25 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ jobs:
- name: Build ${{ matrix.lua }} vendored
run: |
cargo build --features "${{ matrix.lua }},vendored"
cargo build --features "${{ matrix.lua }},vendored,async,serialize,macros"
cargo build --features "${{ matrix.lua }},vendored,async,serialize,macros,send"
cargo build --features "${{ matrix.lua }},vendored,async,serialize,macros,anyhow"
cargo build --features "${{ matrix.lua }},vendored,async,serialize,macros,anyhow,send"
shell: bash
- name: Build ${{ matrix.lua }} pkg-config
if: ${{ matrix.os == 'ubuntu-latest' }}
Expand Down Expand Up @@ -123,8 +123,8 @@ jobs:
- name: Run ${{ matrix.lua }} tests
run: |
cargo test --features "${{ matrix.lua }},vendored"
cargo test --features "${{ matrix.lua }},vendored,async,serialize,macros"
cargo test --features "${{ matrix.lua }},vendored,async,serialize,macros,send"
cargo test --features "${{ matrix.lua }},vendored,async,serialize,macros,anyhow"
cargo test --features "${{ matrix.lua }},vendored,async,serialize,macros,anyhow,send"
shell: bash
- name: Run compile tests (macos lua54)
if: ${{ matrix.os == 'macos-latest' && matrix.lua == 'lua54' }}
Expand Down Expand Up @@ -281,4 +281,4 @@ jobs:
- uses: giraffate/clippy-action@v1
with:
reporter: 'github-pr-review'
clippy_flags: --features "${{ matrix.lua }},vendored,async,send,serialize,macros"
clippy_flags: --features "${{ matrix.lua }},vendored,async,send,serialize,macros,anyhow"
22 changes: 13 additions & 9 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ impl fmt::Display for Error {
Error::DeserializeError(err) => {
write!(fmt, "deserialize error: {err}")
},
Error::ExternalError(err) => write!(fmt, "{err}"),
Error::ExternalError(err) => err.fmt(fmt),
Error::WithContext { context, cause } => {
writeln!(fmt, "{context}")?;
write!(fmt, "{cause}")
Expand All @@ -328,10 +328,7 @@ impl StdError for Error {
// returns nothing.
Error::CallbackError { .. } => None,
Error::ExternalError(err) => err.source(),
Error::WithContext { cause, .. } => match cause.as_ref() {
Error::ExternalError(err) => err.source(),
_ => None,
},
Error::WithContext { cause, .. } => Self::source(&cause),
_ => None,
}
}
Expand All @@ -357,10 +354,7 @@ impl Error {
{
match self {
Error::ExternalError(err) => err.downcast_ref(),
Error::WithContext { cause, .. } => match cause.as_ref() {
Error::ExternalError(err) => err.downcast_ref(),
_ => None,
},
Error::WithContext { cause, .. } => Self::downcast_ref(&cause),
_ => None,
}
}
Expand All @@ -373,6 +367,16 @@ impl Error {
}
}

/// Returns the parent of this error.
#[doc(hidden)]
pub fn parent(&self) -> Option<&Error> {
match self {
Error::CallbackError { cause, .. } => Some(cause.as_ref()),
Error::WithContext { cause, .. } => Some(cause.as_ref()),
_ => None,
}
}

pub(crate) fn bad_self_argument(to: &str, cause: Error) -> Self {
Error::BadArgument {
to: Some(to.to_string()),
Expand Down
2 changes: 1 addition & 1 deletion tarpaulin.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[lua54_coverage]
features = "lua54,vendored,async,send,serialize,macros"
features = "lua54,vendored,async,send,serialize,macros,anyhow"

[lua54_with_memory_limit_coverage]
features = "lua54,vendored,async,send,serialize,macros"
Expand Down
24 changes: 14 additions & 10 deletions tests/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::io;
use std::error::Error as _;
use std::{fmt, io};

use mlua::{Error, ErrorContext, Lua, Result};

Expand Down Expand Up @@ -27,7 +28,6 @@ fn test_error_context() -> Result<()> {
.load("local _, err = pcall(func2); return tostring(err)")
.eval::<String>()?;
assert!(msg2.contains("failed to find global"));
println!("{msg2}");
assert!(msg2.contains("error converting Lua nil to String"));

// Rewrite context message and test `downcast_ref`
Expand All @@ -36,13 +36,12 @@ fn test_error_context() -> Result<()> {
.context("some context")
.context("some new context")
})?;
let res = func3.call::<()>(()).err().unwrap();
let Error::CallbackError { cause, .. } = &res else {
unreachable!()
};
assert!(!res.to_string().contains("some context"));
assert!(res.to_string().contains("some new context"));
assert!(cause.downcast_ref::<io::Error>().is_some());
let err = func3.call::<()>(()).unwrap_err();
let err = err.parent().unwrap();
assert!(!err.to_string().contains("some context"));
assert!(err.to_string().contains("some new context"));
assert!(err.downcast_ref::<io::Error>().is_some());
assert!(err.downcast_ref::<fmt::Error>().is_none());

Ok(())
}
Expand All @@ -59,7 +58,7 @@ fn test_error_chain() -> Result<()> {
let err = Error::external(io::Error::new(io::ErrorKind::Other, "other")).context("io error");
Err::<(), _>(err)
})?;
let err = func.call::<()>(()).err().unwrap();
let err = func.call::<()>(()).unwrap_err();
assert_eq!(err.chain().count(), 3);
for (i, err) in err.chain().enumerate() {
match i {
Expand All @@ -70,6 +69,11 @@ fn test_error_chain() -> Result<()> {
}
}

let err = err.parent().unwrap();
assert!(err.source().is_none()); // The source is included to the `Display` output
assert!(err.to_string().contains("io error"));
assert!(err.to_string().contains("other"));

Ok(())
}

Expand Down

0 comments on commit 084a85c

Please sign in to comment.