Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistently use the error message from a callback error. #1043

Merged
merged 1 commit into from
May 21, 2024
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
13 changes: 12 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use libc::c_int;
use std::env::JoinPathsError;
use std::error;
use std::ffi::{CStr, NulError};
use std::ffi::{CStr, CString, NulError};
use std::fmt;
use std::str;

Expand Down Expand Up @@ -350,6 +350,17 @@ impl Error {
pub fn message(&self) -> &str {
&self.message
}

/// A low-level convenience to call [`raw::git_error_set_str`] with the
/// information from this error.
///
/// Returns the [`Error::raw_code`] value of this error, which is often
/// needed from a C callback.
pub(crate) unsafe fn raw_set_git_error(&self) -> raw::git_error_code {
let s = CString::new(self.message()).unwrap();
raw::git_error_set_str(self.class() as c_int, s.as_ptr());
self.raw_code()
}
}

impl error::Error for Error {}
Expand Down
20 changes: 5 additions & 15 deletions src/remote_callbacks.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use libc::{c_char, c_int, c_uint, c_void, size_t};
use std::ffi::{CStr, CString};
use std::ffi::CStr;
use std::mem;
use std::ptr;
use std::slice;
Expand Down Expand Up @@ -312,11 +312,7 @@ extern "C" fn credentials_cb(

let cred_type = CredentialType::from_bits_truncate(allowed_types as u32);

callback(url, username_from_url, cred_type).map_err(|e| {
let s = CString::new(e.to_string()).unwrap();
raw::git_error_set_str(e.class() as c_int, s.as_ptr());
e.raw_code() as c_int
})
callback(url, username_from_url, cred_type).map_err(|e| e.raw_set_git_error())
});
match ok {
Some(Ok(cred)) => {
Expand Down Expand Up @@ -415,13 +411,7 @@ extern "C" fn certificate_check_cb(
match ok {
Some(Ok(CertificateCheckStatus::CertificateOk)) => 0,
Some(Ok(CertificateCheckStatus::CertificatePassthrough)) => raw::GIT_PASSTHROUGH as c_int,
Some(Err(e)) => {
let s = CString::new(e.message()).unwrap();
unsafe {
raw::git_error_set_str(e.class() as c_int, s.as_ptr());
}
e.raw_code() as c_int
}
Some(Err(e)) => unsafe { e.raw_set_git_error() },
None => {
// Panic. The *should* get resumed by some future call to check().
-1
Expand All @@ -448,7 +438,7 @@ extern "C" fn push_update_reference_cb(
};
match callback(refname, status) {
Ok(()) => 0,
Err(e) => e.raw_code(),
Err(e) => e.raw_set_git_error(),
}
})
.unwrap_or(-1)
Expand Down Expand Up @@ -511,7 +501,7 @@ extern "C" fn push_negotiation_cb(
let updates = slice::from_raw_parts(updates as *mut PushUpdate<'_>, len);
match callback(updates) {
Ok(()) => 0,
Err(e) => e.raw_code(),
Err(e) => e.raw_set_git_error(),
}
})
.unwrap_or(-1)
Expand Down
10 changes: 1 addition & 9 deletions src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,7 @@ extern "C" fn subtransport_action(
if generate_stream {
let obj = match transport.obj.action(url, action) {
Ok(s) => s,
Err(e) => {
set_err(&e);
return e.raw_code() as c_int;
}
Err(e) => return e.raw_set_git_error(),
};
*stream = mem::transmute(Box::new(RawSmartSubtransportStream {
raw: raw::git_smart_subtransport_stream {
Expand Down Expand Up @@ -363,11 +360,6 @@ unsafe fn set_err_io(e: &io::Error) {
raw::git_error_set_str(raw::GIT_ERROR_NET as c_int, s.as_ptr());
}

unsafe fn set_err(e: &Error) {
let s = CString::new(e.message()).unwrap();
raw::git_error_set_str(e.raw_class() as c_int, s.as_ptr());
}

// callback used by smart transports to free a `SmartSubtransportStream`
// object.
extern "C" fn stream_free(stream: *mut raw::git_smart_subtransport_stream) {
Expand Down