Cleanup messy error messages

This commit is contained in:
topjohnwu 2023-10-12 18:54:09 -07:00
parent b0b04690d5
commit 4b83c1e76c
2 changed files with 36 additions and 19 deletions

View File

@ -207,6 +207,10 @@ pub trait ResultExt<T> {
fn log_with_msg<F: FnOnce(Formatter) -> fmt::Result>(self, f: F) -> LoggedResult<T>;
}
pub trait ResultNoLog<T> {
fn no_log(self) -> LoggedResult<T>;
}
// Internal C++ bridging logging routines
pub(crate) trait CxxResultExt<T> {
fn log_cxx(self) -> LoggedResult<T>;
@ -223,6 +227,24 @@ trait LogImpl<T> {
) -> LoggedResult<T>;
}
impl<T, E> ResultNoLog<T> for Result<T, E> {
fn no_log(self) -> LoggedResult<T> {
match self {
Ok(v) => Ok(v),
Err(_) => Err(LoggedError::default()),
}
}
}
impl<T> ResultNoLog<T> for Option<T> {
fn no_log(self) -> LoggedResult<T> {
match self {
Some(v) => Ok(v),
None => Err(LoggedError::default()),
}
}
}
impl<T, R: LogImpl<T>> CxxResultExt<T> for R {
fn log_cxx(self) -> LoggedResult<T> {
self.log_impl(LogLevel::ErrorCxx, None)

View File

@ -12,8 +12,8 @@ use quick_protobuf::{BytesReader, MessageRead, MessageWrite, Writer};
use base::libc::{O_CLOEXEC, O_RDONLY};
use base::{
clone_attr, cstr, debug, libc::mkstemp, Directory, FsPath, FsPathBuf, LibcReturn, LoggedError,
LoggedResult, MappedFile, Utf8CStr, Utf8CStrBufArr, WalkResult,
clone_attr, cstr, debug, libc::mkstemp, Directory, FsPath, FsPathBuf, LibcReturn, LoggedResult,
MappedFile, ResultNoLog, Utf8CStr, Utf8CStrBufArr, WalkResult,
};
use crate::ffi::{prop_cb_exec, PropCb};
@ -68,11 +68,8 @@ impl PropExt for PersistentProperties {
}
fn find(&mut self, name: &Utf8CStr) -> LoggedResult<&mut PersistentPropertyRecord> {
if let Ok(idx) = self.find_index(name) {
let idx = self.find_index(name).no_log()?;
Ok(&mut self[idx])
} else {
Err(LoggedError::default())
}
}
}
@ -85,7 +82,7 @@ fn file_get_prop(name: &Utf8CStr) -> LoggedResult<String> {
let path = FsPathBuf::new(&mut buf)
.join(PERSIST_PROP_DIR!())
.join(name);
let mut file = path.open(O_RDONLY | O_CLOEXEC)?;
let mut file = path.open(O_RDONLY | O_CLOEXEC).no_log()?;
debug!("resetprop: read prop from [{}]", path);
let mut s = String::new();
file.read_to_string(&mut s)?;
@ -112,8 +109,8 @@ fn file_set_prop(name: &Utf8CStr, value: Option<&Utf8CStr>) -> LoggedResult<()>
debug!("resetprop: write prop to [{}]", tmp);
tmp.rename_to(path)?
} else {
path.remove().no_log()?;
debug!("resetprop: unlink [{}]", path);
path.remove()?;
}
Ok(())
}
@ -150,10 +147,11 @@ pub unsafe fn persist_get_prop(name: *const c_char, prop_cb: Pin<&mut PropCb>) {
let name = unsafe { Utf8CStr::from_ptr(name)? };
if check_proto() {
let mut props = proto_read_props()?;
if let Ok(PersistentPropertyRecord {
let prop = props.find(name)?;
if let PersistentPropertyRecord {
name: Some(ref mut n),
value: Some(ref mut v),
}) = props.find(name)
} = prop
{
prop_cb.exec(Utf8CStr::from_string(n), Utf8CStr::from_string(v));
}
@ -171,11 +169,11 @@ pub unsafe fn persist_get_props(prop_cb: Pin<&mut PropCb>) {
fn inner(mut prop_cb: Pin<&mut PropCb>) -> LoggedResult<()> {
if check_proto() {
let mut props = proto_read_props()?;
props.iter_mut().for_each(|p| {
props.iter_mut().for_each(|prop| {
if let PersistentPropertyRecord {
name: Some(ref mut n),
value: Some(ref mut v),
} = p
} = prop
{
prop_cb.exec(Utf8CStr::from_string(n), Utf8CStr::from_string(v));
}
@ -204,12 +202,9 @@ pub unsafe fn persist_delete_prop(name: *const c_char) -> bool {
let name = unsafe { Utf8CStr::from_ptr(name)? };
if check_proto() {
let mut props = proto_read_props()?;
if let Ok(idx) = props.find_index(name) {
let idx = props.find_index(name).no_log()?;
props.remove(idx);
proto_write_props(&props)
} else {
Err(LoggedError::default())
}
} else {
file_set_prop(name, None)
}