cleanup some error callsites

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-15 04:19:43 +00:00
parent b903b46d16
commit d67f19a55d
9 changed files with 96 additions and 144 deletions

View file

@ -31,6 +31,7 @@
//! Err(Database(error!("problem with db: {msg}")))` logs the error at the
//! callsite and then returns the error with the same string. Caller has the
//! option of replacing `error!` with `debug_error!`.
#[macro_export]
macro_rules! Err {
($($args:tt)*) => {

View file

@ -78,7 +78,7 @@ pub enum Error {
// conduwuit
#[error("Arithmetic operation failed: {0}")]
Arithmetic(&'static str),
Arithmetic(Cow<'static, str>),
#[error("There was a problem with the '{0}' directive in your configuration: {1}")]
Config(&'static str, Cow<'static, str>),
#[error("{0}")]

View file

@ -2,14 +2,14 @@ use std::{cmp, time::Duration};
pub use checked_ops::checked_ops;
use crate::{Error, Result};
use crate::{Err, Error, Result};
/// Checked arithmetic expression. Returns a Result<R, Error::Arithmetic>
#[macro_export]
macro_rules! checked {
($($input:tt)*) => {
$crate::utils::math::checked_ops!($($input)*)
.ok_or_else(|| $crate::Error::Arithmetic("operation overflowed or result invalid"))
.ok_or_else(|| $crate::err!(Arithmetic("operation overflowed or result invalid")))
}
}
@ -21,7 +21,7 @@ macro_rules! validated {
($($input:tt)*) => {
//#[allow(clippy::arithmetic_side_effects)] {
//Some($($input)*)
// .ok_or_else(|| $crate::Error::Arithmetic("this error should never been seen"))
// .ok_or_else(|| $crate::err!(Arithmetic("this error should never been seen")))
//}
//NOTE: remove me when stmt_expr_attributes is stable
@ -57,7 +57,7 @@ pub fn continue_exponential_backoff(min: Duration, max: Duration, elapsed: Durat
#[allow(clippy::as_conversions)]
pub fn usize_from_f64(val: f64) -> Result<usize, Error> {
if val < 0.0 {
return Err(Error::Arithmetic("Converting negative float to unsigned integer"));
return Err!(Arithmetic("Converting negative float to unsigned integer"));
}
//SAFETY: <https://doc.rust-lang.org/std/primitive.f64.html#method.to_int_unchecked>