Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: Use different error message for empty strings in schema processing #2151

Merged
merged 4 commits into from
May 24, 2023
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

**Features**:

- Use different error message for empty strings in schema processing. ([#2151](https://github.com/getsentry/relay/pull/2151))

## 23.5.1

**Bug Fixes**:
28 changes: 22 additions & 6 deletions relay-general/src/store/schema.rs
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ impl Processor for SchemaProcessor {
state: &ProcessingState<'_>,
) -> ProcessingResult {
value_trim_whitespace(value, meta, state);
verify_value_nonempty(value, meta, state)?;
verify_value_nonempty_string(value, meta, state)?;
verify_value_characters(value, meta, state)?;
Ok(())
}
@@ -84,6 +84,22 @@ where
}
}

fn verify_value_nonempty_string<T>(
value: &mut T,
meta: &mut Meta,
state: &ProcessingState<'_>,
) -> ProcessingResult
where
T: Empty,
{
if state.attrs().nonempty && value.is_empty() {
meta.add_error(Error::nonempty_string());
Err(ProcessingAction::DeleteValueHard)
} else {
Ok(())
}
}

fn verify_value_characters(
value: &mut str,
meta: &mut Meta,
@@ -113,7 +129,7 @@ mod tests {
};
use crate::types::{Annotated, Array, Error, ErrorKind, Object};

fn assert_nonempty_base<T>()
fn assert_nonempty_base<T>(expected_error: &str)
where
T: Default + PartialEq + crate::processor::ProcessValue,
{
@@ -133,25 +149,25 @@ mod tests {
assert_eq!(
wrapper,
Annotated::new(Foo {
bar: Annotated::from_error(Error::expected("a non-empty value"), None),
bar: Annotated::from_error(Error::expected(expected_error), None),
bar2: Annotated::new(T::default())
})
);
}

#[test]
fn test_nonempty_string() {
assert_nonempty_base::<String>();
assert_nonempty_base::<String>("a non-empty string");
}

#[test]
fn test_nonempty_array() {
assert_nonempty_base::<Array<u64>>();
assert_nonempty_base::<Array<u64>>("a non-empty value");
}

#[test]
fn test_nonempty_object() {
assert_nonempty_base::<Object<u64>>();
assert_nonempty_base::<Object<u64>>("a non-empty value");
}

#[test]
6 changes: 6 additions & 0 deletions relay-general/src/types/meta.rs
Original file line number Diff line number Diff line change
@@ -314,6 +314,12 @@ impl Error {
Error::invalid("expected a non-empty value")
}

/// Creates an error that describes an expected non-empty string.
pub fn nonempty_string() -> Self {
// TODO: Replace `invalid_data` this with an explicity error constant for empty values
Error::invalid("expected a non-empty string")
}

/// Returns the kind of this error.
pub fn kind(&self) -> &ErrorKind {
&self.kind