From 9dd86c508b29477fcfde076bf76fd5b458bd970e Mon Sep 17 00:00:00 2001 From: Christopher Hock Date: Tue, 23 Apr 2024 11:10:16 +0200 Subject: [PATCH] Add validation exception (#71) * add validation exception --- src/publisher/publisher_exceptions.rs | 29 ++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/publisher/publisher_exceptions.rs b/src/publisher/publisher_exceptions.rs index 1f1b9be..793012b 100644 --- a/src/publisher/publisher_exceptions.rs +++ b/src/publisher/publisher_exceptions.rs @@ -22,9 +22,7 @@ use reqwest::Error as ReqwestError; -use std::{error::Error, fmt}; - -use crate::configuration; +use std::{error::Error, fmt, process}; #[derive(Debug)] pub enum NetBoxApiError { @@ -48,3 +46,28 @@ impl From for NetBoxApiError { NetBoxApiError::Reqwest(err) } } + +/// Error to be thrown when the validation of an API request payload fails. +/// +/// # Members +/// +/// * message: `String` - The error message containing the reason for the failure. +pub struct PayloadValidationError { + message: String, +} + +/// Implements the `Format` trait for `PayloadValidationError` for easy printing. +impl fmt::Display for PayloadValidationError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "[FATAL] PayloadValidationError: {}", self.message) + } +} + +/// Implement `abort` function to exit the program in an orderly manner, printing the error message +/// in the process. +impl PayloadValidationError { + pub fn abort(&self, exit_code: i32) -> ! { + println!("{} (Error code: {})", self, exit_code); + process::exit(exit_code); + } +}