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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
// src/error.rs
//! 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::*;
use std::io;
/// Test case for converting an `io::Error` into `ServerError::Io`.
#[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 case for creating a `ServerError::Custom` from a string slice.
#[test]
fn test_custom_error_creation() {
let custom_error: ServerError = "Unexpected error".into();
assert!(matches!(custom_error, ServerError::Custom(_)));
}
/// Test case for verifying the error messages of different `ServerError` variants.
#[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 case for creating a `ServerError::InvalidRequest` using the `invalid_request` method.
#[test]
fn test_invalid_request_creation() {
let invalid_request =
ServerError::invalid_request("Bad request");
assert!(matches!(
invalid_request,
ServerError::InvalidRequest(_)
));
assert_eq!(
invalid_request.to_string(),
"Invalid request: Bad request"
);
}
/// Test case for creating a `ServerError::NotFound` using the `not_found` method.
#[test]
fn test_not_found_creation() {
let not_found = ServerError::not_found("/nonexistent.html");
assert!(matches!(not_found, ServerError::NotFound(_)));
assert_eq!(
not_found.to_string(),
"File not found: /nonexistent.html"
);
}
/// Test case for creating a `ServerError::Forbidden` using the `forbidden` method.
#[test]
fn test_forbidden_creation() {
let forbidden = ServerError::forbidden("Access denied");
assert!(matches!(forbidden, ServerError::Forbidden(_)));
assert_eq!(forbidden.to_string(), "Forbidden: Access denied");
}
/// Test case for verifying the `ServerError::Custom` variant and its error message.
#[test]
fn test_custom_error_message() {
let custom_error =
ServerError::Custom("Custom error occurred".to_string());
assert_eq!(
custom_error.to_string(),
"Custom error: Custom error occurred"
);
}
/// Test case for checking `ServerError::from` for string conversion.
#[test]
fn test_custom_error_from_str() {
let custom_error: ServerError = "Some custom error".into();
assert!(matches!(custom_error, ServerError::Custom(_)));
assert_eq!(
custom_error.to_string(),
"Custom error: Some custom error"
);
}
/// Test case for converting `io::Error` using a different error kind to `ServerError::Io`.
#[test]
fn test_io_error_conversion_other_kind() {
let io_error = io::Error::new(
io::ErrorKind::PermissionDenied,
"permission denied",
);
let server_error = ServerError::from(io_error);
assert!(matches!(server_error, ServerError::Io(_)));
assert_eq!(
server_error.to_string(),
"I/O error: permission denied"
);
}
/// Test case for verifying if `ServerError::InvalidRequest` carries the correct error message.
#[test]
fn test_invalid_request_message() {
let error_message = "Invalid HTTP version";
let invalid_request =
ServerError::InvalidRequest(error_message.to_string());
assert_eq!(
invalid_request.to_string(),
format!("Invalid request: {}", error_message)
);
}
/// Test case for verifying if `ServerError::NotFound` carries the correct file path.
#[test]
fn test_not_found_message() {
let file_path = "missing.html";
let not_found = ServerError::NotFound(file_path.to_string());
assert_eq!(
not_found.to_string(),
format!("File not found: {}", file_path)
);
}
/// Test case for verifying if `ServerError::Forbidden` carries the correct message.
#[test]
fn test_forbidden_message() {
let forbidden_message = "Access denied to private resource";
let forbidden =
ServerError::Forbidden(forbidden_message.to_string());
assert_eq!(
forbidden.to_string(),
format!("Forbidden: {}", forbidden_message)
);
}
/// Test case for `ServerError::Io` with a generic IO error to ensure correct propagation.
#[test]
fn test_io_error_generic() {
let io_error =
io::Error::new(io::ErrorKind::Other, "generic I/O error");
let server_error = ServerError::from(io_error);
assert!(matches!(server_error, ServerError::Io(_)));
assert_eq!(
server_error.to_string(),
"I/O error: generic I/O error"
);
}
}