From df0ac4a78ee5ad4443c0286b6d25c0b603bde026 Mon Sep 17 00:00:00 2001 From: Sergei Zharinov <zharinov@users.noreply.github.com> Date: Tue, 6 Apr 2021 17:49:06 +0400 Subject: [PATCH] refactor(presets): Reuse PRESET_INVALID_JSON variable (#9412) Co-authored-by: Rhys Arkins <rhys@arkins.net> --- lib/config/presets/bitbucket-server/index.spec.ts | 6 +++--- lib/config/presets/bitbucket-server/index.ts | 10 +++++++--- lib/config/presets/gitea/index.spec.ts | 6 +++--- lib/config/presets/gitea/index.ts | 8 ++++++-- lib/config/presets/github/index.spec.ts | 6 +++--- lib/config/presets/github/index.ts | 8 ++++++-- lib/config/presets/util.ts | 1 + 7 files changed, 29 insertions(+), 16 deletions(-) diff --git a/lib/config/presets/bitbucket-server/index.spec.ts b/lib/config/presets/bitbucket-server/index.spec.ts index afa5e7022a..443d964374 100644 --- a/lib/config/presets/bitbucket-server/index.spec.ts +++ b/lib/config/presets/bitbucket-server/index.spec.ts @@ -1,7 +1,7 @@ import * as httpMock from '../../../../test/http-mock'; import { getName, mocked } from '../../../../test/util'; import * as _hostRules from '../../../util/host-rules'; -import { PRESET_DEP_NOT_FOUND } from '../util'; +import { PRESET_DEP_NOT_FOUND, PRESET_INVALID_JSON } from '../util'; import * as bitbucketServer from '.'; jest.mock('../../../util/host-rules'); @@ -73,7 +73,7 @@ describe(getName(__filename), () => { 'some-filename.json', bitbucketApiHost ) - ).rejects.toThrow('invalid preset JSON'); + ).rejects.toThrow(PRESET_INVALID_JSON); expect(httpMock.getTrace()).toMatchSnapshot(); }); @@ -93,7 +93,7 @@ describe(getName(__filename), () => { 'some-filename.json', bitbucketApiHost ) - ).rejects.toThrow('invalid preset JSON'); + ).rejects.toThrow(PRESET_INVALID_JSON); expect(httpMock.getTrace()).toMatchSnapshot(); }); }); diff --git a/lib/config/presets/bitbucket-server/index.ts b/lib/config/presets/bitbucket-server/index.ts index 727d8246da..3dbe80cf46 100644 --- a/lib/config/presets/bitbucket-server/index.ts +++ b/lib/config/presets/bitbucket-server/index.ts @@ -6,7 +6,11 @@ import { setBaseUrl, } from '../../../util/http/bitbucket-server'; import type { Preset } from '../types'; -import { PRESET_DEP_NOT_FOUND, fetchPreset } from '../util'; +import { + PRESET_DEP_NOT_FOUND, + PRESET_INVALID_JSON, + fetchPreset, +} from '../util'; const http = new BitbucketServerHttp(); @@ -34,14 +38,14 @@ export async function fetchJSONFile( } if (!res.body.isLastPage) { logger.warn({ size: res.body.size }, 'Renovate config to big'); - throw new Error('invalid preset JSON'); + throw new Error(PRESET_INVALID_JSON); } try { const content = res.body.lines.map((l) => l.text).join(''); const parsed = JSON.parse(content); return parsed; } catch (err) { - throw new Error('invalid preset JSON'); + throw new Error(PRESET_INVALID_JSON); } } diff --git a/lib/config/presets/gitea/index.spec.ts b/lib/config/presets/gitea/index.spec.ts index ddab9e13ab..79baf2260e 100644 --- a/lib/config/presets/gitea/index.spec.ts +++ b/lib/config/presets/gitea/index.spec.ts @@ -2,7 +2,7 @@ import * as httpMock from '../../../../test/http-mock'; import { getName, mocked } from '../../../../test/util'; import * as _hostRules from '../../../util/host-rules'; import { setBaseUrl } from '../../../util/http/gitea'; -import { PRESET_NOT_FOUND } from '../util'; +import { PRESET_INVALID_JSON, PRESET_NOT_FOUND } from '../util'; import * as gitea from '.'; jest.mock('../../../util/host-rules'); @@ -63,7 +63,7 @@ describe(getName(__filename), () => { await expect( gitea.getPreset({ packageName: 'some/repo' }) - ).rejects.toThrow('invalid preset JSON'); + ).rejects.toThrow(PRESET_INVALID_JSON); expect(httpMock.getTrace()).toMatchSnapshot(); }); @@ -77,7 +77,7 @@ describe(getName(__filename), () => { await expect( gitea.getPreset({ packageName: 'some/repo' }) - ).rejects.toThrow('invalid preset JSON'); + ).rejects.toThrow(PRESET_INVALID_JSON); expect(httpMock.getTrace()).toMatchSnapshot(); }); diff --git a/lib/config/presets/gitea/index.ts b/lib/config/presets/gitea/index.ts index ae3229b721..3c4f560bb2 100644 --- a/lib/config/presets/gitea/index.ts +++ b/lib/config/presets/gitea/index.ts @@ -5,7 +5,11 @@ import { } from '../../../platform/gitea/gitea-helper'; import { ExternalHostError } from '../../../types/errors/external-host-error'; import type { Preset, PresetConfig } from '../types'; -import { PRESET_DEP_NOT_FOUND, fetchPreset } from '../util'; +import { + PRESET_DEP_NOT_FOUND, + PRESET_INVALID_JSON, + fetchPreset, +} from '../util'; export const Endpoint = 'https://gitea.com/api/v1/'; @@ -33,7 +37,7 @@ export async function fetchJSONFile( const parsed = JSON.parse(content); return parsed; } catch (err) { - throw new Error('invalid preset JSON'); + throw new Error(PRESET_INVALID_JSON); } } diff --git a/lib/config/presets/github/index.spec.ts b/lib/config/presets/github/index.spec.ts index 6de3ecc7b9..fe95e80d6f 100644 --- a/lib/config/presets/github/index.spec.ts +++ b/lib/config/presets/github/index.spec.ts @@ -1,7 +1,7 @@ import * as httpMock from '../../../../test/http-mock'; import { getName, mocked } from '../../../../test/util'; import * as _hostRules from '../../../util/host-rules'; -import { PRESET_NOT_FOUND } from '../util'; +import { PRESET_INVALID_JSON, PRESET_NOT_FOUND } from '../util'; import * as github from '.'; jest.mock('../../../util/host-rules'); @@ -61,7 +61,7 @@ describe(getName(__filename), () => { await expect( github.getPreset({ packageName: 'some/repo' }) - ).rejects.toThrow('invalid preset JSON'); + ).rejects.toThrow(PRESET_INVALID_JSON); expect(httpMock.getTrace()).toMatchSnapshot(); }); @@ -75,7 +75,7 @@ describe(getName(__filename), () => { await expect( github.getPreset({ packageName: 'some/repo' }) - ).rejects.toThrow('invalid preset JSON'); + ).rejects.toThrow(PRESET_INVALID_JSON); expect(httpMock.getTrace()).toMatchSnapshot(); }); diff --git a/lib/config/presets/github/index.ts b/lib/config/presets/github/index.ts index fef6d0f124..0f1a9be5cb 100644 --- a/lib/config/presets/github/index.ts +++ b/lib/config/presets/github/index.ts @@ -2,7 +2,11 @@ import { logger } from '../../../logger'; import { ExternalHostError } from '../../../types/errors/external-host-error'; import { GithubHttp } from '../../../util/http/github'; import type { Preset, PresetConfig } from '../types'; -import { PRESET_DEP_NOT_FOUND, fetchPreset } from '../util'; +import { + PRESET_DEP_NOT_FOUND, + PRESET_INVALID_JSON, + fetchPreset, +} from '../util'; export const Endpoint = 'https://api.github.com/'; @@ -33,7 +37,7 @@ export async function fetchJSONFile( const parsed = JSON.parse(content); return parsed; } catch (err) { - throw new Error('invalid preset JSON'); + throw new Error(PRESET_INVALID_JSON); } } diff --git a/lib/config/presets/util.ts b/lib/config/presets/util.ts index 12c0e88576..acbaad5bd8 100644 --- a/lib/config/presets/util.ts +++ b/lib/config/presets/util.ts @@ -4,6 +4,7 @@ import type { Preset } from './types'; export const PRESET_DEP_NOT_FOUND = 'dep not found'; export const PRESET_NOT_FOUND = 'preset not found'; +export const PRESET_INVALID_JSON = 'invalid preset JSON'; export type PresetFetcher = ( repo: string, -- GitLab