Skip to content

Commit 084a85c

Browse files
committed
Update error tests
1 parent 735aa22 commit 084a85c

File tree

4 files changed

+33
-25
lines changed

4 files changed

+33
-25
lines changed

.github/workflows/main.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ jobs:
2727
- name: Build ${{ matrix.lua }} vendored
2828
run: |
2929
cargo build --features "${{ matrix.lua }},vendored"
30-
cargo build --features "${{ matrix.lua }},vendored,async,serialize,macros"
31-
cargo build --features "${{ matrix.lua }},vendored,async,serialize,macros,send"
30+
cargo build --features "${{ matrix.lua }},vendored,async,serialize,macros,anyhow"
31+
cargo build --features "${{ matrix.lua }},vendored,async,serialize,macros,anyhow,send"
3232
shell: bash
3333
- name: Build ${{ matrix.lua }} pkg-config
3434
if: ${{ matrix.os == 'ubuntu-latest' }}
@@ -123,8 +123,8 @@ jobs:
123123
- name: Run ${{ matrix.lua }} tests
124124
run: |
125125
cargo test --features "${{ matrix.lua }},vendored"
126-
cargo test --features "${{ matrix.lua }},vendored,async,serialize,macros"
127-
cargo test --features "${{ matrix.lua }},vendored,async,serialize,macros,send"
126+
cargo test --features "${{ matrix.lua }},vendored,async,serialize,macros,anyhow"
127+
cargo test --features "${{ matrix.lua }},vendored,async,serialize,macros,anyhow,send"
128128
shell: bash
129129
- name: Run compile tests (macos lua54)
130130
if: ${{ matrix.os == 'macos-latest' && matrix.lua == 'lua54' }}
@@ -281,4 +281,4 @@ jobs:
281281
- uses: giraffate/clippy-action@v1
282282
with:
283283
reporter: 'github-pr-review'
284-
clippy_flags: --features "${{ matrix.lua }},vendored,async,send,serialize,macros"
284+
clippy_flags: --features "${{ matrix.lua }},vendored,async,send,serialize,macros,anyhow"

src/error.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ impl fmt::Display for Error {
309309
Error::DeserializeError(err) => {
310310
write!(fmt, "deserialize error: {err}")
311311
},
312-
Error::ExternalError(err) => write!(fmt, "{err}"),
312+
Error::ExternalError(err) => err.fmt(fmt),
313313
Error::WithContext { context, cause } => {
314314
writeln!(fmt, "{context}")?;
315315
write!(fmt, "{cause}")
@@ -328,10 +328,7 @@ impl StdError for Error {
328328
// returns nothing.
329329
Error::CallbackError { .. } => None,
330330
Error::ExternalError(err) => err.source(),
331-
Error::WithContext { cause, .. } => match cause.as_ref() {
332-
Error::ExternalError(err) => err.source(),
333-
_ => None,
334-
},
331+
Error::WithContext { cause, .. } => Self::source(&cause),
335332
_ => None,
336333
}
337334
}
@@ -357,10 +354,7 @@ impl Error {
357354
{
358355
match self {
359356
Error::ExternalError(err) => err.downcast_ref(),
360-
Error::WithContext { cause, .. } => match cause.as_ref() {
361-
Error::ExternalError(err) => err.downcast_ref(),
362-
_ => None,
363-
},
357+
Error::WithContext { cause, .. } => Self::downcast_ref(&cause),
364358
_ => None,
365359
}
366360
}
@@ -373,6 +367,16 @@ impl Error {
373367
}
374368
}
375369

370+
/// Returns the parent of this error.
371+
#[doc(hidden)]
372+
pub fn parent(&self) -> Option<&Error> {
373+
match self {
374+
Error::CallbackError { cause, .. } => Some(cause.as_ref()),
375+
Error::WithContext { cause, .. } => Some(cause.as_ref()),
376+
_ => None,
377+
}
378+
}
379+
376380
pub(crate) fn bad_self_argument(to: &str, cause: Error) -> Self {
377381
Error::BadArgument {
378382
to: Some(to.to_string()),

tarpaulin.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[lua54_coverage]
2-
features = "lua54,vendored,async,send,serialize,macros"
2+
features = "lua54,vendored,async,send,serialize,macros,anyhow"
33

44
[lua54_with_memory_limit_coverage]
55
features = "lua54,vendored,async,send,serialize,macros"

tests/error.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::io;
1+
use std::error::Error as _;
2+
use std::{fmt, io};
23

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

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

3333
// Rewrite context message and test `downcast_ref`
@@ -36,13 +36,12 @@ fn test_error_context() -> Result<()> {
3636
.context("some context")
3737
.context("some new context")
3838
})?;
39-
let res = func3.call::<()>(()).err().unwrap();
40-
let Error::CallbackError { cause, .. } = &res else {
41-
unreachable!()
42-
};
43-
assert!(!res.to_string().contains("some context"));
44-
assert!(res.to_string().contains("some new context"));
45-
assert!(cause.downcast_ref::<io::Error>().is_some());
39+
let err = func3.call::<()>(()).unwrap_err();
40+
let err = err.parent().unwrap();
41+
assert!(!err.to_string().contains("some context"));
42+
assert!(err.to_string().contains("some new context"));
43+
assert!(err.downcast_ref::<io::Error>().is_some());
44+
assert!(err.downcast_ref::<fmt::Error>().is_none());
4645

4746
Ok(())
4847
}
@@ -59,7 +58,7 @@ fn test_error_chain() -> Result<()> {
5958
let err = Error::external(io::Error::new(io::ErrorKind::Other, "other")).context("io error");
6059
Err::<(), _>(err)
6160
})?;
62-
let err = func.call::<()>(()).err().unwrap();
61+
let err = func.call::<()>(()).unwrap_err();
6362
assert_eq!(err.chain().count(), 3);
6463
for (i, err) in err.chain().enumerate() {
6564
match i {
@@ -70,6 +69,11 @@ fn test_error_chain() -> Result<()> {
7069
}
7170
}
7271

72+
let err = err.parent().unwrap();
73+
assert!(err.source().is_none()); // The source is included to the `Display` output
74+
assert!(err.to_string().contains("io error"));
75+
assert!(err.to_string().contains("other"));
76+
7377
Ok(())
7478
}
7579

0 commit comments

Comments
 (0)