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

[cdnjs gem] make exception classes more consistent #1683

Merged
merged 5 commits into from
May 15, 2018
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
6 changes: 3 additions & 3 deletions lib/error-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
};
Expand All @@ -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 });
}
};

Expand Down
2 changes: 1 addition & 1 deletion lib/request-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down
65 changes: 45 additions & 20 deletions services/errors.js
Original file line number Diff line number Diff line change
@@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions services/gem/gem.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down