idlefox/src/error_handling.rs

81 lines
3.3 KiB
Rust

//! This module provides a set of error types that can be used to handle various issues
//! with interacting with the Discord API or handling date-related tasks.
//!
//! The errors in this module are designed to provide detailed information about what
//! went wrong, making it easier to diagnose and fix problems.
#![allow(dead_code)]
use chrono::Month;
use log::SetLoggerError;
use poise::serenity_prelude;
use thiserror::Error;
// // Since the errors themselves are very verbose, this seemed like complete overkill
// #[allow(clippy::missing_docs_in_private_items)]
/// Global error struct for idlefox
///
/// This enum represents various errors that can occur when interacting with most of the section of the program
#[derive(Error, Debug)]
pub enum IdleError {
/// Error indicating a critical failure in the Discord API interaction
///
/// This error is raised when an unrecoverable issue occurs while interfacing with the Discord API.
/// It may be recoverable by retrying the request or seeking assistance from the API provider.
#[error("An unrecoverable error occurred during Discord API interaction")]
GenericApiFailure,
/// Error indicating an invalid date format or an invalid date (dec 32, feb 30, etc..)
///
/// This error is raised when a user-provided date string cannot be parsed into a valid (date, month).
#[error("An error happened while tring to make sense of the date ({0:?},{1:?})")]
InvalidDate(u32, Option<Month>),
/// Generic error indicating an unexpected issue during Discord API call processing
///
/// This error is raised when an unforeseen issue occurs while handling a Discord API request.
/// It may be recoverable by re-trying the request or seeking assistance from the API provider.
#[error("An unexpected error occurred during Discord API processing: {0}")]
ApiProcessingError(#[from] serenity_prelude::Error),
/// Error indicating an invalid date string
///
/// This error is raised when a user-provided date string cannot be parsed into a valid [crate::date::date_parse::DatePoint].
/// Notifying the user and asking for manual correction will likely be required
#[error("An error occured while parsing the date given {0:?}")]
InvalidDateParsing(nom::Err<nom::error::Error<Box<str>>>),
/// Error indicating an issue with logging initialization
///
/// This error is raised when a critical issue occurs during logging initialization.
/// It may indicate that the file reader is read-only, and manual intervention will be required to resolve the problem.
#[error("An error occurred while initializing the logger: {0}")]
LoggerInitFailure(#[from] SetLoggerError),
}
impl From<nom::Err<nom::error::Error<&str>>> for IdleError {
fn from(err: nom::Err<nom::error::Error<&str>>) -> Self {
Self::InvalidDateParsing(err.map_input(|input| input.into()))
}
}
#[cfg(test)]
mod tests {
use super::*;
fn into_error() -> Result<(), IdleError> {
Err(nom::Err::Error(nom::error::Error::new(
"",
nom::error::ErrorKind::Tag,
)))?
}
#[test]
fn test_nom_error() {
match into_error() {
Err(IdleError::InvalidDateParsing(_d)) => {}
_ => panic!("The convertion from a nom error into a IdleError failed"),
};
}
}