diff --git a/src/lib/error/pattern-error.ts b/src/lib/error/pattern-error.ts index 53da023a16..2f24f6e6d1 100644 --- a/src/lib/error/pattern-error.ts +++ b/src/lib/error/pattern-error.ts @@ -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, }; } } diff --git a/src/lib/services/feature-toggle-service.ts b/src/lib/services/feature-toggle-service.ts index 26828e0482..0c1c30667f 100644 --- a/src/lib/services/feature-toggle-service.ts +++ b/src/lib/services/feature-toggle-service.ts @@ -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.`, + ]); } } }