Skip to content

Commit

Permalink
fix(core/presentation): Handle empty message in validation message fu…
Browse files Browse the repository at this point in the history
…nctions, e.g., `errorMessage(undefined)`
  • Loading branch information
christopherthielen committed Oct 10, 2019
1 parent a096f8d commit aad69b0
Showing 1 changed file with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ const inverseLabels: { [label: string]: IValidationCategory } = Object.keys(cate
{},
);

const buildCategoryMessage = (type: IValidationCategory) => (message: string) => `${categoryLabels[type]}: ${message}`;
const buildCategoryMessage = (type: IValidationCategory) => (message: string) => {
return message ? `${categoryLabels[type]}: ${message}` : null;
};

export const asyncMessage = buildCategoryMessage('async');
export const errorMessage = buildCategoryMessage('error');
Expand All @@ -42,6 +44,10 @@ const validationMessageRegexp = new RegExp(`^(${labels.join('|')}): (.*)$`);
// Example: "Error: there was an error" => ['error', 'there was an error']
// Example: "this message has no explicit category" => ['error', 'this message has no explicit category']
export const categorizeValidationMessage = (validationMessage: string): [IValidationCategory, string] => {
if (!validationMessage) {
return [null, null];
}

const result = validationMessageRegexp.exec(validationMessage);
if (!result) {
// If no known category label was found embedded in the error message, default the category to 'error'
Expand Down

0 comments on commit aad69b0

Please sign in to comment.