Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gitoxide-core/src/repository/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ fn resolve_revspec(
// We extract the error from the tree to learn the name, and treat it as file.
let not_found = err
.iter_frames()
.find_map(|f| f.downcast::<gix::refs::file::find::existing::Error>());
.find_map(|f| f.error().downcast_ref::<gix::refs::file::find::existing::Error>());
if let Some(gix::refs::file::find::existing::Error::NotFound { name }) = not_found {
let root = repo.workdir().map(ToOwned::to_owned);
let name = gix::path::os_string_into_bstring(name.into())?;
Expand Down
6 changes: 3 additions & 3 deletions gix-error/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ impl Error {
/// Return the error that is most likely the root cause, based on heuristics.
/// Note that if there is nothing but this error, i.e. no source or children, this error is returned.
pub fn probable_cause(&self) -> &(dyn std::error::Error + 'static) {
let root = self.inner.as_frame();
let root = self.inner.frame();
root.probable_cause().unwrap_or(root).error()
}

/// Return an iterator over all errors in the tree in breadth-first order, starting with this one.
pub fn iter_frames(&self) -> impl Iterator<Item = &exn::Frame> + '_ {
self.inner.as_frame().iter()
self.inner.frame().iter()
}
}

Expand All @@ -22,7 +22,7 @@ pub(super) enum Inner {
}

impl Inner {
fn as_frame(&self) -> &exn::Frame {
fn frame(&self) -> &exn::Frame {
match self {
Inner::ExnAsError(f) | Inner::Exn(f) => f,
}
Expand Down
11 changes: 5 additions & 6 deletions gix-error/src/exn/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

use crate::exn::Exn;
use std::error::Error;

/// A trait bound of the supported error type of [`Exn`].
pub trait ErrorExt: std::error::Error + Send + Sync + 'static {
Expand All @@ -37,7 +36,7 @@ pub trait ErrorExt: std::error::Error + Send + Sync + 'static {

/// Raise this error as a new exception, with `sources` as causes.
#[track_caller]
fn raise_iter<T, I>(self, sources: I) -> Exn<Self>
fn raise_all<T, I>(self, sources: I) -> Exn<Self>
where
Self: Sized,
T: std::error::Error + Send + Sync + 'static,
Expand Down Expand Up @@ -123,7 +122,7 @@ pub trait ResultExt {
F: FnOnce() -> A;
}

impl<T, E> ResultExt for std::result::Result<T, E>
impl<T, E> ResultExt for Result<T, E>
where
E: std::error::Error + Send + Sync + 'static,
{
Expand Down Expand Up @@ -153,14 +152,14 @@ where
#[track_caller]
fn or_raise_erased<A, F>(self, err: F) -> Result<Self::Success, Exn>
where
A: Error + Send + Sync + 'static,
A: std::error::Error + Send + Sync + 'static,
F: FnOnce() -> A,
{
self.or_raise(err).map_err(Exn::erased)
}
}

impl<T, E> ResultExt for std::result::Result<T, Exn<E>>
impl<T, E> ResultExt for Result<T, Exn<E>>
where
E: std::error::Error + Send + Sync + 'static,
{
Expand Down Expand Up @@ -190,7 +189,7 @@ where
#[track_caller]
fn or_raise_erased<A, F>(self, err: F) -> Result<Self::Success, Exn>
where
A: Error + Send + Sync + 'static,
A: std::error::Error + Send + Sync + 'static,
F: FnOnce() -> A,
{
self.or_raise(err).map_err(Exn::erased)
Expand Down
7 changes: 1 addition & 6 deletions gix-error/src/exn/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl<E: Error + Send + Sync + 'static> Exn<E> {
/// Iterate over all frames and find one that downcasts into error of type `T`.
/// Note that the search includes this instance as well.
pub fn downcast_any_ref<T: Error + 'static>(&self) -> Option<&T> {
self.iter().find_map(|e| e.downcast())
self.iter().find_map(|e| e.error.downcast_ref())
}
}

Expand Down Expand Up @@ -307,11 +307,6 @@ impl Frame {
&*self.error
}

/// Try to downcast this error into the exact type T, or return `None`
pub fn downcast<T: Error + 'static>(&self) -> Option<&T> {
self.error.downcast_ref()
}
Comment on lines -310 to -313
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc comment should be "Try to downcast this error as a ref to the exact type T, or return None"

But since it can be written as a fluent call f.error().downcast_ref::<T>(), I tend not to add this method to keep the API less.


/// Return the source code location where this exception frame was created.
pub fn location(&self) -> &'static Location<'static> {
self.location
Expand Down
10 changes: 5 additions & 5 deletions gix-error/tests/error/exn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ fn raise_chain() {
}

#[test]
fn raise_iter() {
let e = Error("Top").raise_iter(
(1..5).map(|idx| message!("E{}", idx).raise_iter((0..idx).map(|sidx| message!("E{}-{}", idx, sidx)))),
fn raise_all() {
let e = Error("Top").raise_all(
(1..5).map(|idx| message!("E{}", idx).raise_all((0..idx).map(|sidx| message!("E{}-{}", idx, sidx)))),
);
insta::assert_debug_snapshot!(e, @r"
Top
Expand Down Expand Up @@ -257,7 +257,7 @@ fn raise_iter() {
|
└─ Message("SE2")
"#);
let _this_should_compile = Error("Top-untyped").raise_iter((1..5).map(|idx| message!("E{}", idx).raise_erased()));
let _this_should_compile = Error("Top-untyped").raise_all((1..5).map(|idx| message!("E{}", idx).raise_erased()));

assert_eq!(
e.into_error().probable_cause().to_string(),
Expand Down Expand Up @@ -381,7 +381,7 @@ fn error_tree() {
]
"#);

let new_e = Error("E-New").raise_iter(err.drain_children());
let new_e = Error("E-New").raise_all(err.drain_children());
insta::assert_debug_snapshot!(new_e, @r"
E-New
|
Expand Down
2 changes: 1 addition & 1 deletion gix-revision/src/spec/parse/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ where
}
})
})
.ok_or_else(|| Error::new_with_input("couldn't parse revision", input).raise_iter(errors))?;
.ok_or_else(|| Error::new_with_input("couldn't parse revision", input).raise_all(errors))?;
}

input = {
Expand Down
Loading