Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(presets): optimize error handling #14653

Merged
merged 5 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion lib/config/presets/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as _local from './local';
import * as _npm from './npm';
import {
PRESET_DEP_NOT_FOUND,
PRESET_INVALID_JSON,
PRESET_NOT_FOUND,
PRESET_RENOVATE_CONFIG_NOT_FOUND,
} from './util';
Expand Down Expand Up @@ -115,6 +116,22 @@ describe('config/presets/index', () => {
expect(e.validationMessage).toBeUndefined();
});

it('throws if invalid preset json', async () => {
config.foo = 1;
config.extends = ['org/repo'];
let e: Error;
local.getPreset.mockRejectedValueOnce(new Error(PRESET_INVALID_JSON));
try {
await presets.resolveConfigPresets(config);
} catch (err) {
e = err;
}
expect(e).toBeDefined();
expect(e.validationSource).toBeUndefined();
expect(e.validationError).toBe('Preset is invalid JSON (org/repo)');
expect(e.validationMessage).toBeUndefined();
});

it('throws noconfig', async () => {
config.foo = 1;
config.extends = ['noconfig:base'];
Expand Down Expand Up @@ -143,7 +160,9 @@ describe('config/presets/index', () => {
}
expect(e).toBeDefined();
expect(e.validationSource).toBeUndefined();
expect(e.validationError).toBeUndefined();
expect(e.validationError).toBe(
'Preset caused unexpected error (throw:base)'
);
expect(e.validationMessage).toBeUndefined();
});

Expand Down
5 changes: 5 additions & 0 deletions lib/config/presets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type { ParsedPreset, PresetApi } from './types';
import {
PRESET_DEP_NOT_FOUND,
PRESET_INVALID,
PRESET_INVALID_JSON,
PRESET_NOT_FOUND,
PRESET_PROHIBITED_SUBPRESET,
PRESET_RENOVATE_CONFIG_NOT_FOUND,
Expand Down Expand Up @@ -293,6 +294,10 @@ export async function resolveConfigPresets(
error.validationError = `Preset is invalid (${preset})`;
} else if (err.message === PRESET_PROHIBITED_SUBPRESET) {
error.validationError = `Sub-presets cannot be combined with a custom path (${preset})`;
} else if (err.message === PRESET_INVALID_JSON) {
error.validationError = `Preset is invalid JSON (${preset})`;
} else {
error.validationError = `Preset caused unexpected error (${preset})`;
}
// istanbul ignore if
if (existingPresets.length) {
Expand Down