http_handle/error.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
//! Error types for the Http Handle.
//!
//! This module defines the various error types that can occur during the operation
//! of the Http Handle. It provides a centralized place for error handling and
//! propagation throughout the application.
//!
//! The main type exposed by this module is the `ServerError` enum, which
//! encompasses all possible error conditions the server might encounter.
use std::io;
use thiserror::Error;
/// Represents the different types of errors that can occur in the server.
///
/// This enum defines various errors that can be encountered during the server's operation,
/// such as I/O errors, invalid requests, file not found, and forbidden access.
///
/// # Examples
///
/// Creating an I/O error:
///
/// ```
/// use std::io::{Error, ErrorKind};
/// use http_handle::ServerError;
///
/// let io_error = Error::new(ErrorKind::NotFound, "file not found");
/// let server_error = ServerError::from(io_error);
/// assert!(matches!(server_error, ServerError::Io(_)));
/// ```
///
/// Creating an invalid request error:
///
/// ```
/// use http_handle::ServerError;
///
/// let invalid_request = ServerError::InvalidRequest("Missing HTTP method".to_string());
/// assert!(matches!(invalid_request, ServerError::InvalidRequest(_)));
/// ```
#[derive(Error, Debug)]
pub enum ServerError {
/// An I/O error occurred.
#[error("I/O error: {0}")]
Io(#[from] io::Error),
/// The request received by the server was invalid or malformed.
#[error("Invalid request: {0}")]
InvalidRequest(String),
/// The requested file was not found on the server.
#[error("File not found: {0}")]
NotFound(String),
/// Access to the requested resource is forbidden.
#[error("Forbidden: {0}")]
Forbidden(String),
/// A custom error type for unexpected scenarios.
#[error("Custom error: {0}")]
Custom(String),
}
impl ServerError {
/// Creates a new `InvalidRequest` error with the given message.
///
/// # Arguments
///
/// * `message` - A string slice that holds the error message.
///
/// # Returns
///
/// A `ServerError::InvalidRequest` variant.
///
/// # Examples
///
/// ```
/// use http_handle::ServerError;
///
/// let error = ServerError::invalid_request("Missing HTTP version");
/// assert!(matches!(error, ServerError::InvalidRequest(_)));
/// ```
pub fn invalid_request<T: Into<String>>(message: T) -> Self {
ServerError::InvalidRequest(message.into())
}
/// Creates a new `NotFound` error with the given path.
///
/// # Arguments
///
/// * `path` - A string slice that holds the path of the not found resource.
///
/// # Returns
///
/// A `ServerError::NotFound` variant.
///
/// # Examples
///
/// ```
/// use http_handle::ServerError;
///
/// let error = ServerError::not_found("/nonexistent.html");
/// assert!(matches!(error, ServerError::NotFound(_)));
/// ```
pub fn not_found<T: Into<String>>(path: T) -> Self {
ServerError::NotFound(path.into())
}
/// Creates a new `Forbidden` error with the given message.
///
/// # Arguments
///
/// * `message` - A string slice that holds the error message.
///
/// # Returns
///
/// A `ServerError::Forbidden` variant.
///
/// # Examples
///
/// ```
/// use http_handle::ServerError;
///
/// let error = ServerError::forbidden("Access denied to sensitive file");
/// assert!(matches!(error, ServerError::Forbidden(_)));
/// ```
pub fn forbidden<T: Into<String>>(message: T) -> Self {
ServerError::Forbidden(message.into())
}
}
impl From<&str> for ServerError {
/// Converts a string slice into a `ServerError::Custom` variant.
///
/// This implementation allows for easy creation of custom errors from string literals.
///
/// # Arguments
///
/// * `error` - A string slice that holds the error message.
///
/// # Returns
///
/// A `ServerError::Custom` variant.
///
/// # Examples
///
/// ```
/// use http_handle::ServerError;
///
/// let error: ServerError = "Unexpected error".into();
/// assert!(matches!(error, ServerError::Custom(_)));
/// ```
fn from(error: &str) -> Self {
ServerError::Custom(error.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_io_error_conversion() {
let io_error =
io::Error::new(io::ErrorKind::NotFound, "file not found");
let server_error = ServerError::from(io_error);
assert!(matches!(server_error, ServerError::Io(_)));
}
#[test]
fn test_custom_error_creation() {
let custom_error = ServerError::from("Unexpected error");
assert!(matches!(custom_error, ServerError::Custom(_)));
}
#[test]
fn test_error_messages() {
let not_found = ServerError::not_found("index.html");
assert_eq!(not_found.to_string(), "File not found: index.html");
let forbidden = ServerError::forbidden("Access denied");
assert_eq!(forbidden.to_string(), "Forbidden: Access denied");
let invalid_request =
ServerError::invalid_request("Missing HTTP method");
assert_eq!(
invalid_request.to_string(),
"Invalid request: Missing HTTP method"
);
}
#[test]
fn test_error_creation_methods() {
let invalid_request =
ServerError::invalid_request("Bad request");
assert!(matches!(
invalid_request,
ServerError::InvalidRequest(_)
));
let not_found = ServerError::not_found("/nonexistent.html");
assert!(matches!(not_found, ServerError::NotFound(_)));
let forbidden = ServerError::forbidden("Access denied");
assert!(matches!(forbidden, ServerError::Forbidden(_)));
}
}