fix: Filter component - improve errors (#10456)

This commit is contained in:
Michael Kret
2024-08-19 19:01:33 +03:00
committed by GitHub
parent f784a4c95a
commit 61ac0c7775
10 changed files with 157 additions and 39 deletions

View File

@@ -94,14 +94,61 @@ function parseFilterConditionValues(
)} ${suffix}`;
};
const invalidTypeDescription = 'Try changing the type of comparison.';
const getTypeDescription = (isStrict: boolean) => {
if (isStrict)
return 'Try changing the type of the comparison, or enabling less strict type validation.';
return 'Try changing the type of the comparison.';
};
const composeInvalidTypeDescription = (
type: string,
fromType: string,
valuePosition: 'first' | 'second',
) => {
fromType = fromType.toLocaleLowerCase();
const expectedType = withIndefiniteArticle(type);
let convertionFunction = '';
if (type === 'string') {
convertionFunction = '.toString()';
} else if (type === 'number') {
convertionFunction = '.toNumber()';
} else if (type === 'boolean') {
convertionFunction = '.toBoolean()';
}
if (strict && convertionFunction) {
const suggestFunction = ` by adding <code>${convertionFunction}</code>`;
return `
<p>Try either:</p>
<ol>
<li>Enabling less strict type validation</li>
<li>Converting the ${valuePosition} field to ${expectedType}${suggestFunction}</li>
</ol>
`;
}
return getTypeDescription(strict);
};
if (!leftValid && !rightValid && typeof condition.leftValue === typeof condition.rightValue) {
return {
ok: false,
error: new FilterError(
`Comparison type expects ${withIndefiniteArticle(operator.type)} but both fields are ${withIndefiniteArticle(
typeof condition.leftValue,
)}`,
getTypeDescription(strict),
),
};
}
if (!leftValid) {
return {
ok: false,
error: new FilterError(
composeInvalidTypeMessage(operator.type, typeof condition.leftValue, leftValueString),
invalidTypeDescription,
composeInvalidTypeDescription(operator.type, typeof condition.leftValue, 'first'),
),
};
}
@@ -111,7 +158,7 @@ function parseFilterConditionValues(
ok: false,
error: new FilterError(
composeInvalidTypeMessage(rightType, typeof condition.rightValue, rightValueString),
invalidTypeDescription,
composeInvalidTypeDescription(rightType, typeof condition.rightValue, 'second'),
),
};
}