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>; 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 // Internal C++ bridging logging routines
pub(crate) trait CxxResultExt<T> { pub(crate) trait CxxResultExt<T> {
fn log_cxx(self) -> LoggedResult<T>; fn log_cxx(self) -> LoggedResult<T>;
@ -223,6 +227,24 @@ trait LogImpl<T> {
) -> LoggedResult<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 { impl<T, R: LogImpl<T>> CxxResultExt<T> for R {
fn log_cxx(self) -> LoggedResult<T> { fn log_cxx(self) -> LoggedResult<T> {
self.log_impl(LogLevel::ErrorCxx, None) 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::libc::{O_CLOEXEC, O_RDONLY};
use base::{ use base::{
clone_attr, cstr, debug, libc::mkstemp, Directory, FsPath, FsPathBuf, LibcReturn, LoggedError, clone_attr, cstr, debug, libc::mkstemp, Directory, FsPath, FsPathBuf, LibcReturn, LoggedResult,
LoggedResult, MappedFile, Utf8CStr, Utf8CStrBufArr, WalkResult, MappedFile, ResultNoLog, Utf8CStr, Utf8CStrBufArr, WalkResult,
}; };
use crate::ffi::{prop_cb_exec, PropCb}; use crate::ffi::{prop_cb_exec, PropCb};
@ -68,11 +68,8 @@ impl PropExt for PersistentProperties {
} }
fn find(&mut self, name: &Utf8CStr) -> LoggedResult<&mut PersistentPropertyRecord> { 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]) 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) let path = FsPathBuf::new(&mut buf)
.join(PERSIST_PROP_DIR!()) .join(PERSIST_PROP_DIR!())
.join(name); .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); debug!("resetprop: read prop from [{}]", path);
let mut s = String::new(); let mut s = String::new();
file.read_to_string(&mut s)?; 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); debug!("resetprop: write prop to [{}]", tmp);
tmp.rename_to(path)? tmp.rename_to(path)?
} else { } else {
path.remove().no_log()?;
debug!("resetprop: unlink [{}]", path); debug!("resetprop: unlink [{}]", path);
path.remove()?;
} }
Ok(()) 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)? }; let name = unsafe { Utf8CStr::from_ptr(name)? };
if check_proto() { if check_proto() {
let mut props = proto_read_props()?; let mut props = proto_read_props()?;
if let Ok(PersistentPropertyRecord { let prop = props.find(name)?;
if let PersistentPropertyRecord {
name: Some(ref mut n), name: Some(ref mut n),
value: Some(ref mut v), value: Some(ref mut v),
}) = props.find(name) } = prop
{ {
prop_cb.exec(Utf8CStr::from_string(n), Utf8CStr::from_string(v)); 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<()> { fn inner(mut prop_cb: Pin<&mut PropCb>) -> LoggedResult<()> {
if check_proto() { if check_proto() {
let mut props = proto_read_props()?; let mut props = proto_read_props()?;
props.iter_mut().for_each(|p| { props.iter_mut().for_each(|prop| {
if let PersistentPropertyRecord { if let PersistentPropertyRecord {
name: Some(ref mut n), name: Some(ref mut n),
value: Some(ref mut v), value: Some(ref mut v),
} = p } = prop
{ {
prop_cb.exec(Utf8CStr::from_string(n), Utf8CStr::from_string(v)); 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)? }; let name = unsafe { Utf8CStr::from_ptr(name)? };
if check_proto() { if check_proto() {
let mut props = proto_read_props()?; 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); props.remove(idx);
proto_write_props(&props) proto_write_props(&props)
} else {
Err(LoggedError::default())
}
} else { } else {
file_set_prop(name, None) file_set_prop(name, None)
} }