spacedriveapp/spacedrive · error · CoreError

Failed to serialize {}: {}

Error message

Failed to serialize {}: {}

What it means

Produced inside the resource-registration macro in core/src/domain/resource_registry.rs:237 when serde_json::to_value(&resource) fails for a resource fetched through Identifiable::from_ids. The macro serializes every resource of a registered type into JSON for the resource-resolution pipeline; if the resource's Serialize impl returns an error (not a JSON-shape mismatch, an actual serialization error), the whole from_ids batch fails with CoreError::Other wrapping this message.

Source

Thrown at core/src/domain/resource_registry.rs:237

		inventory::submit! {
			$crate::domain::resource_registry::ResourceInventoryEntry {
				build: || {
					$crate::domain::resource_registry::ResourceRegistration::new(
						<$resource as $crate::domain::resource::Identifiable>::resource_type(),
						<$resource as $crate::domain::resource::Identifiable>::sync_dependencies(),
						|db, dep_type, dep_id| {
							Box::pin(async move {
								<$resource as $crate::domain::resource::Identifiable>::route_from_dependency(db, dep_type, dep_id).await
							})
						},
						|db, ids| {
							Box::pin(async move {
								let resources = <$resource as $crate::domain::resource::Identifiable>::from_ids(db, ids).await?;
								resources
									.into_iter()
									.map(|r| {
										serde_json::to_value(&r).map_err(|e| {
											$crate::common::errors::CoreError::Other(anyhow::anyhow!(
												"Failed to serialize {}: {}",
												<$resource as $crate::domain::resource::Identifiable>::resource_type(),
												e
											))
										})
									})
									.collect::<$crate::common::errors::Result<Vec<_>>>()
							})
						},
						<$resource as $crate::domain::resource::Identifiable>::no_merge_fields(),
					)
				}
			}
		}
	};
}

#[cfg(test)]

View on GitHub (pinned to 6dfeccf211)

Solutions

  1. Reproduce directly: `serde_json::to_value(&sample_resource)` in a unit test for the failing resource type; the serde error names the exact field.
  2. Fix the Serialize impl or the offending field (most often: convert HashMap<NonStringKey, _> keys to Strings, or guard NaN/Infinity floats).
  3. Add a regression test `assert!(serde_json::to_value(&resource).is_ok())` next to the resource definition so registration-time serialization stays covered.

Example fix

// before (key type fails to serialize as JSON object key)
let meta: HashMap<i64, String> = fetch_meta();

// after
let meta: HashMap<String, String> = fetch_meta()
    .into_iter()
    .map(|(k, v)| (k.to_string(), v))
    .collect();
Defensive patterns

Strategy: try-catch

Validate before calling

// register-time smoke test: prove every resource serializes before shipping
#[test]
fn resource_serializes_to_json() {
    let r = SampleResource::fixture();
    assert!(serde_json::to_value(&r).is_ok(), "Identifiable resource must serialize");
}

Try / catch

// catch per resource batch and report the type instead of a generic opaque error
let values = serde_json::to_value(&resource).map_err(|e| {
    CoreError::Other(anyhow::anyhow!("resource {} failed JSON roundtrip: {e}", std::any::type_name::<R>()))
});

Prevention

When it happens

Trigger: Registering a resource type (via the registry macro) whose Serialize impl can fail: HashMap with non-string-serializable keys, custom Serialize::serialize returning Err, f64::NAN with serde_json arbitrary-precision features, or a field type whose to_value errors at runtime.

Common situations: Adding a new Identifiable resource with a hand-written Serialize; changing a domain struct so a map key type stops serializing to a JSON string; unit tests only ever checking Debug, never exercising to_value.

Related errors


AI-assisted analysis of spacedriveapp/spacedrive@6dfeccf211 (2026-08-16). Data as JSON: /api/errors/61b780a5a905d3d3. Report an issue: GitHub.