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 3 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
64 changes: 45 additions & 19 deletions services/errors.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,60 @@
'use strict';

class NotFound extends Error {
constructor(prettyMessage = 'not found') {
class ShieldsRuntimeError extends Error {

get name() { return 'ShieldsRuntimeError'; }
get defaultPrettyMessage() { throw new Error('Must implement abstract method'); }

constructor(props, message) {
props = props || {};
super(message);
this.prettyMessage = props.prettyMessage || this.defaultPrettyMessage;
if (props.underlyingError) {
this.stack = props.underlyingError.stack;
}
}
}

class NotFound extends ShieldsRuntimeError {

get name() { return 'NotFound'; }
get defaultPrettyMessage() { return 'not found'; }

constructor(props) {
props = props || {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would we be better to remove this line and declare the default value in the header instead?

constructor(props = {}) {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 updated

const prettyMessage = props.prettyMessage || 'not found';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this line (& line below) be using this.defaultPrettyMessage in place of 'not found'

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be ideal, but there's a catch 22:

If we try to reference this before super(), error 'this' is not allowed before 'super()' is thrown (which makes sense), but we need to assemble message before we can call super().

Maybe an alternative would be to declare the default messages as constants outside the classes so we can say

const defaultNotFoundMessage = 'not found';

class NotFound extends ShieldsRuntimeError {

  get name() { return 'NotFound'; }
  get defaultPrettyMessage() { return defaultNotFoundMessage; }

  constructor(props = {}) {
    const prettyMessage = props.prettyMessage || defaultNotFoundMessage;
    //...
  }
}

do you think that is better?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah that makes sense, sounds good to me 👍
You can keep it how it is or change it, whichever makes the most sense for you.

const message = prettyMessage === 'not found'
? '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) {
props = 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) {
props = 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