docker_volume/
errors.rs

1use axum::http::StatusCode;
2use axum::response::{IntoResponse, Response};
3use std::io;
4use thiserror::Error;
5
6pub type VolumeResponse<T> = Result<T, VolumeError>;
7
8#[derive(Debug, Error)]
9#[non_exhaustive]
10pub enum VolumeError {
11    #[error("Something went wrong")]
12    Unknown,
13    #[error("Provided volume wasn't found")]
14    NotFound,
15    #[error("{0} is not a valid option")]
16    NoOption(String),
17    #[error("Invalid options: {0}")]
18    InvalidOptions(String),
19    #[error("Failed to create a location for a volume: {0}")]
20    FailedIO(io::Error),
21    #[error("Failed to mount the volume due to an internal error: {0}")]
22    FailedMount(String),
23}
24
25impl IntoResponse for VolumeError {
26    fn into_response(self) -> Response {
27        let message = self.to_string();
28        match self {
29            VolumeError::Unknown => (StatusCode::INTERNAL_SERVER_ERROR, message),
30            VolumeError::NotFound => (StatusCode::NOT_FOUND, message),
31            VolumeError::NoOption(_) => (StatusCode::BAD_REQUEST, message),
32            VolumeError::InvalidOptions(_) => (StatusCode::BAD_REQUEST, message),
33            VolumeError::FailedIO(_) => (StatusCode::INTERNAL_SERVER_ERROR, message),
34            VolumeError::FailedMount(_) => (StatusCode::INTERNAL_SERVER_ERROR, message),
35        }
36        .into_response()
37    }
38}