sensors using keys

This commit is contained in:
2026-02-20 14:11:06 -06:00
parent f9cfcf8f40
commit a84c07082c
9 changed files with 416 additions and 260 deletions

View File

@@ -148,8 +148,42 @@ impl From<sqlx::Error> for ApiError {
match err {
sqlx::Error::RowNotFound => ApiError::NotFound("Resource not found".to_string()),
sqlx::Error::Database(db_err) => {
// Check for unique constraint violations
if let Some(constraint) = db_err.constraint() {
// PostgreSQL error codes:
// 23505 = unique_violation → 409 Conflict
// 23503 = foreign_key_violation → 422 Unprocessable Entity
// 23514 = check_violation → 422 Unprocessable Entity
// P0001 = raise_exception → 400 Bad Request (trigger-raised errors)
let pg_code = db_err.code().map(|c| c.to_string()).unwrap_or_default();
if pg_code == "23505" {
// Unique constraint violation — duplicate key
let detail = db_err
.constraint()
.map(|c| format!(" ({})", c))
.unwrap_or_default();
ApiError::Conflict(format!("Already exists{}", detail))
} else if pg_code == "23503" {
// Foreign key violation — the referenced row doesn't exist
let detail = db_err
.constraint()
.map(|c| format!(" ({})", c))
.unwrap_or_default();
ApiError::UnprocessableEntity(format!(
"Referenced entity does not exist{}",
detail
))
} else if pg_code == "23514" {
// CHECK constraint violation — value doesn't meet constraint
let detail = db_err
.constraint()
.map(|c| format!(": {}", c))
.unwrap_or_default();
ApiError::UnprocessableEntity(format!("Validation constraint failed{}", detail))
} else if pg_code == "P0001" {
// RAISE EXCEPTION from a trigger or function
// Extract the human-readable message from the exception
let msg = db_err.message().to_string();
ApiError::BadRequest(msg)
} else if let Some(constraint) = db_err.constraint() {
ApiError::Conflict(format!("Constraint violation: {}", constraint))
} else {
ApiError::DatabaseError(format!("Database error: {}", db_err))

View File

@@ -719,8 +719,13 @@ pub async fn update_sensor(
label: request.label,
description: request.description,
entrypoint: request.entrypoint,
runtime: None,
runtime_ref: None,
trigger: None,
trigger_ref: None,
enabled: request.enabled,
param_schema: request.param_schema,
config: None,
};
let sensor = SensorRepository::update(&state.db, existing_sensor.id, update_input).await?;
@@ -799,8 +804,13 @@ pub async fn enable_sensor(
label: None,
description: None,
entrypoint: None,
runtime: None,
runtime_ref: None,
trigger: None,
trigger_ref: None,
enabled: Some(true),
param_schema: None,
config: None,
};
let sensor = SensorRepository::update(&state.db, existing_sensor.id, update_input).await?;
@@ -840,8 +850,13 @@ pub async fn disable_sensor(
label: None,
description: None,
entrypoint: None,
runtime: None,
runtime_ref: None,
trigger: None,
trigger_ref: None,
enabled: Some(false),
param_schema: None,
config: None,
};
let sensor = SensorRepository::update(&state.db, existing_sensor.id, update_input).await?;