Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions aimdb-client/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- **A rejected `set_record` value is `invalid_value` (#454).** A value the
record's type cannot decode now fails with `ServerError { code:
"invalid_value" }` instead of `internal: engine/transport failure`.

- **`get_record` on a ring returns `unsupported` (#444).** A ring has no
current value; use `drain_record` or `subscribe`. `get_record` no longer moves
the connection's drain cursor, so `drain_record` returns every unread event.
Expand Down
5 changes: 5 additions & 0 deletions aimdb-client/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,11 @@ fn rpc_err(e: RpcError) -> ClientError {
ClientError::server_error("not_found", "method or record not found", None)
}
RpcError::Denied => ClientError::server_error("denied", "permission denied", None),
RpcError::InvalidValue => ClientError::server_error(
"invalid_value",
"the value does not match the record's type",
None,
),
RpcError::Unsupported => ClientError::server_error(
"unsupported",
"not supported for this record (a ring has no current value: \
Expand Down
12 changes: 12 additions & 0 deletions aimdb-client/tests/aimx_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ async fn aimx_roundtrip_over_uds_production_server() {
assert_eq!(after_set.stamp(), set_ts);
assert!(set_ts >= seeded);

// RPC: a value the record's type rejects is the caller's fault, not the
// engine's (#454): `invalid_value`, the stored value untouched, and the
// connection still serves the next call.
match conn.set_record("setting", json!({ "level": "high" })).await {
Err(aimdb_client::ClientError::ServerError { code, .. }) => {
assert_eq!(code, "invalid_value")
}
other => panic!("expected invalid_value, got {other:?}"),
}
let unchanged = conn.get_record("setting").await.unwrap();
assert_eq!(unchanged.value, json!({ "level": 7 }));

// Streaming: a producer feeds `events`; the subscription routes updates back.
let producer = db.producer(key_events_2).expect("producer");
tokio::spawn(async move {
Expand Down
6 changes: 6 additions & 0 deletions aimdb-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
an unknown name answer `not_found` at once. A ring registered with
`buffer_raw()` and no `buffer_cfg()` answers `not_found`, since core cannot
tell it is a ring.
- **A rejected `record.set` value answers `invalid_value` (#454).** A value
that does not decode into the record's type was reported as `internal`,
blaming the engine. It now fails with the new `DbError::InvalidValue`
(kind `Data`), which AimX sends as `RpcError::InvalidValue` / wire code
`invalid_value`. Older clients decode the unknown code as `internal`, as
before.

### Added

Expand Down
17 changes: 17 additions & 0 deletions aimdb-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ pub enum DbError {
#[error("Permission denied: {operation}")]
PermissionDenied { operation: String },

/// A remote write's value does not decode into the record's type
#[error("Value rejected: it does not decode into '{type_name}'")]
InvalidValue { type_name: String },

// ===== Configuration Errors =====
/// Missing required configuration parameter
#[error("Missing configuration parameter: {parameter}")]
Expand Down Expand Up @@ -249,6 +253,13 @@ impl DbError {
}
}

/// Builds an [`InvalidValue`](DbError::InvalidValue).
pub fn invalid_value(type_name: impl Into<String>) -> Self {
DbError::InvalidValue {
type_name: type_name.into(),
}
}

/// Builds a [`RecordKeyNotFound`](DbError::RecordKeyNotFound).
pub fn record_key_not_found(key: impl Into<String>) -> Self {
DbError::RecordKeyNotFound { key: key.into() }
Expand Down Expand Up @@ -311,6 +322,8 @@ impl DbError {

Self::ConnectionFailed { .. } => DbErrorKind::Transport,

Self::InvalidValue { .. } => DbErrorKind::Data,

Self::MissingConfiguration { .. }
| Self::InvalidConfiguration { .. }
| Self::CyclicDependency { .. }
Expand Down Expand Up @@ -403,6 +416,10 @@ mod tests {
DbError::permission_denied("write"),
DbError::PermissionDenied { .. }
));
assert!(matches!(
DbError::invalid_value("T"),
DbError::InvalidValue { .. }
));
assert!(matches!(
DbError::record_key_not_found("k"),
DbError::RecordKeyNotFound { .. }
Expand Down
3 changes: 3 additions & 0 deletions aimdb-core/src/session/aimx/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ fn err_code(e: &RpcError) -> &'static str {
RpcError::NotFound => "not_found",
RpcError::Unsupported => "unsupported",
RpcError::Denied => "denied",
RpcError::InvalidValue => "invalid_value",
RpcError::Internal => "internal",
RpcError::VersionMismatch => "version_mismatch",
// Client-side only; a server has no reason to send it.
Expand All @@ -161,6 +162,7 @@ fn code_err(s: &str) -> RpcError {
"not_found" => RpcError::NotFound,
"unsupported" => RpcError::Unsupported,
"denied" => RpcError::Denied,
"invalid_value" => RpcError::InvalidValue,
"version_mismatch" => RpcError::VersionMismatch,
_ => RpcError::Internal,
}
Expand Down Expand Up @@ -519,6 +521,7 @@ mod tests {
RpcError::NotFound,
RpcError::Unsupported,
RpcError::Denied,
RpcError::InvalidValue,
RpcError::Internal,
RpcError::VersionMismatch,
] {
Expand Down
1 change: 1 addition & 0 deletions aimdb-core/src/session/aimx/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ fn map_db_err(e: DbError) -> RpcError {
match e {
DbError::RecordKeyNotFound { .. } | DbError::InvalidRecordId { .. } => RpcError::NotFound,
DbError::PermissionDenied { .. } => RpcError::Denied,
DbError::InvalidValue { .. } => RpcError::InvalidValue,
_ => RpcError::Internal,
}
}
Expand Down
3 changes: 3 additions & 0 deletions aimdb-core/src/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ pub enum RpcError {
Unsupported,
/// The caller lacks permission for this operation.
Denied,
/// The caller's value was refused: it does not decode into the record's
/// type, e.g. a `record.set` value that fails the type's deserializer.
InvalidValue,
/// The handler failed.
Internal,
/// The peer's declared protocol version is incompatible with this server's
Expand Down
24 changes: 6 additions & 18 deletions aimdb-core/src/typed_record/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1201,42 +1201,30 @@ impl<T: Send + Sync + 'static + Debug + Clone> JsonRecordAccess for TypedRecord<
}

fn set_from_json(&self, json_value: serde_json::Value) -> crate::DbResult<()> {
use crate::DbError;

log_debug!(
"set_from_json called for type: {}",
core::any::type_name::<T>()
);

let codec = self.remote_write_codec("set_from_json")?;

let value: T = codec.decode(&json_value).ok_or_else(|| {
DbError::runtime_error(alloc::format!(
"Failed to deserialize JSON to type '{}'. \
JSON structure does not match the expected schema.",
core::any::type_name::<T>()
))
})?;
let value: T = codec
.decode(&json_value)
.ok_or_else(|| crate::DbError::invalid_value(core::any::type_name::<T>()))?;

self.commit_remote_write(value)
}

fn set_from_json_bytes(&self, bytes: &[u8]) -> crate::DbResult<()> {
use crate::DbError;

log_debug!(
"set_from_json_bytes called for type: {}",
core::any::type_name::<T>()
);

let codec = self.remote_write_codec("set_from_json_bytes")?;
let value: T = codec.decode_slice(bytes).ok_or_else(|| {
DbError::runtime_error(alloc::format!(
"Failed to deserialize JSON to type '{}'. \
JSON structure does not match the expected schema.",
core::any::type_name::<T>()
))
})?;
let value: T = codec
.decode_slice(bytes)
.ok_or_else(|| crate::DbError::invalid_value(core::any::type_name::<T>()))?;

self.commit_remote_write(value)
}
Expand Down
3 changes: 3 additions & 0 deletions aimdb-wasm-adapter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed (breaking, wire)

- A `record.set` value the record's type rejects now reports
`invalid_value` instead of `internal` (#454).

- **`query()` resolves with `{ records }` (#429).** `total` is gone; use
`records.length`.

Expand Down
1 change: 1 addition & 0 deletions aimdb-wasm-adapter/src/ws_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,7 @@ fn rpc_err_str(e: &RpcError) -> &'static str {
RpcError::NotFound => "not_found",
RpcError::Unsupported => "unsupported (a ring has no current value)",
RpcError::Denied => "denied",
RpcError::InvalidValue => "invalid_value (the value does not match the record's type)",
RpcError::VersionMismatch => {
"version_mismatch (server rejected the client protocol version)"
}
Expand Down