diff --git a/capture/src/api.rs b/capture/src/api.rs index 0938ced..91ed578 100644 --- a/capture/src/api.rs +++ b/capture/src/api.rs @@ -28,6 +28,8 @@ pub enum CaptureError { EmptyBatch, #[error("event submitted with an empty event name")] MissingEventName, + #[error("event submitted with an empty distinct_id")] + EmptyDistinctId, #[error("event submitted without a distinct_id")] MissingDistinctId, @@ -59,6 +61,7 @@ impl IntoResponse for CaptureError { | CaptureError::RequestParsingError(_) | CaptureError::EmptyBatch | CaptureError::MissingEventName + | CaptureError::EmptyDistinctId | CaptureError::MissingDistinctId | CaptureError::EventTooBig | CaptureError::NonRetryableSinkError => (StatusCode::BAD_REQUEST, self.to_string()), diff --git a/capture/src/v0_request.rs b/capture/src/v0_request.rs index 3d0052e..a4e1042 100644 --- a/capture/src/v0_request.rs +++ b/capture/src/v0_request.rs @@ -217,10 +217,11 @@ impl RawEvent { .as_str() .map(|s| s.to_owned()) .unwrap_or_else(|| value.to_string()); - Ok(match distinct_id.len() { - 0..=200 => distinct_id, - _ => distinct_id.chars().take(200).collect(), - }) + match distinct_id.len() { + 0 => Err(CaptureError::EmptyDistinctId), + 1..=200 => Ok(distinct_id), + _ => Ok(distinct_id.chars().take(200).collect()), + } } } @@ -303,11 +304,16 @@ mod tests { parse_and_extract(r#"{"event": "e"}"#), Err(CaptureError::MissingDistinctId) )); - // Return MissingDistinctId if null, breaking compat with capture-py + // Return MissingDistinctId if null assert!(matches!( parse_and_extract(r#"{"event": "e", "distinct_id": null}"#), Err(CaptureError::MissingDistinctId) )); + // Return EmptyDistinctId if empty string + assert!(matches!( + parse_and_extract(r#"{"event": "e", "distinct_id": ""}"#), + Err(CaptureError::EmptyDistinctId) + )); let assert_extracted_id = |input: &'static str, expected: &str| { let id = parse_and_extract(input).expect("failed to extract");