Skip to content

Commit

Permalink
Merge pull request #23 from hubot-scripts/improve-error-handling
Browse files Browse the repository at this point in the history
Improve error handling
  • Loading branch information
stephenyeargin authored May 27, 2022
2 parents 8b3a801 + 48268ff commit 84ec734
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 13 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ Then add **hubot-youtube** to your `external-scripts.json`:

### Obtain a [Google Developer Console](https://console.developers.google.com) token

_Ensure your Project has a billing account connected in order to query the API._

Enable the "YouTube Data API v3" permission from the API menu.

![Enable v3](https://cloud.githubusercontent.com/assets/80459/7863722/8161df38-0523-11e5-931a-5c2bf6d8105b.png)

Create a "Public" token rather than the OAuth credentials for this particular implementation.
Create a "Public" token rather than the OAuth credentials for this particular implementation.

![Get Public Token](https://cloud.githubusercontent.com/assets/80459/7600553/f2fa44c2-f8d1-11e4-8edf-009c0e3f04f1.png)

Expand Down Expand Up @@ -74,3 +76,9 @@ export HUBOT_YOUTUBE_DECODE_HTML=true
user1> hubot youtube no no no cat remix
hubot> http://www.youtube.com/watch?v=z7OJ3vDqyw8&feature=youtube_gdata
```

## Troubleshooting

### Quota Exceeded Error

The YouTube API now requires a billing account attached to your GCP project.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 10 additions & 11 deletions src/youtube.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,28 @@ module.exports = function (robot) {
key: process.env.HUBOT_YOUTUBE_API_KEY
})
.get()(function (err, res, body) {
let error, videos
robot.logger.debug(body)
let json, videos
if (err) {
robot.logger.error(err)
return robot.emit('error', err, msg)
}
try {
json = JSON.parse(body)
if (res.statusCode === 200) {
videos = JSON.parse(body)
robot.logger.debug(`Videos: ${JSON.stringify(videos)}`)
robot.logger.debug(`Videos: ${JSON.stringify(body)}`)
} else {
return robot.emit('error', `${res.statusCode}: ${body}`, msg)
return msg.send(json.error.message)
}
} catch (error1) {
error = error1
robot.logger.error(error)
return msg.send(`Error! ${body}`)
} catch (exception) {
robot.logger.error(exception)
return msg.send(`Error! ${exception}`)
}
if (videos.error) {
if (json.error) {
robot.logger.error(videos.error)
return msg.send(`Error! ${JSON.stringify(videos.error)}`)
return msg.send(`Error! ${JSON.stringify(json.error)}`)
}
videos = videos.items
videos = json.items
if ((videos == null) || !(videos.length > 0)) {
return msg.send(`No video results for \"${query}\"`)
}
Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/quota-exceeded.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"error": {
"code": 403,
"message": "The request cannot be completed because you have exceeded your \u003ca href=\"/youtube/v3/getting-started#quota\"\u003equota\u003c/a\u003e.",
"errors": [
{
"message": "The request cannot be completed because you have exceeded your \u003ca href=\"/youtube/v3/getting-started#quota\"\u003equota\u003c/a\u003e.",
"domain": "youtube.quota",
"reason": "quotaExceeded"
}
]
}
}
51 changes: 50 additions & 1 deletion test/youtube-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,55 @@ describe('youtube video title with decoded HTML', () => {
});
});

describe('youtube quota exceeded', () => {
var room = null;

beforeEach(() => {
process.env.HUBOT_LOG_LEVEL = 'error';
process.env.HUBOT_YOUTUBE_API_KEY = 'foobarbaz';
process.env.HUBOT_YOUTUBE_DETERMINISTIC_RESULTS = 'true';
process.env.HUBOT_YOUTUBE_DISPLAY_VIDEO_TITLE = 'true';
room = helper.createRoom();
nock.disableNetConnect();
this.robot = {
respond: sinon.spy(),
hear: sinon.spy()
};
return require('../src/youtube')(this.robot);
});

afterEach(() => {
delete process.env.HUBOT_LOG_LEVEL;
delete process.env.HUBOT_YOUTUBE_API_KEY;
delete process.env.HUBOT_YOUTUBE_DETERMINISTIC_RESULTS;
delete process.env.HUBOT_YOUTUBE_DISPLAY_VIDEO_TITLE;
room.destroy();
nock.cleanAll();
});

context('responds with an error message', () => {
beforeEach(function(done) {
nock('https://www.googleapis.com')
.get('/youtube/v3/search')
.query({
order: 'relevance',
part: 'snippet',
type: 'video',
maxResults: 1,
q: 'star wars',
key: 'foobarbaz'
})
.replyWithFile(403, __dirname +'/fixtures/quota-exceeded.json');
room.user.say('alice', 'hubot yt star wars');
return setTimeout(done, 100);
});

return it('should respond with error message', function() {
return expect(room.messages[1][1]).to.equal('The request cannot be completed because you have exceeded your \u003ca href=\"/youtube/v3/getting-started#quota\"\u003equota\u003c/a\u003e.');
});
});
});

describe('youtube missing configuration', () => {
var room = null;

Expand All @@ -292,7 +341,7 @@ describe('youtube missing configuration', () => {
nock.cleanAll();
});

context('retrieves top video with decoded title', () => {
context('responds with an error message', () => {
beforeEach(function(done) {
room.user.say('alice', 'hubot yt star wars');
return setTimeout(done, 100);
Expand Down

0 comments on commit 84ec734

Please sign in to comment.