use crate::runtime::RuntimeMutationError;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::Json;
use nomoreide_daemon_client::protocol::{ErrorEnvelope, MutationErrorEnvelope};
pub(crate) async fn unmatched() -> Response {
(
StatusCode::NOT_FOUND,
[(
axum::http::header::CONTENT_TYPE,
axum::http::HeaderValue::from_static("text/html; charset=utf-8"),
)],
"Not found",
)
.into_response()
}
pub(crate) async fn method_not_allowed() -> Response {
error(StatusCode::METHOD_NOT_ALLOWED, "Method not allowed")
}
pub(crate) fn config_failure(reason: &anyhow::Error) -> Response {
let status = if nomoreide_core::config::is_config_validation_error(reason) {
StatusCode::BAD_REQUEST
} else {
StatusCode::INTERNAL_SERVER_ERROR
};
error(status, &reason.to_string())
}
pub(crate) fn error(status: StatusCode, message: &str) -> Response {
(
status,
Json(ErrorEnvelope {
ok: false,
error: message.to_string(),
}),
)
.into_response()
}
pub(crate) fn service_mutation_error(failure: RuntimeMutationError) -> Response {
if let RuntimeMutationError::PortConflict { message, conflict } = failure {
return (
StatusCode::CONFLICT,
Json(MutationErrorEnvelope {
ok: false,
error: message,
conflict: Some(*conflict),
}),
)
.into_response();
}
mutation_error(failure)
}
pub(crate) fn mutation_error(failure: RuntimeMutationError) -> Response {
error(
StatusCode::INTERNAL_SERVER_ERROR,
&mutation_message(failure),
)
}
pub(crate) fn mutation_message(failure: RuntimeMutationError) -> String {
match failure {
RuntimeMutationError::ServiceNotFound(name) => {
format!("Service \"{name}\" is not registered.")
}
RuntimeMutationError::BundleNotFound(name) => {
format!("Bundle \"{name}\" is not registered.")
}
RuntimeMutationError::UnsupportedServiceKind => {
"Only local, ssh, and docker-compose services are supported by the native daemon."
.to_string()
}
RuntimeMutationError::DaemonDraining => {
"The daemon is draining process mutations.".to_string()
}
RuntimeMutationError::DaemonCleanupFailed => {
"The daemon previously failed to clean up its services; new starts are disabled."
.to_string()
}
RuntimeMutationError::ConfigLoadFailed => "Failed to load NoMoreIDE config.".to_string(),
RuntimeMutationError::ServiceStartFailed => {
"Failed to start the registered service.".to_string()
}
RuntimeMutationError::DependencyCycle(message) => message,
RuntimeMutationError::CleanupFailed => "Failed to confirm service cleanup.".to_string(),
RuntimeMutationError::PortConflict { message, .. } => message,
}
}