1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

1-1334: show short error message when validation fails (#4617)

The error message only tells the user that the name doesn't match the
pattern. Because we already show the pattern above the input, we don't
need to repeat it in the error message. This makes for a shorter and
more concise message and better UX.

At the same time, for API users, we can keep the more detailed message
that includes info about the pattern, the example, and the description.


![image](https://github.com/Unleash/unleash/assets/17786332/0492f2ad-810d-435e-bfe6-785afee96892)
This commit is contained in:
Thomas Heartman 2023-09-06 12:20:10 +02:00 committed by GitHub
parent 1ffa0b86d9
commit f55c67fe2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 10 deletions

View File

@ -1,18 +1,21 @@
import BadDataError from './bad-data-error';
import { ApiErrorSchema } from './unleash-error';
import { ApiErrorSchema, UnleashError } from './unleash-error';
class PatternError extends BadDataError {
pattern: string;
class PatternError extends UnleashError {
statusCode = 400;
constructor(message: string, pattern: string) {
details?: { message: string }[];
constructor(message: string, details?: string[]) {
super(message);
this.pattern = pattern;
this.details = details?.map((description) => ({
message: description,
}));
}
toJSON(): ApiErrorSchema {
return {
...super.toJSON(),
pattern: this.pattern,
details: this.details,
};
}
}

View File

@ -1099,16 +1099,22 @@ class FeatureToggleService {
const project = await this.projectStore.get(projectId);
const namingPattern = project.featureNaming?.pattern;
const namingExample = project.featureNaming?.example;
const namingDescription = project.featureNaming?.description;
if (
namingPattern &&
!featureName.match(new RegExp(namingPattern))
) {
const error = `The feature flag name "${featureName}" does not match the project's naming pattern: "${namingPattern}.`;
const error = `The feature flag name "${featureName}" does not match the project's naming pattern: "${namingPattern}".`;
const example = namingExample
? ` Here's an example of a name that does match the pattern: "${namingExample}. Try something like that instead."`
? ` Here's an example of a name that does match the pattern: "${namingExample}"."`
: '';
throw new PatternError(`${error}${example}`, namingPattern);
const description = namingDescription
? ` The pattern's description is: "${namingDescription}"`
: '';
throw new PatternError(`${error}${example}${description}`, [
`The flag name does not match the pattern.`,
]);
}
}
}