Files
pruefwerk/compliance-agent/src/error.rs
sharang 1df32d5e1a
CI / Check (push) Skipped
CI / Agent integration (push) Skipped
CI / Detect Changes (push) Successful in 3s
CI / Deploy Docs (push) Skipped
CI / Deploy MCP (push) Successful in 2m52s
CI / Deploy Dashboard (push) Successful in 5m31s
CI / Deploy Agent (push) Successful in 7m28s
feat(metering): a plan can allow no extra targets (entry plan) (#1079)
2026-11-28 22:09:18 +00:00

125 lines
3.9 KiB
Rust

use compliance_core::models::RunErrorCode;
use compliance_core::CoreError;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum AgentError {
#[error(transparent)]
Core(#[from] CoreError),
#[error("Database error: {0}")]
Database(#[from] mongodb::error::Error),
#[error("Git error: {0}")]
Git(#[from] git2::Error),
#[error("HTTP error: {0}")]
Http(#[from] reqwest::Error),
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Scheduler error: {0}")]
Scheduler(String),
#[error("Configuration error: {0}")]
Config(String),
#[error(transparent)]
Secret(#[from] crate::secrets::SecretError),
#[error("Dynamic-execution error: {0}")]
Exec(#[from] werkbank_exec::ExecError),
/// An uploaded archive is password-protected and no password is set.
#[error("the archive is password-protected; set its password on the target")]
ArchivePasswordRequired,
/// The password set for an uploaded archive does not open it.
#[error("the archive password set on the target does not open it")]
ArchivePasswordWrong,
/// A counted Prüfung the plan does not allow now (t72): not an error of
/// the scan, which ends without scanning.
#[error("not run: {0:?}")]
NotAdmitted(compliance_core::models::Refusal),
/// A new target the plan has no room for: as many targets are active
/// as it allows (the entry plan's `extra_targets: 0`).
#[error("the plan allows {0} active targets")]
TargetLimit(u32),
#[error("{0}")]
Other(String),
}
impl AgentError {
/// What a scan run that stopped on this error records: a stable code and
/// a plain English sentence, never the error's own text, which can hold
/// server paths and internal details (it goes to the log instead).
pub fn run_failure(&self) -> (RunErrorCode, &'static str) {
match self {
Self::Git(_) => (
RunErrorCode::RepositoryUnreachable,
"the repository could not be cloned or fetched",
),
Self::ArchivePasswordRequired => (
RunErrorCode::ArchivePasswordRequired,
"the archive is password-protected and no password is set",
),
Self::ArchivePasswordWrong => (
RunErrorCode::ArchivePasswordWrong,
"the stored password does not open the archive",
),
Self::Core(_)
| Self::Database(_)
| Self::Http(_)
| Self::Json(_)
| Self::Io(_)
| Self::Scheduler(_)
| Self::Config(_)
| Self::Secret(_)
| Self::Exec(_)
| Self::NotAdmitted(_)
| Self::TargetLimit(_)
| Self::Other(_) => (
RunErrorCode::Internal,
"the scan stopped because of an internal error",
),
}
}
}
impl From<AgentError> for axum::http::StatusCode {
fn from(err: AgentError) -> Self {
match err {
AgentError::Core(CoreError::NotFound(_)) => axum::http::StatusCode::NOT_FOUND,
AgentError::TargetLimit(_) => axum::http::StatusCode::CONFLICT,
_ => axum::http::StatusCode::INTERNAL_SERVER_ERROR,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_failed_run_records_no_raw_error() {
let io = AgentError::Io(std::io::Error::new(
std::io::ErrorKind::NotFound,
"/data/compliance-scanner/artifacts/work/65f0/a1: no such file",
));
let (code, message) = io.run_failure();
assert_eq!(code, RunErrorCode::Internal);
assert!(!message.contains('/'), "{message}");
assert_eq!(
AgentError::ArchivePasswordWrong.run_failure().0,
RunErrorCode::ArchivePasswordWrong
);
}
}