#[cfg(any(feature = "platform", feature = "web-axum"))]
use axum::http::StatusCode;
#[cfg(any(feature = "platform", feature = "web-axum"))]
use axum::response::{IntoResponse, Json, Response};
#[cfg(any(feature = "platform", feature = "web-axum"))]
use chrono::Utc;
#[cfg(any(feature = "platform", feature = "web-axum"))]
use log::error;
use once_cell::sync::Lazy;
use regex::Regex;
use serde::Serialize;
use crate::i18n::{I18nBundle, Locale};
#[derive(Debug, thiserror::Error)]
pub enum CrawlRsError {
#[cfg(any(feature = "platform", feature = "web-axum"))]
#[error("Database error: {0}")]
Database(#[from] dbnexus::sea_orm::DbErr),
#[cfg(not(any(feature = "platform", feature = "web-axum")))]
#[error("Database error: {0}")]
Database(String),
#[error("Network error: {0}")]
Network(String),
#[error("Configuration error: {0}")]
Config(String),
#[error("Validation error: {0}")]
Validation(String),
#[error("Unprocessable entity: {0}")]
Unprocessable(String),
#[error("Not found: {0}")]
NotFound(String),
#[error("Authentication failed: {0}")]
Authentication(String),
#[error("Permission denied: {0}")]
PermissionDenied(String),
#[error("Service unavailable: {0}")]
ServiceUnavailable(String),
#[error("Timeout: {0}")]
Timeout(String),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
#[error("Engine error: {0}")]
Engine(String),
#[error("Cache error: {0}")]
Cache(String),
#[error("Task error: {0}")]
Task(String),
#[error("Rate limit error: {0}")]
RateLimit(String),
#[error("Error: {0}")]
Other(String),
}
impl CrawlRsError {
#[cfg(any(feature = "platform", feature = "web-axum"))]
fn to_api_error(&self) -> Option<sdforge::error::ApiError> {
use sdforge::error::ApiError;
Some(match self {
CrawlRsError::Database(_)
| CrawlRsError::Config(_)
| CrawlRsError::Io(_)
| CrawlRsError::Task(_)
| CrawlRsError::Other(_) => {
ApiError::internal_error("crawlrs internal error", "internal")
}
CrawlRsError::Validation(msg) => ApiError::invalid_input(msg.clone(), None, None),
CrawlRsError::Json(err) => ApiError::invalid_input(err.to_string(), None, None),
CrawlRsError::Unprocessable(msg) => ApiError::validation("request", msg.clone()),
CrawlRsError::NotFound(msg) => ApiError::not_found(msg.clone(), None),
CrawlRsError::Authentication(msg) => ApiError::authentication_failed(msg.clone()),
CrawlRsError::PermissionDenied(msg) => ApiError::access_denied(msg.clone(), None),
CrawlRsError::ServiceUnavailable(_) | CrawlRsError::Cache(_) => {
ApiError::service_unavailable("crawlrs", None)
}
CrawlRsError::RateLimit(_) => ApiError::rate_limit_exceeded(0, 0),
CrawlRsError::Network(_) | CrawlRsError::Engine(_) | CrawlRsError::Timeout(_) => {
return None
}
})
}
#[cfg(any(feature = "platform", feature = "web-axum"))]
pub fn status_code(&self) -> StatusCode {
if let Some(api_error) = self.to_api_error() {
return StatusCode::from_u16(sdforge::error::unified::http_status_for(&api_error))
.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
}
match self {
CrawlRsError::Network(_) | CrawlRsError::Engine(_) => StatusCode::BAD_GATEWAY,
CrawlRsError::Timeout(_) => StatusCode::GATEWAY_TIMEOUT,
other => unreachable!("status_code residual branch hit by {other:?}"),
}
}
pub fn error_code(&self) -> &'static str {
match self {
CrawlRsError::Database(_) => "DATABASE_ERROR",
CrawlRsError::Network(_) => "EXTERNAL_SERVICE_ERROR",
CrawlRsError::Config(_) => "CONFIGURATION_ERROR",
CrawlRsError::Validation(_) => "VALIDATION_ERROR",
CrawlRsError::Unprocessable(_) => "UNPROCESSABLE_ENTITY",
CrawlRsError::NotFound(_) => "NOT_FOUND",
CrawlRsError::Authentication(_) => "AUTHENTICATION_ERROR",
CrawlRsError::PermissionDenied(_) => "FORBIDDEN",
CrawlRsError::ServiceUnavailable(_) => "SERVICE_UNAVAILABLE",
CrawlRsError::Timeout(_) => "TIMEOUT",
CrawlRsError::Io(_) => "IO_ERROR",
CrawlRsError::Json(_) => "JSON_ERROR",
CrawlRsError::Engine(_) => "ENGINE_ERROR",
CrawlRsError::Cache(_) => "CACHE_ERROR",
CrawlRsError::Task(_) => "TASK_ERROR",
CrawlRsError::RateLimit(_) => "RATE_LIMITED",
CrawlRsError::Other(_) => "INTERNAL_ERROR",
}
}
pub fn user_message(&self) -> String {
if let Some((locale, bundle)) = crate::i18n::startup_i18n() {
return self.user_message_locale(locale, bundle);
}
match self {
CrawlRsError::Database(_) => {
"Database operation failed. Please try again later.".to_string()
}
CrawlRsError::Network(_) => {
"External service unavailable. Please try again later.".to_string()
}
CrawlRsError::Config(_) => "Configuration error. Please contact support.".to_string(),
CrawlRsError::Validation(msg) => format!("Validation error: {}", msg),
CrawlRsError::Unprocessable(msg) => format!("Unprocessable entity: {}", msg),
CrawlRsError::NotFound(msg) => format!("Resource not found: {}", msg),
CrawlRsError::Authentication(msg) => format!("Authentication failed: {}", msg),
CrawlRsError::PermissionDenied(msg) => format!("Permission denied: {}", msg),
CrawlRsError::ServiceUnavailable(_) => {
"Service unavailable. Please try again later.".to_string()
}
CrawlRsError::Timeout(_) => "Request timed out. Please try again later.".to_string(),
CrawlRsError::Io(_) => "Internal I/O error. Please try again later.".to_string(),
CrawlRsError::Json(_) => "Invalid JSON format. Please check your request.".to_string(),
CrawlRsError::Engine(e) => sanitize_engine_error(e),
CrawlRsError::Cache(_) => {
"Cache service unavailable. Please try again later.".to_string()
}
CrawlRsError::Task(_) => "Task processing error. Please try again later.".to_string(),
CrawlRsError::RateLimit(_) => {
"Rate limit exceeded. Please slow down your requests.".to_string()
}
CrawlRsError::Other(_) => "Internal server error. Please try again later.".to_string(),
}
}
pub fn user_message_locale(&self, locale: &Locale, bundle: &I18nBundle) -> String {
match self {
CrawlRsError::Database(_) => bundle.translate(locale, "error-database"),
CrawlRsError::Network(_) => bundle.translate(locale, "error-network"),
CrawlRsError::Config(_) => bundle.translate(locale, "error-config"),
CrawlRsError::Validation(msg) => bundle.translate_with_args(
locale,
"error-validation",
&[("message", msg.as_str().to_string())],
),
CrawlRsError::Unprocessable(msg) => bundle.translate_with_args(
locale,
"error-validation",
&[("message", msg.as_str().to_string())],
),
CrawlRsError::NotFound(msg) => bundle.translate_with_args(
locale,
"error-not-found",
&[("resource", msg.as_str().to_string())],
),
CrawlRsError::Authentication(msg) => bundle.translate_with_args(
locale,
"error-auth",
&[("reason", msg.as_str().to_string())],
),
CrawlRsError::PermissionDenied(_) => bundle.translate(locale, "error-permission"),
CrawlRsError::ServiceUnavailable(_) => {
bundle.translate(locale, "error-service-unavailable")
}
CrawlRsError::Timeout(_) => bundle.translate(locale, "error-timeout"),
CrawlRsError::Io(_) => bundle.translate(locale, "error-io"),
CrawlRsError::Json(_) => bundle.translate(locale, "error-json"),
CrawlRsError::Engine(_) => bundle.translate(locale, "error-engine"),
CrawlRsError::Cache(_) => bundle.translate(locale, "error-cache"),
CrawlRsError::Task(_) => bundle.translate(locale, "error-task"),
CrawlRsError::RateLimit(_) => bundle.translate(locale, "error-rate-limit"),
CrawlRsError::Other(_) => bundle.translate(locale, "error-internal"),
}
}
pub fn detailed_message(&self) -> String {
self.to_string()
}
pub fn to_api_error_response(&self) -> ApiErrorResponse {
ApiErrorResponse {
code: self.error_code().to_string(),
message: self.to_string(),
}
}
}
#[derive(Debug, Serialize)]
pub struct ApiErrorResponse {
pub code: String,
pub message: String,
}
pub type CrawlRsResult<T> = Result<T, CrawlRsError>;
impl From<CrawlRsError> for ApiErrorResponse {
fn from(error: CrawlRsError) -> Self {
error.to_api_error_response()
}
}
#[cfg(any(feature = "platform", feature = "web-axum"))]
fn should_show_detailed_errors() -> bool {
let env = std::env::var("CRAWLRS_ENV")
.or_else(|_| std::env::var("APP_ENVIRONMENT"))
.or_else(|_| std::env::var("RUST_ENV"))
.unwrap_or_else(|_| "production".to_string());
env.eq_ignore_ascii_case("development")
|| env.eq_ignore_ascii_case("dev")
|| env.eq_ignore_ascii_case("local")
}
fn sanitize_engine_error(error: &str) -> String {
static RE_FILE_PATH: Lazy<Regex> =
Lazy::new(|| Regex::new(r"/[a-zA-Z0-9_/.-]+\.(rs|toml|env|yml|json|txt)").unwrap());
static RE_LINE_NUMBER: Lazy<Regex> =
Lazy::new(|| Regex::new(r"[a-zA-Z0-9_/.-]+\.rs:\d+").unwrap());
static RE_INTERNAL_IP: Lazy<Regex> = Lazy::new(|| {
Regex::new(
r"\b(10\.\d{1,3}\.\d{1,3}\.\d{1,3}|192\.168\.\d{1,3}\.\d{1,3}|172\.(1[6-9]|2\d|3[0-1])\.\d{1,3}\.\d{1,3})\b"
).unwrap()
});
static RE_INTERNAL_SERVICE: Lazy<Regex> =
Lazy::new(|| Regex::new(r"(localhost|127\.0\.0\.1):\d+").unwrap());
static RE_PUBLIC_IP: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"\b(?:[0-9]{1,2}|1[01][0-9]|12[0-689]|1[3-9][0-9]|2[0-4][0-9]|25[0-5])\.(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){2}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b").unwrap()
});
static RE_URL_PARAMS: Lazy<Regex> = Lazy::new(|| Regex::new(r"\?[^#\s]+").unwrap());
static RE_URL_CREDS: Lazy<Regex> =
Lazy::new(|| Regex::new(r"(https?://)[^:]+:[^@]+@").unwrap());
static RE_DB_PASSWORD: Lazy<Regex> =
Lazy::new(|| Regex::new(r"(postgres|mysql|mongodb|redis)://[^:]+:[^@]+@").unwrap());
static RE_API_KEY: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"(?i)(api[_-]?key|token|secret|password|auth)[=:\s]+[^\s&]+").unwrap()
});
let mut sanitized = error.to_string();
sanitized = RE_FILE_PATH
.replace_all(&sanitized, "[FILE_PATH]")
.to_string();
sanitized = RE_LINE_NUMBER
.replace_all(&sanitized, "[LOCATION]")
.to_string();
sanitized = RE_INTERNAL_IP
.replace_all(&sanitized, "[INTERNAL_IP]")
.to_string();
sanitized = RE_INTERNAL_SERVICE
.replace_all(&sanitized, "$1:[PORT]")
.to_string();
sanitized = RE_PUBLIC_IP.replace_all(&sanitized, "[IP]").to_string();
sanitized = RE_URL_PARAMS
.replace_all(&sanitized, "?[PARAMS_REDACTED]")
.to_string();
sanitized = RE_URL_CREDS
.replace_all(&sanitized, "$1[CREDENTIALS_REDACTED]@")
.to_string();
sanitized = RE_DB_PASSWORD
.replace_all(&sanitized, "$1://[USER]:[PASSWORD]@")
.to_string();
sanitized = RE_API_KEY
.replace_all(&sanitized, "$1=[REDACTED]")
.to_string();
if sanitized.trim().is_empty() || sanitized.len() < 5 {
return "Engine error occurred".to_string();
}
sanitized
}
#[cfg(any(feature = "platform", feature = "web-axum"))]
impl IntoResponse for CrawlRsError {
fn into_response(self) -> Response {
let status = self.status_code();
let error_code = self.error_code();
let detailed_msg = self.detailed_message();
error!(
"Request error occurred error_code={:?} status_code={} error_details={}",
error_code,
status.as_u16(),
detailed_msg
);
let error_response = if should_show_detailed_errors() {
serde_json::json!({
"success": false,
"data": serde_json::Value::Null,
"error": {
"code": error_code,
"message": detailed_msg,
},
"timestamp": Utc::now().to_rfc3339(),
})
} else {
let user_msg = self.user_message();
serde_json::json!({
"success": false,
"data": serde_json::Value::Null,
"error": {
"code": error_code,
"message": user_msg,
},
"timestamp": Utc::now().to_rfc3339(),
})
};
(status, Json(error_response)).into_response()
}
}
impl From<reqwest::Error> for CrawlRsError {
fn from(err: reqwest::Error) -> Self {
CrawlRsError::Network(err.to_string())
}
}
impl From<crate::engines::engine_client::EngineError> for CrawlRsError {
fn from(err: crate::engines::engine_client::EngineError) -> Self {
match err {
crate::engines::engine_client::EngineError::RequestFailed(msg) => {
CrawlRsError::Engine(msg)
}
crate::engines::engine_client::EngineError::Timeout(duration) => {
CrawlRsError::Timeout(format!("Request timed out after {:?}", duration))
}
crate::engines::engine_client::EngineError::NoEnginesAvailable => {
CrawlRsError::Engine("No scraping engines available".to_string())
}
crate::engines::engine_client::EngineError::MemoryConstrained(msg) => {
CrawlRsError::Engine(format!("Memory constrained: {}", msg))
}
crate::engines::engine_client::EngineError::Unsupported(msg) => {
CrawlRsError::Engine(format!("Unsupported operation: {}", msg))
}
crate::engines::engine_client::EngineError::InvalidUrl(msg) => {
CrawlRsError::Validation(msg)
}
crate::engines::engine_client::EngineError::SsrfProtection(msg) => {
CrawlRsError::PermissionDenied(format!("SSRF protection triggered: {}", msg))
}
crate::engines::engine_client::EngineError::BrowserError(msg) => {
CrawlRsError::Engine(format!("Browser error: {}", msg))
}
crate::engines::engine_client::EngineError::AntiBotDetected(msg) => {
CrawlRsError::Engine(format!("Anti-bot detected: {}", msg))
}
crate::engines::engine_client::EngineError::FeatureToggle(msg) => {
CrawlRsError::Engine(format!("Feature toggle: {}", msg))
}
crate::engines::engine_client::EngineError::Expired => {
CrawlRsError::Timeout("Request expired".to_string())
}
crate::engines::engine_client::EngineError::AllEnginesFailed(msg) => {
CrawlRsError::Engine(format!("All engines failed: {}", msg))
}
crate::engines::engine_client::EngineError::Other(msg) => CrawlRsError::Engine(msg),
crate::engines::engine_client::EngineError::Internal(msg) => CrawlRsError::Engine(msg),
crate::engines::engine_client::EngineError::EngineMrtExceeded { engine, mrt } => {
CrawlRsError::Timeout(format!(
"Engine {} exceeded MRT of {:?} (waterfall fallback exhausted)",
engine, mrt
))
}
}
}
}
#[macro_export]
macro_rules! impl_basic_error_conversions {
($error_type:ty, $variant:ident) => {
impl From<String> for $error_type {
fn from(msg: String) -> Self {
Self::$variant(msg)
}
}
impl From<&str> for $error_type {
fn from(msg: &str) -> Self {
Self::$variant(msg.to_string())
}
}
impl From<anyhow::Error> for $error_type {
fn from(err: anyhow::Error) -> Self {
Self::$variant(err.to_string())
}
}
};
}
#[derive(Debug, thiserror::Error)]
pub enum RepositoryError {
#[error("Database error: {0}")]
DatabaseError(String),
#[error("Record not found")]
NotFound,
#[error("Record already exists")]
AlreadyExists,
#[error("Invalid parameter: {0}")]
InvalidParameter(String),
#[error("Internal error: {0}")]
InternalError(String),
}
#[derive(Debug, thiserror::Error)]
pub enum WorkerError {
#[error("Repository error: {0}")]
RepositoryError(String),
#[error("Rate limiting error: {0}")]
RateLimitingError(String),
#[error("Internal error: {0}")]
InternalError(String),
#[error("Domain error: {0}")]
DomainError(String),
#[error("Service error: {0}")]
ServiceError(String),
#[error("Not found: {0}")]
NotFound(String),
}
impl_basic_error_conversions!(WorkerError, InternalError);
impl From<RepositoryError> for WorkerError {
fn from(err: RepositoryError) -> Self {
match err {
RepositoryError::DatabaseError(msg) => WorkerError::RepositoryError(msg),
RepositoryError::NotFound => WorkerError::NotFound("Resource not found".to_string()),
RepositoryError::AlreadyExists => {
WorkerError::RepositoryError("Resource already exists".to_string())
}
RepositoryError::InvalidParameter(msg) => WorkerError::DomainError(msg),
RepositoryError::InternalError(msg) => WorkerError::InternalError(msg),
}
}
}
impl From<RepositoryError> for CrawlRsError {
fn from(err: RepositoryError) -> Self {
match err {
#[cfg(any(feature = "platform", feature = "web-axum"))]
RepositoryError::DatabaseError(msg) => {
CrawlRsError::Database(dbnexus::sea_orm::DbErr::Custom(msg))
}
#[cfg(not(any(feature = "platform", feature = "web-axum")))]
RepositoryError::DatabaseError(msg) => CrawlRsError::Database(msg),
RepositoryError::NotFound => CrawlRsError::NotFound("Resource not found".to_string()),
RepositoryError::AlreadyExists => {
CrawlRsError::Validation("Resource already exists".to_string())
}
RepositoryError::InvalidParameter(msg) => CrawlRsError::Validation(msg),
RepositoryError::InternalError(msg) => CrawlRsError::Other(msg),
}
}
}
impl From<crate::domain::repositories::task_repository::RepositoryError> for CrawlRsError {
fn from(err: crate::domain::repositories::task_repository::RepositoryError) -> Self {
match err {
crate::domain::repositories::task_repository::RepositoryError::Database(db_err) => {
CrawlRsError::Other(db_err.to_string())
}
crate::domain::repositories::task_repository::RepositoryError::NotFound => {
CrawlRsError::NotFound("Resource not found".to_string())
}
crate::domain::repositories::task_repository::RepositoryError::Conflict { detail } => {
CrawlRsError::Validation(detail)
}
}
}
}
impl From<confers::ConfigError> for CrawlRsError {
fn from(err: confers::ConfigError) -> Self {
CrawlRsError::Config(err.to_string())
}
}
impl From<crate::utils::robots::RobotsCheckerError> for CrawlRsError {
fn from(err: crate::utils::robots::RobotsCheckerError) -> Self {
CrawlRsError::Other(err.to_string())
}
}
impl_basic_error_conversions!(CrawlRsError, Other);
pub trait RepositoryResultExt<T> {
fn repo_err(self) -> Result<T, WorkerError>;
}
impl<T, E: std::fmt::Display> RepositoryResultExt<T> for Result<T, E> {
fn repo_err(self) -> Result<T, WorkerError> {
self.map_err(|e| WorkerError::RepositoryError(e.to_string()))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(any(feature = "platform", feature = "web-axum"))]
use crate::common::test_support::ENV_MUTEX;
#[cfg(any(feature = "platform", feature = "web-axum"))]
use axum::http::StatusCode;
use std::time::Duration;
#[test]
fn test_error_display() {
let err = CrawlRsError::NotFound("User".to_string());
assert_eq!(err.to_string(), "Not found: User");
}
#[cfg(any(feature = "platform", feature = "web-axum"))]
#[test]
fn test_status_code_mapping() {
assert_eq!(
CrawlRsError::NotFound("test".to_string()).status_code(),
StatusCode::NOT_FOUND
);
assert_eq!(
CrawlRsError::Validation("test".to_string()).status_code(),
StatusCode::BAD_REQUEST
);
assert_eq!(
CrawlRsError::PermissionDenied("test".to_string()).status_code(),
StatusCode::FORBIDDEN
);
assert_eq!(
CrawlRsError::RateLimit("test".to_string()).status_code(),
StatusCode::TOO_MANY_REQUESTS
);
}
fn make_db_error(msg: &str) -> CrawlRsError {
#[cfg(any(feature = "platform", feature = "web-axum"))]
{
CrawlRsError::Database(dbnexus::sea_orm::DbErr::Custom(msg.to_string()))
}
#[cfg(not(any(feature = "platform", feature = "web-axum")))]
{
CrawlRsError::Database(msg.to_string())
}
}
#[test]
fn test_error_code_mapping() {
let db_err = make_db_error("test connection error");
assert_eq!(db_err.error_code(), "DATABASE_ERROR");
assert_eq!(
CrawlRsError::NotFound("test".to_string()).error_code(),
"NOT_FOUND"
);
assert_eq!(
CrawlRsError::Validation("test".to_string()).error_code(),
"VALIDATION_ERROR"
);
}
#[test]
fn test_to_api_error_response() {
let err = CrawlRsError::NotFound("User not found".to_string());
let response = err.to_api_error_response();
assert_eq!(response.code, "NOT_FOUND");
assert_eq!(response.message, "Not found: User not found");
}
#[test]
fn test_user_message_sanitization() {
let db_err =
make_db_error("Connection failed to postgres://user:password123@localhost:5432");
let user_msg = db_err.user_message();
assert!(!user_msg.contains("password123"));
assert!(!user_msg.contains("localhost:5432"));
assert!(user_msg.contains("Database operation failed"));
let net_err = crate::utils::error_helpers::map_to_network_error(
"Connection refused to 10.0.0.1:8080",
);
let user_msg = net_err.user_message();
assert!(!user_msg.contains("10.0.0.1"));
assert!(user_msg.contains("External service unavailable"));
let config_err =
CrawlRsError::Config("Missing database URL in /home/dev/crawlrs/.env".to_string());
let user_msg = config_err.user_message();
assert!(!user_msg.contains("/home/dev/"));
assert!(user_msg.contains("Configuration error"));
let validation_err = CrawlRsError::Validation("Email format is invalid".to_string());
let user_msg = validation_err.user_message();
assert!(user_msg.contains("Email format is invalid"));
}
#[test]
fn test_detailed_message_preserves_info() {
let err = make_db_error("Connection failed to postgres://user:password123@localhost:5432");
let detailed = err.detailed_message();
assert!(detailed.contains("postgres://"));
}
#[test]
fn test_sanitize_engine_error_removes_file_paths() {
let error = "Failed to load config from /home/dev/crawlrs/src/config.rs:42";
let sanitized = sanitize_engine_error(error);
assert!(!sanitized.contains("/home/dev/crawlrs/"));
assert!(sanitized.contains("[FILE_PATH]") || sanitized.contains("[LOCATION]"));
}
#[test]
fn test_sanitize_engine_error_removes_internal_ips() {
let error = "Connection failed to 10.0.0.1:5432 and 192.168.1.100:8080";
let sanitized = sanitize_engine_error(error);
assert!(!sanitized.contains("10.0.0.1"));
assert!(!sanitized.contains("192.168.1.100"));
assert!(sanitized.contains("[INTERNAL_IP]"));
}
#[test]
fn test_sanitize_engine_error_removes_ports() {
let error = "Service at localhost:8080 and 127.0.0.1:5432 failed";
let sanitized = sanitize_engine_error(error);
assert!(sanitized.contains("localhost:[PORT]"));
assert!(sanitized.contains("127.0.0.1:[PORT]"));
}
#[test]
fn test_sanitize_engine_error_removes_credentials() {
let error = "Failed to connect to postgres://admin:secret123@db.example.com:5432/mydb";
let sanitized = sanitize_engine_error(error);
assert!(!sanitized.contains("admin:secret123"));
assert!(sanitized.contains("[USER]:[PASSWORD]"));
}
#[test]
fn test_sanitize_engine_error_removes_api_keys() {
let error = "Request failed with api_key=sk-1234567890abcdef and token=xyz789";
let sanitized = sanitize_engine_error(error);
assert!(!sanitized.contains("sk-1234567890abcdef"));
assert!(!sanitized.contains("xyz789"));
assert!(sanitized.contains("[REDACTED]"));
}
#[test]
fn test_sanitize_engine_error_removes_url_params() {
let error = "Failed to fetch https://api.example.com/data?secret=abc123&token=xyz";
let sanitized = sanitize_engine_error(error);
assert!(!sanitized.contains("secret=abc123"));
assert!(!sanitized.contains("token=xyz"));
assert!(sanitized.contains("[PARAMS_REDACTED]"));
}
#[cfg(any(feature = "platform", feature = "web-axum"))]
#[test]
fn test_should_show_detailed_errors_in_dev() {
let _guard = ENV_MUTEX.lock().unwrap_or_else(|e| e.into_inner());
std::env::remove_var("CRAWLRS_ENV");
std::env::remove_var("APP_ENVIRONMENT");
std::env::remove_var("RUST_ENV");
std::env::set_var("CRAWLRS_ENV", "development");
assert!(should_show_detailed_errors());
std::env::remove_var("CRAWLRS_ENV");
}
#[cfg(any(feature = "platform", feature = "web-axum"))]
#[test]
fn test_should_show_detailed_errors_in_local() {
let _guard = ENV_MUTEX.lock().unwrap_or_else(|e| e.into_inner());
std::env::remove_var("CRAWLRS_ENV");
std::env::remove_var("APP_ENVIRONMENT");
std::env::remove_var("RUST_ENV");
std::env::set_var("APP_ENVIRONMENT", "local");
assert!(should_show_detailed_errors());
std::env::remove_var("APP_ENVIRONMENT");
}
#[cfg(any(feature = "platform", feature = "web-axum"))]
#[test]
fn test_should_hide_detailed_errors_in_prod() {
let _guard = ENV_MUTEX.lock().unwrap_or_else(|e| e.into_inner());
std::env::remove_var("CRAWLRS_ENV");
std::env::remove_var("APP_ENVIRONMENT");
std::env::remove_var("RUST_ENV");
std::env::set_var("CRAWLRS_ENV", "production");
assert!(!should_show_detailed_errors());
std::env::remove_var("CRAWLRS_ENV");
}
#[cfg(any(feature = "platform", feature = "web-axum"))]
#[test]
fn test_should_hide_detailed_errors_by_default() {
let _guard = ENV_MUTEX.lock().unwrap_or_else(|e| e.into_inner());
std::env::remove_var("CRAWLRS_ENV");
std::env::remove_var("APP_ENVIRONMENT");
std::env::remove_var("RUST_ENV");
assert!(!should_show_detailed_errors());
}
#[test]
fn test_sanitize_engine_error_empty_input() {
let msg = "";
let sanitized = sanitize_engine_error(msg);
assert_eq!(sanitized, "Engine error occurred");
}
#[test]
fn test_sanitize_engine_error_very_short_input() {
let msg = "abc";
let sanitized = sanitize_engine_error(msg);
assert_eq!(sanitized, "Engine error occurred");
}
#[test]
fn test_sanitize_engine_error_multiple_patterns() {
let error =
"Error at /home/dev/crawlrs/src/main.rs:42 with IP 10.0.0.1 and api_key=secret123";
let sanitized = sanitize_engine_error(error);
assert!(sanitized.contains("[FILE_PATH]") || sanitized.contains("[LOCATION]"));
assert!(sanitized.contains("[INTERNAL_IP]"));
assert!(sanitized.contains("[REDACTED]"));
}
#[cfg(any(feature = "platform", feature = "web-axum"))]
#[test]
fn test_status_code_all_variants() {
assert_eq!(
CrawlRsError::Network("conn refused".to_string()).status_code(),
StatusCode::BAD_GATEWAY
);
assert_eq!(
CrawlRsError::Config("missing key".to_string()).status_code(),
StatusCode::INTERNAL_SERVER_ERROR
);
assert_eq!(
CrawlRsError::Timeout("timed out".to_string()).status_code(),
StatusCode::GATEWAY_TIMEOUT
);
assert_eq!(
CrawlRsError::Io(std::io::Error::other("io fail")).status_code(),
StatusCode::INTERNAL_SERVER_ERROR
);
assert_eq!(
CrawlRsError::Json(serde_json::from_str::<serde_json::Value>("bad").unwrap_err())
.status_code(),
StatusCode::BAD_REQUEST
);
assert_eq!(
CrawlRsError::Engine("engine fail".to_string()).status_code(),
StatusCode::BAD_GATEWAY
);
assert_eq!(
CrawlRsError::Cache("cache down".to_string()).status_code(),
StatusCode::SERVICE_UNAVAILABLE
);
assert_eq!(
CrawlRsError::Task("task error".to_string()).status_code(),
StatusCode::INTERNAL_SERVER_ERROR
);
assert_eq!(
CrawlRsError::Other("unknown".to_string()).status_code(),
StatusCode::INTERNAL_SERVER_ERROR
);
assert_eq!(
make_db_error("db down").status_code(),
StatusCode::INTERNAL_SERVER_ERROR
);
assert_eq!(
CrawlRsError::Authentication("bad token".to_string()).status_code(),
StatusCode::UNAUTHORIZED
);
assert_eq!(
CrawlRsError::ServiceUnavailable("cache down".to_string()).status_code(),
StatusCode::SERVICE_UNAVAILABLE
);
}
#[test]
fn test_error_code_all_variants() {
assert_eq!(
CrawlRsError::Network("test".to_string()).error_code(),
"EXTERNAL_SERVICE_ERROR"
);
assert_eq!(
CrawlRsError::Config("test".to_string()).error_code(),
"CONFIGURATION_ERROR"
);
assert_eq!(
CrawlRsError::PermissionDenied("test".to_string()).error_code(),
"FORBIDDEN"
);
assert_eq!(
CrawlRsError::Timeout("test".to_string()).error_code(),
"TIMEOUT"
);
assert_eq!(
CrawlRsError::Io(std::io::Error::other("x")).error_code(),
"IO_ERROR"
);
assert_eq!(
CrawlRsError::Json(serde_json::from_str::<serde_json::Value>("x").unwrap_err())
.error_code(),
"JSON_ERROR"
);
assert_eq!(
CrawlRsError::Engine("test".to_string()).error_code(),
"ENGINE_ERROR"
);
assert_eq!(
CrawlRsError::Cache("test".to_string()).error_code(),
"CACHE_ERROR"
);
assert_eq!(
CrawlRsError::Task("test".to_string()).error_code(),
"TASK_ERROR"
);
assert_eq!(
CrawlRsError::Other("test".to_string()).error_code(),
"INTERNAL_ERROR"
);
assert_eq!(
CrawlRsError::RateLimit("test".to_string()).error_code(),
"RATE_LIMITED"
);
assert_eq!(
CrawlRsError::Authentication("test".to_string()).error_code(),
"AUTHENTICATION_ERROR"
);
assert_eq!(
CrawlRsError::ServiceUnavailable("test".to_string()).error_code(),
"SERVICE_UNAVAILABLE"
);
}
#[test]
fn test_user_message_all_variants() {
assert_eq!(
CrawlRsError::Network("http://10.0.0.1".to_string()).user_message(),
"External service unavailable. Please try again later."
);
assert_eq!(
CrawlRsError::Config("/etc/crawlrs/config.toml".to_string()).user_message(),
"Configuration error. Please contact support."
);
assert_eq!(
CrawlRsError::Timeout("30s".to_string()).user_message(),
"Request timed out. Please try again later."
);
assert_eq!(
CrawlRsError::Io(std::io::Error::other("x")).user_message(),
"Internal I/O error. Please try again later."
);
assert_eq!(
CrawlRsError::Json(serde_json::from_str::<serde_json::Value>("x").unwrap_err())
.user_message(),
"Invalid JSON format. Please check your request."
);
assert_eq!(
CrawlRsError::Cache("cache down".to_string()).user_message(),
"Cache service unavailable. Please try again later."
);
assert_eq!(
CrawlRsError::Task("task failed".to_string()).user_message(),
"Task processing error. Please try again later."
);
assert_eq!(
CrawlRsError::RateLimit("too fast".to_string()).user_message(),
"Rate limit exceeded. Please slow down your requests."
);
assert_eq!(
CrawlRsError::Other("unknown".to_string()).user_message(),
"Internal server error. Please try again later."
);
let not_found = CrawlRsError::NotFound("user 42".to_string());
assert!(not_found.user_message().contains("user 42"));
let perm = CrawlRsError::PermissionDenied("insufficient scope".to_string());
assert!(perm.user_message().contains("insufficient scope"));
let auth = CrawlRsError::Authentication("bad token".to_string());
assert!(auth.user_message().contains("Authentication failed"));
let svc_unavail = CrawlRsError::ServiceUnavailable("cache down".to_string());
assert!(svc_unavail.user_message().contains("Service unavailable"));
let engine_err = CrawlRsError::Engine(
"Failed at /home/dev/src/main.rs:42 with api_key=secret".to_string(),
);
let msg = engine_err.user_message();
assert!(!msg.contains("secret"));
assert!(!msg.contains("/home/dev/"));
}
#[test]
fn test_from_app_error_to_api_error_response() {
let err = CrawlRsError::Validation("field required".to_string());
let response: ApiErrorResponse = err.into();
assert_eq!(response.code, "VALIDATION_ERROR");
assert_eq!(response.message, "Validation error: field required");
}
#[test]
fn test_from_app_error_network_to_api_error_response() {
let err = CrawlRsError::Network("timeout".to_string());
let response: ApiErrorResponse = err.into();
assert_eq!(response.code, "EXTERNAL_SERVICE_ERROR");
assert!(response.message.contains("timeout"));
}
#[cfg(any(feature = "platform", feature = "web-axum"))]
#[tokio::test]
async fn test_into_response_production_env() {
let response = {
let _guard = ENV_MUTEX.lock().unwrap_or_else(|e| e.into_inner());
std::env::set_var("CRAWLRS_ENV", "production");
std::env::remove_var("APP_ENVIRONMENT");
std::env::remove_var("RUST_ENV");
CrawlRsError::NotFound("resource 123".to_string()).into_response()
};
assert_eq!(response.status(), StatusCode::NOT_FOUND);
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
.await
.unwrap();
let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(json["success"], false);
assert_eq!(json["error"]["code"], "NOT_FOUND");
assert_eq!(json["data"], serde_json::Value::Null);
assert!(json.get("timestamp").is_some());
assert!(json["error"]["message"]
.as_str()
.unwrap()
.contains("Resource not found"));
}
#[cfg(any(feature = "platform", feature = "web-axum"))]
#[tokio::test]
async fn test_into_response_development_env() {
let response = {
let _guard = ENV_MUTEX.lock().unwrap_or_else(|e| e.into_inner());
std::env::set_var("CRAWLRS_ENV", "development");
CrawlRsError::Validation("bad input".to_string()).into_response()
};
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
.await
.unwrap();
let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(json["success"], false);
assert_eq!(json["error"]["code"], "VALIDATION_ERROR");
assert_eq!(json["data"], serde_json::Value::Null);
assert!(json.get("timestamp").is_some());
assert!(json["error"]["message"]
.as_str()
.unwrap()
.contains("bad input"));
}
#[cfg(any(feature = "platform", feature = "web-axum"))]
#[tokio::test]
async fn test_into_response_rate_limit() {
let response = {
let _guard = ENV_MUTEX.lock().unwrap_or_else(|e| e.into_inner());
std::env::set_var("CRAWLRS_ENV", "production");
std::env::remove_var("APP_ENVIRONMENT");
std::env::remove_var("RUST_ENV");
CrawlRsError::RateLimit("too many requests".to_string()).into_response()
};
assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS);
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
.await
.unwrap();
let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(json["error"]["code"], "RATE_LIMITED");
assert_eq!(json["data"], serde_json::Value::Null);
assert!(json.get("timestamp").is_some());
}
#[test]
fn test_from_engine_error_request_failed() {
let err: CrawlRsError =
crate::engines::engine_client::EngineError::RequestFailed("conn refused".to_string())
.into();
match err {
CrawlRsError::Engine(msg) => assert!(msg.contains("conn refused")),
other => panic!("expected Engine variant, got {:?}", other),
}
}
#[test]
fn test_from_engine_error_timeout() {
let err: CrawlRsError =
crate::engines::engine_client::EngineError::Timeout(Duration::from_secs(30)).into();
match err {
CrawlRsError::Timeout(msg) => assert!(msg.contains("30")),
other => panic!("expected Timeout variant, got {:?}", other),
}
}
#[test]
fn test_from_engine_error_no_engines_available() {
let err: CrawlRsError =
crate::engines::engine_client::EngineError::NoEnginesAvailable.into();
match err {
CrawlRsError::Engine(msg) => assert!(msg.contains("No scraping engines available")),
other => panic!("expected Engine variant, got {:?}", other),
}
}
#[test]
fn test_from_engine_error_invalid_url() {
let err: CrawlRsError =
crate::engines::engine_client::EngineError::InvalidUrl("bad url".to_string()).into();
match err {
CrawlRsError::Validation(msg) => assert_eq!(msg, "bad url"),
other => panic!("expected Validation variant, got {:?}", other),
}
}
#[test]
fn test_from_engine_error_ssrf_protection() {
let err: CrawlRsError =
crate::engines::engine_client::EngineError::SsrfProtection("127.0.0.1".to_string())
.into();
match err {
CrawlRsError::PermissionDenied(msg) => {
assert!(msg.contains("SSRF protection"));
assert!(msg.contains("127.0.0.1"));
}
other => panic!("expected PermissionDenied variant, got {:?}", other),
}
}
#[test]
fn test_from_engine_error_browser_error() {
let err: CrawlRsError =
crate::engines::engine_client::EngineError::BrowserError("crashed".to_string()).into();
match err {
CrawlRsError::Engine(msg) => {
assert!(msg.contains("Browser error"));
assert!(msg.contains("crashed"));
}
other => panic!("expected Engine variant, got {:?}", other),
}
}
#[test]
fn test_from_engine_error_antibot_detected() {
let err: CrawlRsError = crate::engines::engine_client::EngineError::AntiBotDetected(
"Cloudflare challenge".to_string(),
)
.into();
match err {
CrawlRsError::Engine(msg) => {
assert!(msg.contains("Anti-bot detected"));
assert!(msg.contains("Cloudflare challenge"));
}
other => panic!("expected Engine variant, got {:?}", other),
}
}
#[test]
fn test_from_engine_error_expired() {
let err: CrawlRsError = crate::engines::engine_client::EngineError::Expired.into();
match err {
CrawlRsError::Timeout(msg) => assert_eq!(msg, "Request expired"),
other => panic!("expected Timeout variant, got {:?}", other),
}
}
#[test]
fn test_from_engine_error_all_engines_failed() {
let err: CrawlRsError =
crate::engines::engine_client::EngineError::AllEnginesFailed("all down".to_string())
.into();
match err {
CrawlRsError::Engine(msg) => {
assert!(msg.contains("All engines failed"));
assert!(msg.contains("all down"));
}
other => panic!("expected Engine variant, got {:?}", other),
}
}
#[test]
fn test_from_engine_error_other() {
let err: CrawlRsError =
crate::engines::engine_client::EngineError::Other("misc".to_string()).into();
match err {
CrawlRsError::Engine(msg) => assert_eq!(msg, "misc"),
other => panic!("expected Engine variant, got {:?}", other),
}
}
#[test]
fn test_from_engine_error_internal() {
let err: CrawlRsError =
crate::engines::engine_client::EngineError::Internal("internal bug".to_string()).into();
match err {
CrawlRsError::Engine(msg) => assert_eq!(msg, "internal bug"),
other => panic!("expected Engine variant, got {:?}", other),
}
}
#[test]
fn test_from_reqwest_error_conversion() {
let result = reqwest::Proxy::all("not a valid url");
assert!(result.is_err(), "Proxy::all should reject invalid URL");
let reqwest_err = result.unwrap_err();
let app_err: CrawlRsError = reqwest_err.into();
match app_err {
CrawlRsError::Network(msg) => assert!(!msg.is_empty()),
other => panic!("expected Network variant, got {:?}", other),
}
}
#[test]
fn test_worker_error_from_string() {
let err: WorkerError = "something failed".to_string().into();
assert!(matches!(err, WorkerError::InternalError(msg) if msg == "something failed"));
}
#[test]
fn test_worker_error_from_str() {
let err: WorkerError = "disk full".into();
assert!(matches!(err, WorkerError::InternalError(msg) if msg == "disk full"));
}
#[test]
fn test_worker_error_from_anyhow() {
let err: WorkerError = anyhow::anyhow!("network down").into();
assert!(matches!(err, WorkerError::InternalError(msg) if msg.contains("network down")));
}
#[test]
fn test_app_error_from_string_via_macro() {
let err: CrawlRsError = "boom".to_string().into();
assert!(matches!(err, CrawlRsError::Other(msg) if msg == "boom"));
}
#[test]
fn test_app_error_from_str_via_macro() {
let err: CrawlRsError = "crash".into();
assert!(matches!(err, CrawlRsError::Other(msg) if msg == "crash"));
}
#[test]
fn test_app_error_from_anyhow_via_macro() {
let err: CrawlRsError = anyhow::anyhow!("db timeout").into();
assert!(matches!(err, CrawlRsError::Other(msg) if msg.contains("db timeout")));
}
#[test]
fn test_worker_error_from_repository_database_error() {
let err: WorkerError = RepositoryError::DatabaseError("conn refused".to_string()).into();
assert!(matches!(err, WorkerError::RepositoryError(msg) if msg == "conn refused"));
}
#[test]
fn test_worker_error_from_repository_not_found() {
let err: WorkerError = RepositoryError::NotFound.into();
assert!(matches!(err, WorkerError::NotFound(msg) if msg.contains("not found")));
}
#[test]
fn test_worker_error_from_repository_already_exists() {
let err: WorkerError = RepositoryError::AlreadyExists.into();
assert!(matches!(err, WorkerError::RepositoryError(msg) if msg.contains("already exists")));
}
#[test]
fn test_worker_error_from_repository_invalid_parameter() {
let err: WorkerError = RepositoryError::InvalidParameter("bad uuid".to_string()).into();
assert!(matches!(err, WorkerError::DomainError(msg) if msg == "bad uuid"));
}
#[test]
fn test_worker_error_from_repository_internal_error() {
let err: WorkerError = RepositoryError::InternalError("oops".to_string()).into();
assert!(matches!(err, WorkerError::InternalError(msg) if msg == "oops"));
}
#[test]
fn test_app_error_from_repository_database_error() {
let err: CrawlRsError = RepositoryError::DatabaseError("conn refused".to_string()).into();
assert!(matches!(
err,
CrawlRsError::Database(ref m) if m.to_string().contains("conn refused")
));
}
#[test]
fn test_app_error_from_repository_not_found() {
let err: CrawlRsError = RepositoryError::NotFound.into();
assert!(matches!(err, CrawlRsError::NotFound(msg) if msg.contains("not found")));
}
#[test]
fn test_app_error_from_repository_already_exists() {
let err: CrawlRsError = RepositoryError::AlreadyExists.into();
assert!(matches!(err, CrawlRsError::Validation(msg) if msg.contains("already exists")));
}
#[test]
fn test_app_error_from_repository_invalid_parameter() {
let err: CrawlRsError = RepositoryError::InvalidParameter("bad input".to_string()).into();
assert!(matches!(err, CrawlRsError::Validation(msg) if msg == "bad input"));
}
#[test]
fn test_app_error_from_repository_internal_error() {
let err: CrawlRsError = RepositoryError::InternalError("oops".to_string()).into();
assert!(matches!(err, CrawlRsError::Other(msg) if msg == "oops"));
}
#[test]
fn test_app_error_from_task_repo_database_error() {
use crate::domain::repositories::task_repository::RepositoryError as TaskRepoError;
let err: CrawlRsError = TaskRepoError::Database(anyhow::anyhow!("pool exhausted")).into();
assert!(matches!(err, CrawlRsError::Other(msg) if msg.contains("pool exhausted")));
}
#[test]
fn test_app_error_from_task_repo_not_found() {
use crate::domain::repositories::task_repository::RepositoryError as TaskRepoError;
let err: CrawlRsError = TaskRepoError::NotFound.into();
assert!(matches!(err, CrawlRsError::NotFound(msg) if msg.contains("not found")));
}
#[test]
fn test_app_error_from_robots_checker_error() {
let robots_err =
crate::utils::robots::RobotsCheckerError::ValidationError("blocked".to_string());
let err: CrawlRsError = robots_err.into();
assert!(matches!(err, CrawlRsError::Other(msg) if msg.contains("blocked")));
}
#[test]
fn test_repository_result_ext_ok() {
let result: Result<i32, &str> = Ok(42);
assert_eq!(result.repo_err().expect("ok should pass through"), 42);
}
#[test]
fn test_repository_result_ext_err_maps_to_worker_error() {
let result: Result<i32, &str> = Err("disk failure");
let err = result.repo_err().unwrap_err();
assert!(matches!(err, WorkerError::RepositoryError(msg) if msg.contains("disk failure")));
}
#[cfg(any(feature = "platform", feature = "web-axum"))]
#[tokio::test]
async fn test_app_error_authentication_returns_401() {
let response = {
let _guard = ENV_MUTEX.lock().unwrap_or_else(|e| e.into_inner());
std::env::remove_var("CRAWLRS_ENV");
std::env::remove_var("APP_ENVIRONMENT");
std::env::remove_var("RUST_ENV");
CrawlRsError::Authentication("bad token".to_string()).into_response()
};
assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
.await
.unwrap();
let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(json["error"]["code"], "AUTHENTICATION_ERROR");
assert_eq!(json["data"], serde_json::Value::Null);
assert!(json.get("timestamp").is_some());
}
#[cfg(any(feature = "platform", feature = "web-axum"))]
#[tokio::test]
async fn test_app_error_service_unavailable_returns_503() {
let response = {
let _guard = ENV_MUTEX.lock().unwrap_or_else(|e| e.into_inner());
std::env::set_var("CRAWLRS_ENV", "production");
std::env::remove_var("APP_ENVIRONMENT");
std::env::remove_var("RUST_ENV");
let response =
CrawlRsError::ServiceUnavailable("cache down".to_string()).into_response();
std::env::remove_var("CRAWLRS_ENV");
response
};
assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
.await
.unwrap();
let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(json["error"]["code"], "SERVICE_UNAVAILABLE");
assert_eq!(json["data"], serde_json::Value::Null);
assert!(json.get("timestamp").is_some());
}
#[cfg(any(feature = "platform", feature = "web-axum"))]
#[test]
fn test_user_message_locale_across_all_variants() {
use crate::i18n::{I18nBundle, Locale};
let bundle = I18nBundle::load("en-US", &["en-US", "zh-CN"]).unwrap();
let en: Locale = "en-US".parse().unwrap();
let zh: Locale = "zh-CN".parse().unwrap();
let variants = vec![
make_db_error("db"),
CrawlRsError::Network("n".into()),
CrawlRsError::Config("c".into()),
CrawlRsError::Validation("v".into()),
CrawlRsError::Unprocessable("u".into()),
CrawlRsError::NotFound("nf".into()),
CrawlRsError::Authentication("a".into()),
CrawlRsError::PermissionDenied("pd".into()),
CrawlRsError::ServiceUnavailable("su".into()),
CrawlRsError::Timeout("t".into()),
CrawlRsError::Io(std::io::Error::other("io")),
CrawlRsError::Json(
serde_json::from_str::<serde_json::Value>("not-json")
.expect_err("invalid json must produce serde error"),
),
CrawlRsError::Engine("e".into()),
CrawlRsError::Cache("ca".into()),
CrawlRsError::Task("tk".into()),
CrawlRsError::RateLimit("rl".into()),
CrawlRsError::Other("o".into()),
];
for v in &variants {
assert!(
!v.user_message_locale(&en, &bundle).is_empty(),
"en empty: {v}"
);
assert!(
!v.user_message_locale(&zh, &bundle).is_empty(),
"zh empty: {v}"
);
assert!(!v.to_string().is_empty());
}
}
#[cfg(any(feature = "platform", feature = "web-axum"))]
#[test]
fn test_status_code_matches_pre_migration_table() {
use axum::http::StatusCode;
let expected: Vec<(CrawlRsError, u16)> = vec![
(make_db_error("db"), 500),
(CrawlRsError::Network("n".into()), 502),
(CrawlRsError::Config("c".into()), 500),
(CrawlRsError::Validation("v".into()), 400),
(CrawlRsError::Unprocessable("u".into()), 422),
(CrawlRsError::NotFound("nf".into()), 404),
(CrawlRsError::Authentication("a".into()), 401),
(CrawlRsError::PermissionDenied("pd".into()), 403),
(CrawlRsError::ServiceUnavailable("su".into()), 503),
(CrawlRsError::Timeout("t".into()), 504),
(CrawlRsError::Io(std::io::Error::other("io")), 500),
(
CrawlRsError::Json(
serde_json::from_str::<serde_json::Value>("not-json")
.expect_err("invalid json must produce serde error"),
),
400,
),
(CrawlRsError::Engine("e".into()), 502),
(CrawlRsError::Cache("ca".into()), 503),
(CrawlRsError::Task("tk".into()), 500),
(CrawlRsError::RateLimit("rl".into()), 429),
(CrawlRsError::Other("o".into()), 500),
];
assert_eq!(expected.len(), 17, "17 个领域变体必须逐一断言");
for (variant, want) in expected {
assert_eq!(
variant.status_code(),
StatusCode::from_u16(want).unwrap(),
"status_code mismatch for {variant:?}"
);
}
}
#[cfg(any(feature = "platform", feature = "web-axum"))]
#[test]
fn test_status_code_delegates_to_sdforge_mapping() {
use axum::http::StatusCode;
let normalized = vec![
make_db_error("db"),
CrawlRsError::Config("c".into()),
CrawlRsError::Validation("v".into()),
CrawlRsError::Unprocessable("u".into()),
CrawlRsError::NotFound("nf".into()),
CrawlRsError::Authentication("a".into()),
CrawlRsError::PermissionDenied("pd".into()),
CrawlRsError::ServiceUnavailable("su".into()),
CrawlRsError::Io(std::io::Error::other("io")),
CrawlRsError::Json(
serde_json::from_str::<serde_json::Value>("not-json")
.expect_err("invalid json must produce serde error"),
),
CrawlRsError::Cache("ca".into()),
CrawlRsError::Task("tk".into()),
CrawlRsError::RateLimit("rl".into()),
CrawlRsError::Other("o".into()),
];
assert_eq!(normalized.len(), 14, "14 个归一变体必须逐一断言");
for variant in &normalized {
let api_error = variant
.to_api_error()
.expect("normalized variant must produce ApiError");
let want = StatusCode::from_u16(sdforge::error::unified::http_status_for(&api_error))
.expect("sdforge status must be valid");
assert_eq!(
variant.status_code(),
want,
"delegation mismatch: {variant:?}"
);
}
assert!(CrawlRsError::Network("n".into()).to_api_error().is_none());
assert!(CrawlRsError::Engine("e".into()).to_api_error().is_none());
assert!(CrawlRsError::Timeout("t".into()).to_api_error().is_none());
}
#[test]
fn test_sanitized_message_semantic_alignment_with_sdforge() {
let internal_detail = "connect 10.0.0.1:5432 failed password=sk-secret";
let masked =
sdforge::error::ApiError::internal_error(internal_detail, "probe").sanitized_message();
assert!(!masked.contains("10.0.0.1"));
assert!(!masked.contains("sk-secret"));
assert!(!masked.is_empty());
let svc_detail = "upstream http://user:pw@10.1.2.3:8080 down";
let svc_masked =
sdforge::error::ApiError::service_unavailable("upstream", None).sanitized_message();
assert!(!svc_masked.contains("10.1.2.3"));
assert!(!svc_masked.contains("pw"));
let crawlrs_internal =
CrawlRsError::Other("connect 10.0.0.1:5432 failed password=sk-secret".to_string());
let crawlrs_msg = crawlrs_internal.user_message();
assert!(!crawlrs_msg.contains("10.0.0.1"));
assert!(!crawlrs_msg.contains("sk-secret"));
let crawlrs_svc = CrawlRsError::ServiceUnavailable(svc_detail.to_string());
let crawlrs_svc_msg = crawlrs_svc.user_message();
assert!(!crawlrs_svc_msg.contains("10.1.2.3"));
assert!(!crawlrs_svc_msg.contains("user:pw"));
}
#[cfg(any(feature = "platform", feature = "web-axum"))]
#[test]
fn test_user_message_locale_fallback_chain() {
use crate::i18n::{I18nBundle, Locale};
let bundle = I18nBundle::load("en-US", &["en-US", "zh-CN"]).unwrap();
let en: Locale = "en-US".parse().unwrap();
let zh: Locale = "zh-CN".parse().unwrap();
let localized = CrawlRsError::Timeout("t".into()).user_message_locale(&zh, &bundle);
assert!(!localized.is_empty());
let missing_in_zh = bundle.translate(&zh, "error-nonexistent-key");
assert_eq!(missing_in_zh, "error-nonexistent-key");
assert_eq!(
bundle.translate(&en, "error-nonexistent-key"),
"error-nonexistent-key"
);
let msg = CrawlRsError::Validation("email format invalid".to_string());
let rendered = msg.user_message_locale(&en, &bundle);
assert!(
rendered.contains("email format invalid"),
"arg interpolation lost: {rendered}"
);
}
}