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

Breaking changes to improve consistency in filter type errors. #621

Merged
merged 2 commits into from
Jun 13, 2024
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
11 changes: 11 additions & 0 deletions scripts/recreate_all_execution_errors.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

# Fail on first error, on undefined variables, and on failures in pipelines.
set -euo pipefail

# Move relative to the top of the repo, so this script can be run from anywhere.
cd "$(git rev-parse --show-toplevel)/trustfall_testbin"

find ../trustfall_core/test_data/tests/execution_errors -name '*.ir.ron' | \
xargs -n 1 \
sh -c 'cargo run trace $0 >"$(dirname $0)/$(basename $0 | cut -d'.' -f1).exec-error.ron"'
11 changes: 11 additions & 0 deletions scripts/recreate_all_frontend_errors.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

# Fail on first error, on undefined variables, and on failures in pipelines.
set -euo pipefail

# Move relative to the top of the repo, so this script can be run from anywhere.
cd "$(git rev-parse --show-toplevel)/trustfall_testbin"

find ../trustfall_core/test_data/tests/frontend_errors -name '*.graphql-parsed.ron' | \
xargs -n 1 \
sh -c 'cargo run frontend $0 >"$(dirname $0)/$(basename $0 | cut -d'.' -f1).frontend-error.ron"'
11 changes: 11 additions & 0 deletions scripts/recreate_all_parse_errors.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

# Fail on first error, on undefined variables, and on failures in pipelines.
set -euo pipefail

# Move relative to the top of the repo, so this script can be run from anywhere.
cd "$(git rev-parse --show-toplevel)/trustfall_testbin"

find ../trustfall_core/test_data/tests/parse_errors -name '*.graphql.ron' | \
xargs -n 1 \
sh -c 'cargo run parse $0 >"$(dirname $0)/$(basename $0 | cut -d'.' -f1).parse-error.ron"'
11 changes: 11 additions & 0 deletions scripts/recreate_all_schema_errors.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

# Fail on first error, on undefined variables, and on failures in pipelines.
set -euo pipefail

# Move relative to the top of the repo, so this script can be run from anywhere.
cd "$(git rev-parse --show-toplevel)/trustfall_testbin"

find ../trustfall_core/test_data/tests/schema_errors -name '*.graphql.ron' | \
xargs -n 1 \
sh -c 'cargo run schema_error $0 >"$(dirname $0)/$(basename $0 | cut -d'.' -f1).schema-error.ron"'
155 changes: 130 additions & 25 deletions trustfall_core/src/frontend/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use crate::{ir::FieldValue, util::DisplayVec};
use crate::{
ir::{FieldValue, Type},
util::DisplayVec,
};

#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, thiserror::Error)]
Expand Down Expand Up @@ -127,52 +130,154 @@ pub enum FilterTypeError {
IncompatibleVariableTypeRequirements(String, String, String),

#[error(
"Filter operation \"{0}\" unexpectedly used on non-nullable field \"{1}\" of type \"{2}\". \
The filter's result would always be {3}. Please rewrite the query to avoid this filter."
"Filter operation \"{0}\" is applied on non-nullable {1}. \
The filter's result would always be {2}. Please rewrite the query to avoid this filter."
)]
NonNullableTypeFilteredForNullability(String, String, String, bool),
NonNullableTypeFilteredForNullability(String, String, bool),

#[error(
"Type mismatch in @filter operation \"{0}\" between field \"{1}\" of type \"{2}\" \
and tag \"{3}\" representing field \"{4}\" of type \"{5}\""
"Filter operation \"{0}\" is comparing values with incompatible type: {1} versus {2}."
)]
TypeMismatchBetweenTagAndFilter(String, String, String, String, String, String),
TypeMismatchBetweenFilterSubjectAndArgument(String, String, String),

#[error(
"Non-orderable field \"{1}\" (type \"{2}\") used with @filter operation \"{0}\" which \"
only supports orderable types."
"Filter operation \"{0}\" can only be applied to orderable values, but is applied to {1} \
which does not support ordering comparisons."
)]
OrderingFilterOperationOnNonOrderableField(String, String, String),
OrderingFilterOperationOnNonOrderableSubject(String, String),

#[error(
"Tag \"{1}\" represents non-orderable field \"{2}\" (type \"{3}\"), but is used with \
@filter operation \"{0}\" which only supports orderable types."
"Filter operation \"{0}\" requires an argument that supports ordering comparisons, \
but is being used with non-orderable {1}."
)]
OrderingFilterOperationOnNonOrderableTag(String, String, String, String),
OrderingFilterOperationWithNonOrderableArgument(String, String),

#[error(
"Non-string field \"{1}\" (type \"{2}\") used with @filter operation \"{0}\" which \"
only supports strings."
"Filter operation \"{0}\" can only be applied to string values, but is applied to {1} \
which is not a string."
)]
StringFilterOperationOnNonStringField(String, String, String),
StringFilterOperationOnNonStringSubject(String, String),

#[error(
"Tag \"{1}\" represents non-string field \"{2}\" (type \"{3}\"), but is used with @filter \
operation \"{0}\" which only supports strings."
"Filter operation \"{0}\" requires an argument of string type, but is being used \
with non-string {1}."
)]
StringFilterOperationOnNonStringTag(String, String, String, String),
StringFilterOperationOnNonStringArgument(String, String),

#[error(
"Non-list field \"{1}\" (type \"{2}\") used with @filter operation \"{0}\" which can \"
only be used on lists."
"Filter operation \"{0}\" can only be applied to list values, but is applied to {1} \
which is not a list."
)]
ListFilterOperationOnNonListField(String, String, String),
ListFilterOperationOnNonListSubject(String, String),

#[error(
"Tag \"{1}\" represents non-list field \"{2}\" (type \"{3}\"), but is used with @filter \
operation \"{0}\" which requires a list type."
"Filter operation \"{0}\" requires an argument of list type, but is being used \
with non-list {1}."
)]
ListFilterOperationOnNonListTag(String, String, String, String),
ListFilterOperationOnNonListArgument(String, String),
}

impl FilterTypeError {
fn represent_property_and_type(property_name: &str, property_type: &Type) -> String {
format!("property \"{property_name}\" of type \"{property_type}\"")
}

fn represent_tag_name_and_type(tag_name: &str, tag_type: &Type) -> String {
format!("tag \"{tag_name}\" of type \"{tag_type}\"")
}

pub(crate) fn non_nullable_property_with_nullability_filter(
filter_operator: &str,
property_name: &str,
property_type: &Type,
filter_outcome: bool,
) -> Self {
Self::NonNullableTypeFilteredForNullability(
filter_operator.to_string(),
Self::represent_property_and_type(property_name, property_type),
filter_outcome,
)
}

pub(crate) fn type_mismatch_between_property_and_tag(
filter_operator: &str,
property_name: &str,
property_type: &Type,
tag_name: &str,
tag_type: &Type,
) -> Self {
Self::TypeMismatchBetweenFilterSubjectAndArgument(
filter_operator.to_string(),
Self::represent_property_and_type(property_name, property_type),
Self::represent_tag_name_and_type(tag_name, tag_type),
)
}

pub(crate) fn non_orderable_property_with_ordering_filter(
filter_operator: &str,
property_name: &str,
property_type: &Type,
) -> Self {
Self::OrderingFilterOperationOnNonOrderableSubject(
filter_operator.to_string(),
Self::represent_property_and_type(property_name, property_type),
)
}

pub(crate) fn non_orderable_tag_argument_to_ordering_filter(
filter_operator: &str,
tag_name: &str,
tag_type: &Type,
) -> Self {
Self::OrderingFilterOperationWithNonOrderableArgument(
filter_operator.to_string(),
Self::represent_tag_name_and_type(tag_name, tag_type),
)
}

pub(crate) fn non_string_property_with_string_filter(
filter_operator: &str,
property_name: &str,
property_type: &Type,
) -> Self {
Self::StringFilterOperationOnNonStringSubject(
filter_operator.to_string(),
Self::represent_property_and_type(property_name, property_type),
)
}

pub(crate) fn non_string_tag_argument_to_string_filter(
filter_operator: &str,
tag_name: &str,
tag_type: &Type,
) -> Self {
Self::StringFilterOperationOnNonStringArgument(
filter_operator.to_string(),
Self::represent_tag_name_and_type(tag_name, tag_type),
)
}

pub(crate) fn non_list_property_with_list_filter(
filter_operator: &str,
property_name: &str,
property_type: &Type,
) -> Self {
Self::ListFilterOperationOnNonListSubject(
filter_operator.to_string(),
Self::represent_property_and_type(property_name, property_type),
)
}

pub(crate) fn non_list_tag_argument_to_list_filter(
filter_operator: &str,
tag_name: &str,
tag_type: &Type,
) -> Self {
Self::ListFilterOperationOnNonListArgument(
filter_operator.to_string(),
Self::represent_tag_name_and_type(tag_name, tag_type),
)
}
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
Expand Down
Loading