//! 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; use crate::ids::EventIdType; /// 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), /// 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>>), /// 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), /// Indicates an event couldn't be launched /// /// Raised when there's an issue launching an event usually indicates something went wrong with bookeeping. /// Note that while a HUID is given, it may no longer exist in memory and might only be found in logs. #[error("An event couldn't be launched (Id: {0})")] EventLaunch(EventIdType), /// Error indicating multiple events couldn't be pruned /// /// Raised when one or more events cannot be removed from the ongoing events list. /// This usually indicates a problem with managing and updating the list of ongoing events. /// Note that while a HUID is given, it may no longer exist in memory and might only be found in logs. #[error("One or more events couldn't be removed from the ongoing events list (Id(s): {0:?})")] EventPrune(Vec), /// Error indicating an event could not be deleted /// /// This error is raised when attempting to delete an event fails, /// either due to a problem with bookkeping or because the specified HUID /// does not correspond to any existing events anymore. #[error("An event couldn't be deleted (Id: {0})")] DeleteEvent(EventIdType), /// Error indicating that an ongoing event launch couldn't be performed /// /// This error was likely raised as a launch operation for an event was already underway. /// Launching multiple times for the same event may lead to inconsistent states or data corruption. #[error("Couldn't launch event (Id: {0})")] LaunchingEvent(EventIdType), /// Error indicating a mutex was poisoned or couldn't be acquired /// /// This is likely due to a thread being terminated but could also arise from a deadlock or a subtle race condition #[error("An error happened while trying to acquire a mutex")] MutexError, /// Represents an error while acquiring a Discord group /// /// Raised when there is an error attempting to acquire the discord group #[error("An error occured while trying to acquire the discord group: {0}")] AcquiringDiscordGroup(String), /// Indicates an error while acquiring Discord guilds /// /// This may happen when a function was called outside of the context of a server (likely a DM to the bot). #[error("An error occured while trying to acquire discord guilds")] AcquiringDiscordGuild, /// Indicates an error occuring while trying to convert from a DiscordContainer into a ParsedVote /// /// This error will happen when an incorrect string is sent to a command invocation #[error("An error occured while trying to parse a Discord command")] DiscordContainerParse, /// Indicate a backup write couldn't be done, usually means the file descriptor is busy or permission are incorrect #[error("An error occured while trying to write file {0} {1}")] BackupWriteError(String, std::io::Error), /// Indicate a backup read couldn't be done, usually means the file descriptor is busy or permission are incorrect #[error("An error occured while trying to read file {0} {1}")] BackupReadError(String, std::io::Error), /// Indicate a value couldn't be serialized #[error("An error occured while trying to serialize data {0}")] SerdeError(serde_json::Error), } impl From>> for IdleError { fn from(err: nom::Err>) -> 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"), }; } }