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

Adds error handling for group name length #6

Merged
merged 3 commits into from
Oct 31, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
10,853 changes: 7,682 additions & 3,171 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
"test-only": "NODE_ENV=test_local pkg-test-only"
},
"devDependencies": {
"@mishguru/package": "^4.3.1",
"@mishguru/package": "^5.1.0",
"nock": "^9.4.1"
},
"dependencies": {
"es6-error": "^4.1.1",
"json-fetch": "^7.5.1",
"node-fetch": "^2.1.2",
"query-string": "^6.1.0",
Expand Down
18 changes: 18 additions & 0 deletions src/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import ExtendableError from 'es6-error'

class GroupNameCharLimitExceeded extends ExtendableError {
constructor () {
super('GeniusLink group name is longer than the max character limit of 20 characters')
}
}

class AddLinkToGroupFailed extends ExtendableError {
constructor () {
super('Genius API could not generate a code, but still came back with 200')
}
}

export {
GroupNameCharLimitExceeded,
AddLinkToGroupFailed
}
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* @flow */
// @flow

import initGeniusLink from './initGeniusLink'
import addGroup from './requests/addGroup'
Expand Down
2 changes: 1 addition & 1 deletion src/initGeniusLink.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* @flow */
// @flow

import { setApiKeys } from './request'

Expand Down
2 changes: 1 addition & 1 deletion src/initGeniusLink.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* @flow */
// @flow

import test from 'ava'
import nock from 'nock'
Expand Down
2 changes: 1 addition & 1 deletion src/request.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* @flow */
// @flow

import fetch, { retriers } from 'json-fetch'
import qs from 'query-string'
Expand Down
6 changes: 5 additions & 1 deletion src/requests/addGroup.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
/* @flow */
// @flow

import { makeGetRequest } from '../request'
import { GroupNameCharLimitExceeded } from '../errors'

const PATH = '/v1/groups/add'

const addGroup = async (name: string) => {
if (name.length > 20) {
throw new GroupNameCharLimitExceeded()
}
const res = await makeGetRequest(PATH, {
GroupName: name
})
Expand Down
11 changes: 10 additions & 1 deletion src/requests/addGroup.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* @flow */
// @flow

import test from 'ava'
import nock from 'nock'
import { GroupNameCharLimitExceeded } from '../errors'

import addGroup from './addGroup'

Expand Down Expand Up @@ -32,3 +33,11 @@ test(`create a new tracked link`, async (t) => {

t.is(newGroupId, NEW_GROUP_ID)
})

test(`GroupNameCharLimitExceeded is thrown when the group name is too long`, async (t) => {
const GROUP_NAME = 'hereisareallyreallylonggroupname'

const error = await t.throwsAsync(addGroup(GROUP_NAME))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const error = await t.throwsAsync(addGroup(GROUP_NAME))
const error = await t.throwsAsync(
addGroup(GROUP_NAME),
GroupNameCharLimitExceeded
)

Copy link
Contributor

Choose a reason for hiding this comment

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

You can pass an error class as the second argument to throwsAsync

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure sure


t.truthy(error instanceof GroupNameCharLimitExceeded)
})
7 changes: 3 additions & 4 deletions src/requests/addLinkToGroup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* @flow */
// @flow

import { makePostRequest } from '../request'
import { AddLinkToGroupFailed } from '../errors'

const PATH = '/v1/links/add'

Expand All @@ -14,9 +15,7 @@ const addLinkToGroup = async (url: string, TSID: string | number) => {
const [ linkResponse ] = res.body.LinkResponses

if (linkResponse.ErrorMessage && linkResponse.ErrorMessage !== '') {
const error = new Error('Genius API could not generate a code, but still came back with 200')
error.originalMessage = linkResponse.ErrorMessage
throw error
throw new AddLinkToGroupFailed()
}

return linkResponse.NewLink.ShortUrlCode
Expand Down
7 changes: 4 additions & 3 deletions src/requests/addLinkToGroup.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* @flow */

import test from 'ava'
import nock from 'nock'

import { AddLinkToGroupFailed } from '../errors'

import addLinkToGroup from './addLinkToGroup'

test(`create a new tracked link`, async (t) => {
Expand Down Expand Up @@ -37,5 +37,6 @@ test('If genius replies with 200 but LinkResponses contains an error message, th
}]
})

await t.throws(addLinkToGroup(LINK, GROUP_ID), 'Genius API could not generate a code, but still came back with 200')
const error = await t.throwsAsync(addLinkToGroup(LINK, GROUP_ID))
t.truthy(error instanceof AddLinkToGroupFailed)
Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest you use t.true here -- as instanceof returns a boolean.

But in this case, I would also suggest passing AddLinkToGroupFailed directly to throwsAsync as the second argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok thanks!

})
2 changes: 1 addition & 1 deletion src/requests/getGroupDetails.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* @flow */
// @flow

import { makeGetRequest } from '../request'

Expand Down
2 changes: 1 addition & 1 deletion src/requests/getGroupDetails.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* @flow */
// @flow

import test from 'ava'
import nock from 'nock'
Expand Down
2 changes: 1 addition & 1 deletion src/requests/getTotalLinkClicks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* @flow */
// @flow
import { pathOr } from 'ramda'

import { makeGetRequest } from '../request'
Expand Down
2 changes: 1 addition & 1 deletion src/requests/getTotalLinkClicks.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* @flow */
// @flow

import test from 'ava'
import nock from 'nock'
Expand Down