Skip to content

Commit

Permalink
feat: Lift io::Errors to response::Error::UploadPack(…)… (#200)
Browse files Browse the repository at this point in the history
…for easier matching and figuring out what actually happened.
For now, this is left entirely to users of the API, but one day
I imagine that some parsing could be done on gitoxide's side as well.
  • Loading branch information
Byron committed Sep 19, 2021
1 parent 637d12c commit f293b63
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 2 deletions.
2 changes: 1 addition & 1 deletion etc/check-package-size.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ echo "in root: gitoxide CLI"
(enter git-packetline && indent cargo diet -n --package-size-limit 15KB)
(enter git-repository && indent cargo diet -n --package-size-limit 50KB)
(enter git-transport && indent cargo diet -n --package-size-limit 30KB)
(enter gitoxide-core && indent cargo diet -n --package-size-limit 20KB)
(enter gitoxide-core && indent cargo diet -n --package-size-limit 25KB)
21 changes: 20 additions & 1 deletion git-protocol/src/fetch/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ quick_error! {
pub enum Error {
Io(err: io::Error) {
display("Failed to read from line reader")
from()
source(err)
}
UploadPack(err: git_transport::packetline::read::Error) {
display("Upload pack reported an error")
source(err)
}
Transport(err: client::Error) {
Expand All @@ -33,6 +36,22 @@ quick_error! {
}
}

impl From<std::io::Error> for Error {
fn from(err: io::Error) -> Self {
if err.kind() == io::ErrorKind::Other {
match err.into_inner() {
Some(err) => match err.downcast::<git_transport::packetline::read::Error>() {
Ok(err) => Error::UploadPack(*err),
Err(err) => Error::Io(io::Error::new(io::ErrorKind::Other, err)),
},
None => Error::Io(io::ErrorKind::Other.into()),
}
} else {
Error::Io(err)
}
}
}

/// An 'ACK' line received from the server.
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
Expand Down
16 changes: 16 additions & 0 deletions git-protocol/tests/fetch/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,22 @@ mod v2 {
Ok(())
}

#[maybe_async::test(feature = "blocking-client", async(feature = "async-client", async_std::test))]
async fn fetch_with_err_response() {
let mut provider = mock_reader("v2/fetch-err-line.response");
provider.fail_on_err_lines(true);
let mut sidebands = provider.as_read_without_sidebands();
match fetch::Response::from_line_reader(Protocol::V2, &mut sidebands).await {
Ok(_) => panic!("need error response"),
Err(err) => match err {
fetch::response::Error::UploadPack(err) => {
assert_eq!(err.message, "segmentation fault\n")
}
err => panic!("we expect upload pack errors, got {:#?}", err),
},
}
}

#[maybe_async::test(feature = "blocking-client", async(feature = "async-client", async_std::test))]
async fn fetch_acks_and_pack() -> crate::Result {
let mut provider = mock_reader("v2/fetch.response");
Expand Down
1 change: 1 addition & 0 deletions git-protocol/tests/fixtures/v2/fetch-err-line.response
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
001bERR segmentation fault
1 change: 1 addition & 0 deletions git-repository/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
//! * [`interrupt`]
//! * [`protocol`]
//! * [`transport`][protocol::transport]
//! * [`packetline`][protocol::transport::packetline]
//!
#![deny(missing_docs, unsafe_code, rust_2018_idioms)]

Expand Down
2 changes: 2 additions & 0 deletions git-transport/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#![forbid(unsafe_code)]
#![deny(rust_2018_idioms, missing_docs)]

pub use git_packetline as packetline;

/// The version of the way client and server communicate.
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
Expand Down

0 comments on commit f293b63

Please sign in to comment.