Skip to content
Snippets Groups Projects
Unverified Commit 0375422f authored by Sergei Zharinov's avatar Sergei Zharinov Committed by GitHub
Browse files

refactor(presets): Extract and de-duplicate error string constants (#9586)

parent 4b7295ee
Branches
No related tags found
No related merge requests found
...@@ -3,6 +3,11 @@ import { getName, mocked } from '../../../test/util'; ...@@ -3,6 +3,11 @@ import { getName, mocked } from '../../../test/util';
import presetIkatyang from './__fixtures__/renovate-config-ikatyang.json'; import presetIkatyang from './__fixtures__/renovate-config-ikatyang.json';
import * as _local from './local'; import * as _local from './local';
import * as _npm from './npm'; import * as _npm from './npm';
import {
PRESET_DEP_NOT_FOUND,
PRESET_NOT_FOUND,
PRESET_RENOVATE_CONFIG_NOT_FOUND,
} from './util';
import * as presets from '.'; import * as presets from '.';
jest.mock('./npm'); jest.mock('./npm');
...@@ -19,16 +24,16 @@ npm.getPreset = jest.fn(({ packageName, presetName }) => { ...@@ -19,16 +24,16 @@ npm.getPreset = jest.fn(({ packageName, presetName }) => {
][presetName]; ][presetName];
} }
if (packageName === 'renovate-config-notfound') { if (packageName === 'renovate-config-notfound') {
throw new Error('dep not found'); throw new Error(PRESET_DEP_NOT_FOUND);
} }
if (packageName === 'renovate-config-noconfig') { if (packageName === 'renovate-config-noconfig') {
throw new Error('preset renovate-config not found'); throw new Error(PRESET_RENOVATE_CONFIG_NOT_FOUND);
} }
if (packageName === 'renovate-config-throw') { if (packageName === 'renovate-config-throw') {
throw new Error('whoops'); throw new Error('whoops');
} }
if (packageName === 'renovate-config-wrongpreset') { if (packageName === 'renovate-config-wrongpreset') {
throw new Error('preset not found'); throw new Error(PRESET_NOT_FOUND);
} }
return null; return null;
}); });
......
...@@ -17,7 +17,13 @@ import * as internal from './internal'; ...@@ -17,7 +17,13 @@ import * as internal from './internal';
import * as local from './local'; import * as local from './local';
import * as npm from './npm'; import * as npm from './npm';
import type { PresetApi } from './types'; import type { PresetApi } from './types';
import { PRESET_DEP_NOT_FOUND } from './util'; import {
PRESET_DEP_NOT_FOUND,
PRESET_INVALID,
PRESET_NOT_FOUND,
PRESET_PROHIBITED_SUBPRESET,
PRESET_RENOVATE_CONFIG_NOT_FOUND,
} from './util';
const presetSources: Record<string, PresetApi> = { const presetSources: Record<string, PresetApi> = {
github, github,
...@@ -134,10 +140,10 @@ export function parsePreset(input: string): ParsedPreset { ...@@ -134,10 +140,10 @@ export function parsePreset(input: string): ParsedPreset {
// Validation // Validation
if (str.includes(':')) { if (str.includes(':')) {
throw new Error('prohibited sub-preset'); throw new Error(PRESET_PROHIBITED_SUBPRESET);
} }
if (!re.test(str)) { if (!re.test(str)) {
throw new Error('invalid preset'); throw new Error(PRESET_INVALID);
} }
[, packageName, presetPath, presetName] = re.exec(str); [, packageName, presetPath, presetName] = re.exec(str);
} else { } else {
...@@ -255,13 +261,13 @@ export async function resolveConfigPresets( ...@@ -255,13 +261,13 @@ export async function resolveConfigPresets(
const error = new Error(CONFIG_VALIDATION); const error = new Error(CONFIG_VALIDATION);
if (err.message === PRESET_DEP_NOT_FOUND) { if (err.message === PRESET_DEP_NOT_FOUND) {
error.validationError = `Cannot find preset's package (${preset})`; error.validationError = `Cannot find preset's package (${preset})`;
} else if (err.message === 'preset renovate-config not found') { } else if (err.message === PRESET_RENOVATE_CONFIG_NOT_FOUND) {
error.validationError = `Preset package is missing a renovate-config entry (${preset})`; error.validationError = `Preset package is missing a renovate-config entry (${preset})`;
} else if (err.message === 'preset not found') { } else if (err.message === PRESET_NOT_FOUND) {
error.validationError = `Preset name not found within published preset config (${preset})`; error.validationError = `Preset name not found within published preset config (${preset})`;
} else if (err.message === 'invalid preset') { } else if (err.message === PRESET_INVALID) {
error.validationError = `Preset is invalid (${preset})`; error.validationError = `Preset is invalid (${preset})`;
} else if (err.message === 'prohibited sub-preset') { } else if (err.message === PRESET_PROHIBITED_SUBPRESET) {
error.validationError = `Sub-presets cannot be combined with a custom path (${preset})`; error.validationError = `Sub-presets cannot be combined with a custom path (${preset})`;
} }
// istanbul ignore if // istanbul ignore if
......
...@@ -3,6 +3,11 @@ import { NpmResponse } from '../../../datasource/npm/types'; ...@@ -3,6 +3,11 @@ import { NpmResponse } from '../../../datasource/npm/types';
import { logger } from '../../../logger'; import { logger } from '../../../logger';
import { Http } from '../../../util/http'; import { Http } from '../../../util/http';
import type { Preset, PresetConfig } from '../types'; import type { Preset, PresetConfig } from '../types';
import {
PRESET_DEP_NOT_FOUND,
PRESET_NOT_FOUND,
PRESET_RENOVATE_CONFIG_NOT_FOUND,
} from '../util';
const id = 'npm'; const id = 'npm';
...@@ -19,10 +24,10 @@ export async function getPreset({ ...@@ -19,10 +24,10 @@ export async function getPreset({
.body; .body;
dep = body.versions[body['dist-tags'].latest]; dep = body.versions[body['dist-tags'].latest];
} catch (err) { } catch (err) {
throw new Error('dep not found'); throw new Error(PRESET_DEP_NOT_FOUND);
} }
if (!dep['renovate-config']) { if (!dep['renovate-config']) {
throw new Error('preset renovate-config not found'); throw new Error(PRESET_RENOVATE_CONFIG_NOT_FOUND);
} }
const presetConfig = dep['renovate-config'][presetName]; const presetConfig = dep['renovate-config'][presetName];
if (!presetConfig) { if (!presetConfig) {
...@@ -31,7 +36,7 @@ export async function getPreset({ ...@@ -31,7 +36,7 @@ export async function getPreset({
{ presetNames, presetName }, { presetNames, presetName },
'Preset not found within renovate-config' 'Preset not found within renovate-config'
); );
throw new Error('preset not found'); throw new Error(PRESET_NOT_FOUND);
} }
return presetConfig; return presetConfig;
} }
...@@ -3,8 +3,12 @@ import { ensureTrailingSlash } from '../../util/url'; ...@@ -3,8 +3,12 @@ import { ensureTrailingSlash } from '../../util/url';
import type { Preset } from './types'; import type { Preset } from './types';
export const PRESET_DEP_NOT_FOUND = 'dep not found'; export const PRESET_DEP_NOT_FOUND = 'dep not found';
export const PRESET_NOT_FOUND = 'preset not found'; export const PRESET_INVALID = 'invalid preset';
export const PRESET_INVALID_JSON = 'invalid preset JSON'; export const PRESET_INVALID_JSON = 'invalid preset JSON';
export const PRESET_NOT_FOUND = 'preset not found';
export const PRESET_PROHIBITED_SUBPRESET = 'prohibited sub-preset';
export const PRESET_RENOVATE_CONFIG_NOT_FOUND =
'preset renovate-config not found';
export type PresetFetcher = ( export type PresetFetcher = (
repo: string, repo: string,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment