Skip to main content

modo/error/
convert.rs

1//! `From` conversions for common standard-library and third-party error types into [`Error`].
2
3use http::StatusCode;
4
5use super::Error;
6
7/// Converts a [`std::io::Error`] into a `500 Internal Server Error`.
8impl From<std::io::Error> for Error {
9    fn from(err: std::io::Error) -> Self {
10        Error::with_source(StatusCode::INTERNAL_SERVER_ERROR, "IO error", err)
11    }
12}
13
14/// Converts a [`serde_json::Error`] into a `400 Bad Request`.
15impl From<serde_json::Error> for Error {
16    fn from(err: serde_json::Error) -> Self {
17        Error::with_source(StatusCode::BAD_REQUEST, "JSON error", err)
18    }
19}
20
21/// Converts a [`serde_yaml_ng::Error`] into a `500 Internal Server Error`.
22impl From<serde_yaml_ng::Error> for Error {
23    fn from(err: serde_yaml_ng::Error) -> Self {
24        Error::with_source(StatusCode::INTERNAL_SERVER_ERROR, "YAML error", err)
25    }
26}