From bf53e612f5bbebd231f75e9899bdafe4a91aa098 Mon Sep 17 00:00:00 2001 From: chris48s Date: Tue, 15 May 2018 18:57:33 +0100 Subject: [PATCH] [cdnjs gem] make exception classes more consistent (#1683) make exception classes more consistent --- lib/error-helper.js | 6 ++-- lib/request-handler.js | 2 +- services/errors.js | 65 +++++++++++++++++++++++++++++------------- services/gem/gem.js | 4 +-- 4 files changed, 51 insertions(+), 26 deletions(-) diff --git a/lib/error-helper.js b/lib/error-helper.js index 8778e6fefc..1bb0b9c935 100644 --- a/lib/error-helper.js +++ b/lib/error-helper.js @@ -26,10 +26,10 @@ const checkErrorResponse = function(badgeData, err, res, notFoundMessage = 'not checkErrorResponse.asPromise = function ({ notFoundMessage } = {}) { return async function ({ buffer, res }) { if (res.statusCode === 404) { - throw new NotFound(notFoundMessage); + throw new NotFound({ prettyMessage: notFoundMessage }); } else if (res.statusCode !== 200) { const underlying = Error(`Got status code ${res.statusCode} (expected 200)`); - throw new InvalidResponse(undefined, underlying); + throw new InvalidResponse({ underlyingError: underlying}); } return { buffer, res }; }; @@ -39,7 +39,7 @@ async function asJson({ buffer, res }) { try { return JSON.parse(buffer); } catch (err) { - throw new InvalidResponse(undefined, err); + throw new InvalidResponse({ underlyingError: err }); } }; diff --git a/lib/request-handler.js b/lib/request-handler.js index eea93f353b..ae810741d4 100644 --- a/lib/request-handler.js +++ b/lib/request-handler.js @@ -176,7 +176,7 @@ function handleRequest (makeBadge, handlerOptions) { if (err) { // Wrap the error in an Inaccessible so it can be identified // by the BaseService handler. - reject(new Inaccessible(err)); + reject(new Inaccessible({ underlyingError: err })); } else { resolve({ res, buffer }); } diff --git a/services/errors.js b/services/errors.js index 8357509b83..61fc9de89a 100644 --- a/services/errors.js +++ b/services/errors.js @@ -1,34 +1,59 @@ 'use strict'; -class NotFound extends Error { - constructor(prettyMessage = 'not found') { - const message = prettyMessage === 'not found' +class ShieldsRuntimeError extends Error { + + get name() { return 'ShieldsRuntimeError'; } + get defaultPrettyMessage() { throw new Error('Must implement abstract method'); } + + constructor(props = {}, message) { + super(message); + this.prettyMessage = props.prettyMessage || this.defaultPrettyMessage; + if (props.underlyingError) { + this.stack = props.underlyingError.stack; + } + } +} + + +const defaultNotFoundError = 'not found'; + +class NotFound extends ShieldsRuntimeError { + + get name() { return 'NotFound'; } + get defaultPrettyMessage() { return defaultNotFoundError; } + + constructor(props = {}) { + const prettyMessage = props.prettyMessage || defaultNotFoundError; + const message = prettyMessage === defaultNotFoundError ? 'Not Found' : `Not Found: ${prettyMessage}`; - super(message); - this.prettyMessage = prettyMessage; - this.name = 'NotFound'; + super(props, message); } } -class InvalidResponse extends Error { - constructor(prettyMessage = 'invalid', underlyingError) { - const message = underlyingError - ? `Invalid Response: ${underlyingError.message}` +class InvalidResponse extends ShieldsRuntimeError { + + get name() { return 'InvalidResponse'; } + get defaultPrettyMessage() { return 'invalid'; } + + constructor(props = {}) { + const message = props.underlyingError + ? `Invalid Response: ${props.underlyingError.message}` : 'Invalid Response'; - super(message); - this.stack = underlyingError.stack; - this.prettyMessage = prettyMessage; - this.name = 'InvalidResponse'; + super(props, message); } } -class Inaccessible extends Error { - constructor(underlyingError, prettyMessage = 'inaccessible') { - super(`Inaccessible: ${underlyingError.message}`); - this.stack = underlyingError.stack; - this.prettyMessage = prettyMessage; - this.name = 'Inaccessible'; +class Inaccessible extends ShieldsRuntimeError { + + get name() { return 'Inaccessible'; } + get defaultPrettyMessage() { return 'inaccessible'; } + + constructor(props = {}) { + const message = props.underlyingError + ? `Inaccessible: ${props.underlyingError.message}` + : 'Inaccessible'; + super(props, message); } } diff --git a/services/gem/gem.js b/services/gem/gem.js index f5d617ce17..434239e9b1 100644 --- a/services/gem/gem.js +++ b/services/gem/gem.js @@ -129,11 +129,11 @@ class GemDownloads extends BaseService { downloads = metric(versionData.downloads_count); } else { - throw new InvalidResponse('invalid', new Error('version is null')); + throw new InvalidResponse({ underlyingError: new Error('version is null') }); } } else { - throw new InvalidResponse('invalid', new Error('info is invalid')); + throw new InvalidResponse({ underlyingError: new Error('info is invalid') }); } return {