Skip to content

Commit 829393a

Browse files
authored
refactor!: ErrorExt::raise_iter to raise_all + remove Frame::downcast (#2385)
Be more compatible to `exn`. Signed-off-by: tison <wander4096@gmail.com>
1 parent 5707b26 commit 829393a

File tree

6 files changed

+16
-22
lines changed

6 files changed

+16
-22
lines changed

gitoxide-core/src/repository/diff.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ fn resolve_revspec(
130130
// We extract the error from the tree to learn the name, and treat it as file.
131131
let not_found = err
132132
.iter_frames()
133-
.find_map(|f| f.downcast::<gix::refs::file::find::existing::Error>());
133+
.find_map(|f| f.error().downcast_ref::<gix::refs::file::find::existing::Error>());
134134
if let Some(gix::refs::file::find::existing::Error::NotFound { name }) = not_found {
135135
let root = repo.workdir().map(ToOwned::to_owned);
136136
let name = gix::path::os_string_into_bstring(name.into())?;

gix-error/src/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ impl Error {
66
/// Return the error that is most likely the root cause, based on heuristics.
77
/// Note that if there is nothing but this error, i.e. no source or children, this error is returned.
88
pub fn probable_cause(&self) -> &(dyn std::error::Error + 'static) {
9-
let root = self.inner.as_frame();
9+
let root = self.inner.frame();
1010
root.probable_cause().unwrap_or(root).error()
1111
}
1212

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

@@ -22,7 +22,7 @@ pub(super) enum Inner {
2222
}
2323

2424
impl Inner {
25-
fn as_frame(&self) -> &exn::Frame {
25+
fn frame(&self) -> &exn::Frame {
2626
match self {
2727
Inner::ExnAsError(f) | Inner::Exn(f) => f,
2828
}

gix-error/src/exn/ext.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// limitations under the License.
1414

1515
use crate::exn::Exn;
16-
use std::error::Error;
1716

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

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

126-
impl<T, E> ResultExt for std::result::Result<T, E>
125+
impl<T, E> ResultExt for Result<T, E>
127126
where
128127
E: std::error::Error + Send + Sync + 'static,
129128
{
@@ -153,14 +152,14 @@ where
153152
#[track_caller]
154153
fn or_raise_erased<A, F>(self, err: F) -> Result<Self::Success, Exn>
155154
where
156-
A: Error + Send + Sync + 'static,
155+
A: std::error::Error + Send + Sync + 'static,
157156
F: FnOnce() -> A,
158157
{
159158
self.or_raise(err).map_err(Exn::erased)
160159
}
161160
}
162161

163-
impl<T, E> ResultExt for std::result::Result<T, Exn<E>>
162+
impl<T, E> ResultExt for Result<T, Exn<E>>
164163
where
165164
E: std::error::Error + Send + Sync + 'static,
166165
{
@@ -190,7 +189,7 @@ where
190189
#[track_caller]
191190
fn or_raise_erased<A, F>(self, err: F) -> Result<Self::Success, Exn>
192191
where
193-
A: Error + Send + Sync + 'static,
192+
A: std::error::Error + Send + Sync + 'static,
194193
F: FnOnce() -> A,
195194
{
196195
self.or_raise(err).map_err(Exn::erased)

gix-error/src/exn/impls.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ impl<E: Error + Send + Sync + 'static> Exn<E> {
192192
/// Iterate over all frames and find one that downcasts into error of type `T`.
193193
/// Note that the search includes this instance as well.
194194
pub fn downcast_any_ref<T: Error + 'static>(&self) -> Option<&T> {
195-
self.iter().find_map(|e| e.downcast())
195+
self.iter().find_map(|e| e.error.downcast_ref())
196196
}
197197
}
198198

@@ -307,11 +307,6 @@ impl Frame {
307307
&*self.error
308308
}
309309

310-
/// Try to downcast this error into the exact type T, or return `None`
311-
pub fn downcast<T: Error + 'static>(&self) -> Option<&T> {
312-
self.error.downcast_ref()
313-
}
314-
315310
/// Return the source code location where this exception frame was created.
316311
pub fn location(&self) -> &'static Location<'static> {
317312
self.location

gix-error/tests/error/exn.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ fn raise_chain() {
118118
}
119119

120120
#[test]
121-
fn raise_iter() {
122-
let e = Error("Top").raise_iter(
123-
(1..5).map(|idx| message!("E{}", idx).raise_iter((0..idx).map(|sidx| message!("E{}-{}", idx, sidx)))),
121+
fn raise_all() {
122+
let e = Error("Top").raise_all(
123+
(1..5).map(|idx| message!("E{}", idx).raise_all((0..idx).map(|sidx| message!("E{}-{}", idx, sidx)))),
124124
);
125125
insta::assert_debug_snapshot!(e, @r"
126126
Top
@@ -257,7 +257,7 @@ fn raise_iter() {
257257
|
258258
└─ Message("SE2")
259259
"#);
260-
let _this_should_compile = Error("Top-untyped").raise_iter((1..5).map(|idx| message!("E{}", idx).raise_erased()));
260+
let _this_should_compile = Error("Top-untyped").raise_all((1..5).map(|idx| message!("E{}", idx).raise_erased()));
261261

262262
assert_eq!(
263263
e.into_error().probable_cause().to_string(),
@@ -381,7 +381,7 @@ fn error_tree() {
381381
]
382382
"#);
383383

384-
let new_e = Error("E-New").raise_iter(err.drain_children());
384+
let new_e = Error("E-New").raise_all(err.drain_children());
385385
insta::assert_debug_snapshot!(new_e, @r"
386386
E-New
387387
|

gix-revision/src/spec/parse/function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ where
480480
}
481481
})
482482
})
483-
.ok_or_else(|| Error::new_with_input("couldn't parse revision", input).raise_iter(errors))?;
483+
.ok_or_else(|| Error::new_with_input("couldn't parse revision", input).raise_all(errors))?;
484484
}
485485

486486
input = {

0 commit comments

Comments
 (0)