Skip to main content

mini_serve/
error.rs

1use std::fmt;
2
3/// An HTTP error returned by a handler.
4///
5/// Contains an HTTP status code and a message. The message is sanitized
6/// before being sent to clients for 5xx errors (only generic "internal server error"
7/// is shown), but passed through for 4xx errors.
8#[derive(Debug)]
9pub struct ServeError {
10	/// HTTP status code (e.g., 400, 500).
11	pub code:    u16,
12	/// Error message (shown to client for 4xx, sanitized for 5xx).
13	pub message: String,
14}
15
16impl ServeError {
17	/// Create a new error with a status code and message.
18	pub fn new(code: u16, message: impl Into<String>) -> Self {
19		ServeError { code, message: message.into() }
20	}
21}
22
23impl fmt::Display for ServeError {
24	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25		write!(f, "{}: {}", self.code, self.message)
26	}
27}
28
29impl std::error::Error for ServeError {}