Skip to content

Commit

Permalink
feat: use error constants (#5088) (#5100)
Browse files Browse the repository at this point in the history
Use defined error constants throughout codebase.
  • Loading branch information
souravdasslg authored and rarkins committed Jan 12, 2020
1 parent f0b8236 commit ee153e5
Show file tree
Hide file tree
Showing 63 changed files with 542 additions and 268 deletions.
11 changes: 8 additions & 3 deletions lib/config/presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import * as gitlab from '../datasource/gitlab';
import { RenovateConfig } from './common';
import { mergeChildConfig } from './utils';
import { regEx } from '../util/regex';
import {
CONFIG_VALIDATION,
DATASOURCE_FAILURE,
PLATFORM_FAILURE,
} from '../constants/error-messages';

const datasources = {
github,
Expand Down Expand Up @@ -166,12 +171,12 @@ export async function resolveConfigPresets(
logger.debug({ err }, 'Preset fetch error');
// istanbul ignore if
if (
err.message === 'platform-failure' ||
err.message === 'registry-failure'
err.message === PLATFORM_FAILURE ||
err.message === DATASOURCE_FAILURE
) {
throw err;
}
const error = new Error('config-validation');
const error = new Error(CONFIG_VALIDATION);
if (err.message === 'dep not found') {
error.validationError = `Cannot find preset's package (${preset})`;
} else if (err.message === 'preset renovate-config not found') {
Expand Down
47 changes: 47 additions & 0 deletions lib/constants/error-messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// System error
export const SYSTEM_INSUFFICIENT_DISK_SPACE = 'disk-space';

// Platform Error
export const PLATFORM_AUTHENTICATION_ERROR = 'authentication-error';
export const PLATFORM_BAD_CREDENTIALS = 'bad-credentials';
export const PLATFORM_FAILURE = 'platform-failure';
export const PLATFORM_INTEGRATION_UNAUTHORIZED = 'integration-unauthorized';
export const PLATFORM_NOT_FOUND = 'platform-not-found';
export const PLATFORM_RATE_LIMIT_EXCEEDED = 'rate-limit-exceeded';

// Config Error
export const CONFIG_VALIDATION = 'config-validation';

// Repository Error
export const REPOSITORY_ACCESS_FORBIDDEN = 'forbidden';
export const REPOSITORY_ARCHIVED = 'archived';
export const REPOSITORY_BLOCKED = 'blocked';
export const REPOSITORY_CANNOT_FORK = 'cannot-fork';
export const REPOSITORY_CHANGED = 'repository-changed';
export const REPOSITORY_DISABLED = 'disabled';
export const REPOSITORY_EMPTY = 'empty';
export const REPOSITORY_FORKED = 'fork';
export const REPOSITORY_MIRRORED = 'mirror';
export const REPOSITORY_NOT_FOUND = 'not-found';
export const REPOSITORY_NO_VULNERABILITY = 'no-vulnerability-alerts';
export const REPOSITORY_RENAMED = 'renamed';
export const REPOSITORY_TEMPORARY_ERROR = 'temporary-error';
export const REPOSITORY_UNINITIATED = 'uninitiated';

// Manager Error
export const MANAGER_LOCKFILE_ERROR = 'lockfile-error';
export const MANAGER_NO_PACKAGE_FILES = 'no-package-files';

// Datasource error
export const DATASOURCE_FAILURE = 'registry-failure';

// Worker Error
export const WORKER_FILE_UPDATE_FAILED = 'update-failure';

// Bundler Error
export const BUNDLER_COULD_NOT_RESOLVE = 'bundler-resolve';
export const BUNDLER_INVALID_CREDENTIALS = 'bundler-credentials';
export const BUNDLER_UNKNOWN_ERROR = 'bundler-unknown';

// Unknown Error
export const UNKNOWN_ERROR = 'unknown-error';
3 changes: 2 additions & 1 deletion lib/datasource/cargo/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { logger } from '../../logger';
import got from '../../util/got';
import { PkgReleaseConfig, ReleaseResult, Release } from '../common';
import { DATASOURCE_FAILURE } from '../../constants/error-messages';

export async function getPkgReleases({
lookupName,
Expand Down Expand Up @@ -101,7 +102,7 @@ export async function getPkgReleases({
(err.statusCode >= 500 && err.statusCode < 600)
) {
logger.warn({ lookupName, err }, `cargo crates.io registry failure`);
throw new Error('registry-failure');
throw new Error(DATASOURCE_FAILURE);
}
logger.warn(
{ err, lookupName },
Expand Down
3 changes: 2 additions & 1 deletion lib/datasource/dart/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import got from '../../util/got';
import { logger } from '../../logger';
import { ReleaseResult, PkgReleaseConfig } from '../common';
import { DATASOURCE_FAILURE } from '../../constants/error-messages';

export async function getPkgReleases({
lookupName,
Expand Down Expand Up @@ -34,7 +35,7 @@ export async function getPkgReleases({
(err.statusCode >= 500 && err.statusCode < 600)
) {
logger.warn({ lookupName, err }, `pub.dartlang.org registry failure`);
throw new Error('registry-failure');
throw new Error(DATASOURCE_FAILURE);
}
logger.warn(
{ err, lookupName },
Expand Down
23 changes: 12 additions & 11 deletions lib/datasource/docker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import got from '../../util/got';
import * as hostRules from '../../util/host-rules';
import { PkgReleaseConfig, ReleaseResult } from '../common';
import { GotResponse } from '../../platform';
import { DATASOURCE_FAILURE } from '../../constants/error-messages';

// TODO: add got typings when available
// TODO: replace www-authenticate with https://www.npmjs.com/package/auth-header ?
Expand Down Expand Up @@ -162,15 +163,15 @@ async function getAuthHeaders(
if (err.name === 'RequestError' && registry.endsWith('docker.io')) {
logger.debug({ err }, 'err');
logger.info('Docker registry error: RequestError');
throw new Error('registry-failure');
throw new Error(DATASOURCE_FAILURE);
}
if (err.statusCode === 429 && registry.endsWith('docker.io')) {
logger.warn({ err }, 'docker registry failure: too many requests');
throw new Error('registry-failure');
throw new Error(DATASOURCE_FAILURE);
}
if (err.statusCode >= 500 && err.statusCode < 600) {
logger.warn({ err }, 'docker registry failure: internal error');
throw new Error('registry-failure');
throw new Error(DATASOURCE_FAILURE);
}
logger.warn(
{ registry, dockerRepository: repository, err },
Expand Down Expand Up @@ -210,7 +211,7 @@ async function getManifestResponse(
});
return manifestResponse;
} catch (err) /* istanbul ignore next */ {
if (err.message === 'registry-failure') {
if (err.message === DATASOURCE_FAILURE) {
throw err;
}
if (err.statusCode === 401) {
Expand All @@ -235,7 +236,7 @@ async function getManifestResponse(
}
if (err.statusCode === 429 && registry.endsWith('docker.io')) {
logger.warn({ err }, 'docker registry failure: too many requests');
throw new Error('registry-failure');
throw new Error(DATASOURCE_FAILURE);
}
if (err.statusCode >= 500 && err.statusCode < 600) {
logger.info(
Expand All @@ -247,7 +248,7 @@ async function getManifestResponse(
},
'docker registry failure: internal error'
);
throw new Error('registry-failure');
throw new Error(DATASOURCE_FAILURE);
}
if (err.code === 'ETIMEDOUT') {
logger.info(
Expand Down Expand Up @@ -311,7 +312,7 @@ export async function getDigest(
await renovateCache.set(cacheNamespace, cacheKey, digest, cacheMinutes);
return digest;
} catch (err) /* istanbul ignore next */ {
if (err.message === 'registry-failure') {
if (err.message === DATASOURCE_FAILURE) {
throw err;
}
logger.info(
Expand Down Expand Up @@ -366,7 +367,7 @@ async function getTags(
await renovateCache.set(cacheNamespace, cacheKey, tags, cacheMinutes);
return tags;
} catch (err) /* istanbul ignore next */ {
if (err.message === 'registry-failure') {
if (err.message === DATASOURCE_FAILURE) {
throw err;
}
logger.debug(
Expand All @@ -393,14 +394,14 @@ async function getTags(
{ registry, dockerRepository: repository, err },
'docker registry failure: too many requests'
);
throw new Error('registry-failure');
throw new Error(DATASOURCE_FAILURE);
}
if (err.statusCode >= 500 && err.statusCode < 600) {
logger.warn(
{ registry, dockerRepository: repository, err },
'docker registry failure: internal error'
);
throw new Error('registry-failure');
throw new Error(DATASOURCE_FAILURE);
}
if (err.code === 'ETIMEDOUT') {
logger.info(
Expand Down Expand Up @@ -524,7 +525,7 @@ async function getLabels(
await renovateCache.set(cacheNamespace, cacheKey, labels, cacheMinutes);
return labels;
} catch (err) {
if (err.message === 'registry-failure') {
if (err.message === DATASOURCE_FAILURE) {
throw err;
}
if (err.statusCode === 401) {
Expand Down
3 changes: 2 additions & 1 deletion lib/datasource/gradle-version/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import is from '@sindresorhus/is';
import { logger } from '../../logger';
import got from '../../util/got';
import { PkgReleaseConfig, ReleaseResult, Release } from '../common';
import { DATASOURCE_FAILURE } from '../../constants/error-messages';

const GradleVersionsServiceUrl = 'https://services.gradle.org/versions/all';

Expand Down Expand Up @@ -48,7 +49,7 @@ export async function getPkgReleases({
if (!(err.statusCode === 404 || err.code === 'ENOTFOUND')) {
logger.warn({ err }, 'Gradle release lookup failure: Unknown error');
}
throw new Error('registry-failure');
throw new Error(DATASOURCE_FAILURE);
}
})
);
Expand Down
3 changes: 2 additions & 1 deletion lib/datasource/helm/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import yaml from 'js-yaml';
import { DATASOURCE_FAILURE } from '../../constants/error-messages';

import { PkgReleaseConfig, ReleaseResult } from '../common';
import got from '../../util/got';
Expand Down Expand Up @@ -35,7 +36,7 @@ export async function getRepositoryData(
(err.statusCode >= 500 && err.statusCode < 600)
) {
logger.warn({ err }, `${repository} server error`);
throw new Error('registry-failure');
throw new Error(DATASOURCE_FAILURE);
}
logger.warn({ err }, `${repository} lookup failure: Unknown error`);
return null;
Expand Down
3 changes: 2 additions & 1 deletion lib/datasource/hex/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { logger } from '../../logger';
import got from '../../util/got';
import { ReleaseResult, PkgReleaseConfig } from '../common';
import { DATASOURCE_FAILURE } from '../../constants/error-messages';

interface HexRelease {
html_url: string;
Expand Down Expand Up @@ -65,7 +66,7 @@ export async function getPkgReleases({
(err.statusCode >= 500 && err.statusCode < 600)
) {
logger.warn({ lookupName, err }, `hex.pm registry failure`);
throw new Error('registry-failure');
throw new Error(DATASOURCE_FAILURE);
}

if (err.statusCode === 401) {
Expand Down
3 changes: 2 additions & 1 deletion lib/datasource/maven/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import url from 'url';
import got from '../../util/got';
import { logger } from '../../logger';
import { DATASOURCE_FAILURE } from '../../constants/error-messages';

function isMavenCentral(pkgUrl: url.URL | string): boolean {
return (
Expand Down Expand Up @@ -70,7 +71,7 @@ export async function downloadHttpProtocol(
} else if (isTemporalError(err)) {
logger.info({ failedUrl, err }, 'Temporary error');
if (isMavenCentral(pkgUrl)) {
throw new Error('registry-failure');
throw new Error(DATASOURCE_FAILURE);
}
} else if (isConnectionError(err)) {
// istanbul ignore next
Expand Down
3 changes: 2 additions & 1 deletion lib/datasource/npm/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import got from '../../util/got';
import { maskToken } from '../../util/mask';
import { getNpmrc } from './npmrc';
import { Release, ReleaseResult } from '../common';
import { DATASOURCE_FAILURE } from '../../constants/error-messages';

let memcache = {};

Expand Down Expand Up @@ -234,7 +235,7 @@ export async function getDependency(
}
if (regUrl.startsWith('https://registry.npmjs.org')) {
logger.warn({ err, regUrl, depName: name }, 'npm registry failure');
throw new Error('registry-failure');
throw new Error(DATASOURCE_FAILURE);
}
// istanbul ignore next
return null;
Expand Down
3 changes: 2 additions & 1 deletion lib/datasource/packagist/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { logger } from '../../logger';
import got, { GotJSONOptions } from '../../util/got';
import * as hostRules from '../../util/host-rules';
import { PkgReleaseConfig, ReleaseResult } from '../common';
import { DATASOURCE_FAILURE } from '../../constants/error-messages';

function getHostOpts(url: string): GotJSONOptions {
const opts: GotJSONOptions = {
Expand Down Expand Up @@ -287,7 +288,7 @@ async function packageLookup(
err.host === 'packagist.org'
) {
logger.info('Packagist.org timeout');
throw new Error('registry-failure');
throw new Error(DATASOURCE_FAILURE);
}
logger.warn({ err, name }, 'packagist registry failure: Unknown error');
return null;
Expand Down
5 changes: 3 additions & 2 deletions lib/datasource/ruby-version/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { logger } from '../../logger';
import got from '../../util/got';
import { isVersion } from '../../versioning/ruby';
import { PkgReleaseConfig, ReleaseResult } from '../common';
import { DATASOURCE_FAILURE } from '../../constants/error-messages';

const rubyVersionsUrl = 'https://www.ruby-lang.org/en/downloads/releases/';

Expand Down Expand Up @@ -48,9 +49,9 @@ export async function getPkgReleases(
return res;
} catch (err) {
if (err && (err.statusCode === 404 || err.code === 'ENOTFOUND')) {
throw new Error('registry-failure');
throw new Error(DATASOURCE_FAILURE);
}
logger.warn({ err }, 'Ruby release lookup failure: Unknown error');
throw new Error('registry-failure');
throw new Error(DATASOURCE_FAILURE);
}
}
3 changes: 2 additions & 1 deletion lib/datasource/rubygems/get-rubygems-org.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import got from '../../util/got';
import { logger } from '../../logger';
import { ReleaseResult } from '../common';
import { DATASOURCE_FAILURE } from '../../constants/error-messages';

let lastSync = new Date('2000-01-01');
let packageReleases: Record<string, string[]> = Object.create(null); // Because we might need a "constructor" key
Expand All @@ -26,7 +27,7 @@ async function updateRubyGemsVersions(): Promise<void> {
logger.warn({ err }, 'Rubygems error - resetting cache');
contentLength = 0;
packageReleases = Object.create(null); // Because we might need a "constructor" key
throw new Error('registry-failure');
throw new Error(DATASOURCE_FAILURE);
}
logger.debug('Rubygems: No update');
lastSync = new Date();
Expand Down
3 changes: 2 additions & 1 deletion lib/datasource/rubygems/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { maskToken } from '../../util/mask';
import retriable from './retriable';
import { UNAUTHORIZED, FORBIDDEN, NOT_FOUND } from './errors';
import { ReleaseResult } from '../common';
import { DATASOURCE_FAILURE } from '../../constants/error-messages';

const INFO_PATH = '/api/v1/gems';
const VERSIONS_PATH = '/api/v1/versions';
Expand All @@ -29,7 +30,7 @@ const processError = ({ err, ...rest }): null => {
break;
default:
logger.debug(data, 'RubyGems lookup failure');
throw new Error('registry-failure');
throw new Error(DATASOURCE_FAILURE);
}
return null;
};
Expand Down
15 changes: 10 additions & 5 deletions lib/manager/bundler/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import {
} from '../../versioning/ruby';
import { UpdateArtifactsConfig, UpdateArtifactsResult } from '../common';
import { platform } from '../../platform';
import {
BUNDLER_COULD_NOT_RESOLVE,
BUNDLER_INVALID_CREDENTIALS,
BUNDLER_UNKNOWN_ERROR,
} from '../../constants/error-messages';

export async function updateArtifacts(
packageFileName: string,
Expand Down Expand Up @@ -149,8 +154,8 @@ export async function updateArtifacts(
{ err },
'Gemfile.lock update failed due to missing credentials'
);
global.repoCache.bundlerArtifactsError = 'bundler-credentials';
throw new Error('bundler-credentials');
global.repoCache.bundlerArtifactsError = BUNDLER_INVALID_CREDENTIALS;
throw new Error(BUNDLER_INVALID_CREDENTIALS);
}
const resolveMatchRe = new RegExp('\\s+(.*) was resolved to', 'g');
if (err.stderr && err.stderr.match(resolveMatchRe)) {
Expand Down Expand Up @@ -180,10 +185,10 @@ export async function updateArtifacts(
}
logger.warn({ err }, 'Cannot resolve bundler lock update error');
// Do not set global.repoCache because we don't want to stop trying other branches
throw new Error('bundler-resolve');
throw new Error(BUNDLER_COULD_NOT_RESOLVE);
}
logger.warn({ err }, 'Unknown bundler lock file update error');
global.repoCache.bundlerArtifactsError = 'bundler-unknown';
throw new Error('bundler-unknown');
global.repoCache.bundlerArtifactsError = BUNDLER_UNKNOWN_ERROR;
throw new Error(BUNDLER_UNKNOWN_ERROR);
}
}

0 comments on commit ee153e5

Please sign in to comment.