magic-nix-cache/nix-actions-cache/src/error.rs

42 lines
965 B
Rust
Raw Normal View History

2023-05-08 09:48:11 +00:00
//! Errors.
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};
use thiserror::Error;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Error, Debug)]
pub enum Error {
2023-05-08 16:05:43 +00:00
#[error("GitHub API error: {0}")]
Api(#[from] gha_cache::api::Error),
2023-05-08 09:48:11 +00:00
#[error("Not Found")]
NotFound,
#[error("Bad Request")]
BadRequest,
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("Failed to upload paths")]
FailedToUpload,
2023-05-08 09:48:11 +00:00
}
impl IntoResponse for Error {
fn into_response(self) -> Response {
let code = match &self {
// HACK: HTTP 418 makes Nix throw a visible error but not retry
Self::Api(_) => StatusCode::IM_A_TEAPOT,
2023-05-08 09:48:11 +00:00
Self::NotFound => StatusCode::NOT_FOUND,
Self::BadRequest => StatusCode::BAD_REQUEST,
_ => StatusCode::INTERNAL_SERVER_ERROR,
2023-05-08 09:48:11 +00:00
};
(code, format!("{}", self)).into_response()
}
}