Skip to content

Commit

Permalink
refactor(errors): massaging validation messages before we assign it (#…
Browse files Browse the repository at this point in the history
…25452)

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
PhilipAbed and viceice committed Oct 31, 2023
1 parent bdabe43 commit bece5a1
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 21 deletions.
3 changes: 2 additions & 1 deletion lib/config/migrations/custom/host-rules-migration.ts
Expand Up @@ -77,7 +77,8 @@ function validateHostRule(rule: LegacyHostRule & HostRule): void {
if (distinctHostValues.size > 1) {
const error = new Error(CONFIG_VALIDATION);
error.validationSource = 'config';
error.validationMessage = `hostRules cannot contain more than one host-matching field - use "matchHost" only.`;
error.validationMessage =
'`hostRules` cannot contain more than one host-matching field - use `matchHost` only.';
error.validationError =
'The renovate configuration file contains some invalid settings';
throw error;
Expand Down
2 changes: 1 addition & 1 deletion lib/config/secrets.ts
Expand Up @@ -66,7 +66,7 @@ function replaceSecretsInString(
const error = new Error(CONFIG_VALIDATION);
error.validationSource = 'config';
error.validationError = 'Disallowed secret substitution';
error.validationMessage = `The field ${key} may not use secret substitution`;
error.validationMessage = `The field \`${key}\` may not use secret substitution`;
throw error;
}
return value.replace(secretTemplateRegex, (_, secretName) => {
Expand Down
11 changes: 6 additions & 5 deletions lib/util/git/error.ts
Expand Up @@ -63,7 +63,7 @@ export function checkForPlatformFailure(err: Error): Error | null {
logger.debug({ err }, 'Converting git error to CONFIG_VALIDATION error');
const res = new Error(CONFIG_VALIDATION);
res.validationError = message;
res.validationMessage = err.message;
res.validationMessage = `\`${err.message.replaceAll('`', "'")}\``;
return res;
}
}
Expand All @@ -82,7 +82,7 @@ export function handleCommitError(
const error = new Error(CONFIG_VALIDATION);
error.validationSource = 'None';
error.validationError = 'An existing branch is blocking Renovate';
error.validationMessage = `Renovate needs to create the branch "${branchName}" but is blocked from doing so because of an existing branch called "renovate". Please remove it so that Renovate can proceed.`;
error.validationMessage = `Renovate needs to create the branch \`${branchName}\` but is blocked from doing so because of an existing branch called \`renovate\`. Please remove it so that Renovate can proceed.`;
throw error;
}
if (
Expand Down Expand Up @@ -114,9 +114,10 @@ export function handleCommitError(
const error = new Error(CONFIG_VALIDATION);
error.validationSource = branchName;
error.validationError = 'Bitbucket committer error';
error.validationMessage = `Renovate has experienced the following error when attempting to push its branch to the server: "${String(
err.message
)}"`;
error.validationMessage = `Renovate has experienced the following error when attempting to push its branch to the server: \`${err.message.replaceAll(
'`',
"'"
)}\``;
throw error;
}
if (err.message.includes('remote: error: cannot lock ref')) {
Expand Down
2 changes: 1 addition & 1 deletion lib/util/git/index.ts
Expand Up @@ -286,7 +286,7 @@ export function setGitAuthor(gitAuthor: string | undefined): void {
const error = new Error(CONFIG_VALIDATION);
error.validationSource = 'None';
error.validationError = 'Invalid gitAuthor';
error.validationMessage = `gitAuthor is not parsed as valid RFC5322 format: ${gitAuthor!}`;
error.validationMessage = `\`gitAuthor\` is not parsed as valid RFC5322 format: \`${gitAuthor!}\``;
throw error;
}
config.gitAuthorName = gitAuthorParsed.name;
Expand Down
4 changes: 2 additions & 2 deletions lib/util/package-rules/index.spec.ts
Expand Up @@ -732,8 +732,8 @@ describe('util/package-rules/index', () => {
}

expect(error).toStrictEqual(new Error(MISSING_API_CREDENTIALS));
expect(error.validationMessage).toBe('Missing credentials');
expect(error.validationError).toBe(
expect(error.validationError).toBe('Missing credentials');
expect(error.validationMessage).toBe(
'The `matchConfidence` matcher in `packageRules` requires authentication. Please refer to the [documentation](https://docs.renovatebot.com/configuration-options/#matchconfidence) and add the required host rule.'
);
});
Expand Down
5 changes: 3 additions & 2 deletions lib/util/package-rules/merge-confidence.ts
Expand Up @@ -18,8 +18,9 @@ export class MergeConfidenceMatcher extends Matcher {
*/
if (is.undefined(getApiToken())) {
const error = new Error(MISSING_API_CREDENTIALS);
error.validationMessage = 'Missing credentials';
error.validationError =
error.validationSource = 'MatchConfidence Authenticator';
error.validationError = 'Missing credentials';
error.validationMessage =
'The `matchConfidence` matcher in `packageRules` requires authentication. Please refer to the [documentation](https://docs.renovatebot.com/configuration-options/#matchconfidence) and add the required host rule.';
throw error;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/workers/repository/error-config.spec.ts
Expand Up @@ -34,7 +34,7 @@ describe('workers/repository/error-config', () => {
Location: \`package.json\`
Error type: some-error
Message: \`some-message\`
Message: some-message
`;
const error = new Error(CONFIG_VALIDATION);
error.validationSource = 'package.json';
Expand Down
6 changes: 1 addition & 5 deletions lib/workers/repository/error-config.ts
Expand Up @@ -3,7 +3,6 @@ import { GlobalConfig } from '../../config/global';
import type { RenovateConfig } from '../../config/types';
import { logger } from '../../logger';
import { Pr, platform } from '../../modules/platform';
import { regEx } from '../../util/regex';

export function raiseConfigWarningIssue(
config: RenovateConfig,
Expand Down Expand Up @@ -42,10 +41,7 @@ async function raiseWarningIssue(
body += `Error type: ${error.validationError}\n`;
}
if (error.validationMessage) {
body += `Message: \`${error.validationMessage.replace(
regEx(/`/g),
"'"
)}\`\n`;
body += `Message: ${error.validationMessage}\n`;
}

const pr = await platform.getBranchPr(
Expand Down
10 changes: 8 additions & 2 deletions lib/workers/repository/init/merge.ts
Expand Up @@ -140,7 +140,10 @@ export async function detectRepoFileConfig(): Promise<RepoFileConfig> {
'Error parsing renovate config renovate.json5'
);
const validationError = 'Invalid JSON5 (parsing failed)';
const validationMessage = `JSON5.parse error: ${String(err.message)}`;
const validationMessage = `JSON5.parse error: \`${err.message.replaceAll(
'`',
"'"
)}\``;
return {
configFileName,
configFileParseError: { validationError, validationMessage },
Expand Down Expand Up @@ -181,7 +184,10 @@ export async function detectRepoFileConfig(): Promise<RepoFileConfig> {
'Error parsing renovate config'
);
const validationError = 'Invalid JSON (parsing failed)';
const validationMessage = `JSON.parse error: ${String(err.message)}`;
const validationMessage = `JSON.parse error: \`${err.message.replaceAll(
'`',
"'"
)}\``;
return {
configFileName,
configFileParseError: { validationError, validationMessage },
Expand Down
2 changes: 1 addition & 1 deletion lib/workers/repository/process/index.ts
Expand Up @@ -56,7 +56,7 @@ async function getBaseBranchConfig(
const error = new Error(CONFIG_VALIDATION);
error.validationSource = 'config';
error.validationError = 'Error fetching config file';
error.validationMessage = `Error fetching config file ${configFileName} from branch ${baseBranch}`;
error.validationMessage = `Error fetching config file \`${configFileName}\` from branch \`${baseBranch}\``;
throw error;
}

Expand Down

0 comments on commit bece5a1

Please sign in to comment.