mini_static/error.rs
1use std::fmt;
2
3/// Errors that can occur during static file serving.
4///
5/// This error type is non-exhaustive and may gain new variants in future releases.
6///
7/// # Variants
8///
9/// - `NotFound`: The requested path does not exist.
10/// - `Traversal`: The requested path attempts to escape the server root.
11/// - `Io`: An I/O error occurred (file read, permission denied, etc.).
12/// - `PipelineSetup`: A configured CSS/JS tool's binary is missing from `PATH`,
13/// discovered at server startup before the listener binds.
14#[derive(Debug)]
15#[non_exhaustive]
16pub enum StaticError {
17 /// Requested path does not exist.
18 NotFound(String),
19 /// Path traversal attempt detected.
20 Traversal(String),
21 /// I/O error from the filesystem.
22 Io(std::io::Error),
23 /// A configured build-pipeline tool could not be found or validated at startup.
24 PipelineSetup(String),
25}
26
27impl fmt::Display for StaticError {
28 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29 match self {
30 StaticError::NotFound(path) => write!(f, "not found: {path}"),
31 StaticError::Traversal(path) => write!(f, "path traversal denied: {path}"),
32 StaticError::Io(e) => write!(f, "io error: {e}"),
33 StaticError::PipelineSetup(msg) => write!(f, "pipeline setup failed: {msg}"),
34 }
35 }
36}
37
38impl std::error::Error for StaticError {
39 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
40 match self {
41 StaticError::Io(e) => Some(e),
42 _ => None,
43 }
44 }
45}
46
47impl StaticError {
48 /// Returns a user-safe error message for use in HTTP responses.
49 ///
50 /// Traversal errors return "not found" to avoid leaking information about
51 /// the filesystem structure. I/O errors return "internal server error".
52 pub fn user_message(&self) -> &'static str {
53 match self {
54 StaticError::NotFound(_) | StaticError::Traversal(_) => "not found",
55 StaticError::Io(_) | StaticError::PipelineSetup(_) => "internal server error",
56 }
57 }
58}
59
60impl From<std::io::Error> for StaticError {
61 fn from(e: std::io::Error) -> Self {
62 StaticError::Io(e)
63 }
64}