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

Conversation

chris48s
Copy link
Member

As I've been working on the new service code, I've had a couple of thoughts on the exception classes:

#1590 (comment)
#1680 (comment)

I think its worth looking at this before we are using this code in too many places and it becomes difficult to change the constructor signatures. This PR proposes the following changes:

  • All exception classes now have the same signature. I find this confusing:
class InvalidResponse extends Error {
  constructor(prettyMessage = 'invalid', underlyingError) {
    // ...
  }
}

class Inaccessible extends Error {
  constructor(underlyingError, prettyMessage = 'inaccessible') {
    // ...
  }
}
  • Passing a props object means underlyingError and prettyMessage are always optional. This means we don't need to use workarounds like throw new InvalidResponse(undefined, err); or throw new InvalidResponse('invalid', new Error()); if we don't want to customise the error or don't have an exception to wrap.

  • Declare a base class which our runtime exceptions inherit from

does this seem like a reasonable approach?

@shields-ci
Copy link

shields-ci commented May 10, 2018

Warnings
⚠️

This PR modified helper functions in lib/ but not accompanying tests. That's okay so long as it's refactoring existing code.

Messages
📖

✨ Thanks for your contribution to Shields, @chris48s!

Generated by 🚫 dangerJS

@chris48s chris48s added the core Server, BaseService, GitHub auth, Shared helpers label May 11, 2018
Copy link
Member

@PyvesB PyvesB left a comment

Choose a reason for hiding this comment

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

Looks overall good to me! 👍

Minor observation: should we keep a more informative message for the InvalidResponses in gem.js ("version is null", "info is invalid")?

@chris48s
Copy link
Member Author

I was in 2 minds about whether it is useful to wrap a new Error() when we're not handling anything, but you're right that it allows us to provide a more detailed message.

Copy link
Member

@RedSparr0w RedSparr0w left a comment

Choose a reason for hiding this comment

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

Looks good to me!
Left a couple of notes on some things i wasn't sure about though.


constructor(props) {
props = props || {};
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.

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

@chris48s chris48s merged commit bf53e61 into badges:master May 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Server, BaseService, GitHub auth, Shared helpers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants