-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: extract panic-utils crate and fix occasional incorrect downcasting
Extract `panic-utils` crate and consistently use it for downcasting panic payloads to strings across the codebase. The implementation is a bit more efficient than what was used previously and avoids unnecessary allocations, and also fixes a few bugs related to only one of the two possible cases handled when downcasting panic payloads. The most egregious was the case in `sql-query-connector` where we tried downcasting a panic from the database driver to `String` and then unwrapped the result, leading to possible double panics if the original panic was of the form `panic!("message")` and not `panic!("message: {details}")` and thus the panic payload was a `&static str` and not a `String`.
- Loading branch information
Showing
14 changed files
with
55 additions
and
32 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "panic-utils" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
|
||
[lints] | ||
workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use std::{any::Any, borrow::Cow}; | ||
|
||
/// Downcasts a boxed [`Any`] from a panic payload to a string. | ||
pub fn downcast_box_to_string(object: Box<dyn Any>) -> Option<Cow<'static, str>> { | ||
object | ||
.downcast::<&'static str>() | ||
.map(|s| Cow::Borrowed(*s)) | ||
.or_else(|object| object.downcast::<String>().map(|s| Cow::Owned(*s))) | ||
.ok() | ||
} | ||
|
||
/// Downcasts a reference to [`Any`] from panic info hook to a string. | ||
pub fn downcast_ref_to_string(object: &dyn Any) -> Option<&str> { | ||
object | ||
.downcast_ref::<&str>() | ||
.copied() | ||
.or_else(|| object.downcast_ref::<String>().map(|s| s.as_str())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters