Skip to content

Commit

Permalink
fix: throw new errors
Browse files Browse the repository at this point in the history
  • Loading branch information
wopian committed Jul 6, 2017
1 parent e414a7d commit 5aaf154
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 53 deletions.
56 changes: 24 additions & 32 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,16 @@ export default class Kitsu {
* kitsu.whoAmI()
*/
whoAmI = async ({ compact } = false) => {
if (this.isAuth) {
try {
if (!this.isAuth) throw new Error('Not authenticated')
return (await this.get('users', compact ? {
filter: { self: true },
fields: { users: 'name' }
} : {
filter: { self: true }
})).data[0]
} else {
throw {
errors: [
{
title: 'Not Logged In',
detail: 'No user is logged in',
code: 'K01',
status: 'K01'
}
]
}
} catch (e) {
throw e
}
}

Expand Down Expand Up @@ -158,10 +150,10 @@ export default class Kitsu {

return { accessToken }
} else {
throw 'Missing required properties for authentication'
throw new Error('Missing required properties for authentication')
}
} catch (err) {
throw err
} catch (e) {
throw e
}
}

Expand Down Expand Up @@ -193,10 +185,10 @@ export default class Kitsu {
try {
// Handle response
let { body } = await r(`${apiUrl}/${apiVersion}/${kebab(model, '-')}${query(opts)}`, this._opts)
.catch(err => { throw JSON.parse(err.response.body) || err.response.body })
.catch(e => { throw JSON.parse(e.response.body) || e.response.body })
return deserialise(JSON.parse(body))
} catch (err) {
throw err
} catch (e) {
throw e
}
}

Expand Down Expand Up @@ -225,15 +217,15 @@ export default class Kitsu {
*/
post = async (model, data) => {
try {
if (!this.isAuth) throw 'Not authenticated'
if (!this.isAuth) throw new Error('Not authenticated')
// Handle request
const options = Object.assign({
body: JSON.stringify(serialise(model, data))
}, this._opts)
await r.post(`${apiUrl}/${apiVersion}/${kebab(model, '-')}`, options)
.catch(err => { throw JSON.parse(err.response.body) || err.response.body })
} catch (err) {
throw err
.catch(e => { throw JSON.parse(e.response.body) || e.response.body })
} catch (e) {
throw e
}
}

Expand All @@ -254,16 +246,16 @@ export default class Kitsu {
*/
patch = async (model, data) => {
try {
if (!this.isAuth) throw 'Not authenticated'
if (typeof data.id === 'undefined') throw 'PATCH request is missing a model ID'
if (!this.isAuth) throw new Error('Not authenticated')
if (typeof data.id === 'undefined') throw new Error('PATCH request is missing a model ID')
// Handle request
const options = Object.assign({
body: JSON.stringify(serialise(model, data, 'PATCH'))
}, this._opts)
await r.patch(`${apiUrl}/${apiVersion}/${kebab(model, '-')}/${data.id}`, options)
.catch(err => { throw JSON.parse(err.response.body) || err.response.body })
} catch (err) {
throw err
.catch(e => { throw JSON.parse(e.response.body) || e.response.body })
} catch (e) {
throw e
}
}

Expand All @@ -283,16 +275,16 @@ export default class Kitsu {
*/
remove = async (model, data) => {
try {
if (!this.isAuth) throw 'Not authenticated'
if (typeof data.id === 'undefined') throw 'PATCH request is missing a model ID'
if (!this.isAuth) throw new Error('Not authenticated')
if (typeof data.id === 'undefined') throw new Error('PATCH request is missing a model ID')
// Handle request
const options = Object.assign({
body: JSON.stringify(serialise(model, data, 'DELETE'))
}, this._opts)
await r.patch(`${apiUrl}/${apiVersion}/${kebab(model, '-')}/${data.id}`, options)
.catch(err => { throw JSON.parse(err.response.body) || err.response.body })
} catch (err) {
throw err
.catch(e => { throw JSON.parse(e.response.body) || e.response.body })
} catch (e) {
throw e
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/serialise.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export const linkRelationships = async (data, included) => {
}
}
delete data.relationships
} catch (err) {
throw (err)
} catch (e) {
throw e
}
}

Expand All @@ -63,14 +63,14 @@ export function serialise (model, obj = {}, method = 'POST') {
try {
// Check if obj is not an object or empty
if (obj.constructor !== Object && Object.keys(obj).length === 0) {
throw `${method} requires a JSON object body`
throw new Error(`${method} requires a JSON object body`)
}
const type = camel(model)
const data = { type }

// A POST request is the only request to not require an ID
if (method !== 'POST' && typeof obj.id === 'undefined') {
throw `${method} requires an ID for the ${type} type`
throw new Error(`${method} requires an ID for the ${type} type`)
}

// Add ID to data
Expand Down Expand Up @@ -98,8 +98,8 @@ export function serialise (model, obj = {}, method = 'POST') {
}
}
return { data }
} catch (err) {
throw err
} catch (e) {
throw e
}
}

Expand All @@ -124,7 +124,7 @@ export function deserialise (obj) {
delete obj.included

return obj
} catch (err) {
throw err
} catch (e) {
throw e
}
}
21 changes: 8 additions & 13 deletions test/unit/specs/auth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ describe('Authentication', () => {
describe('isAuth', () => {
it('should return false if not logged in', () => {
const kitsu = new Kitsu()

expect(kitsu.isAuth).toBe(false)
})

Expand All @@ -14,23 +13,19 @@ describe('Authentication', () => {
authorization: 'Bearer 1234567890'
}
})

expect(kitsu.isAuth).toBe(true)
})
})

describe('WhoAmI', () => {
it('should return an error if not logged in', () => {
const kitsu = new Kitsu()

expect(kitsu.whoAmI()).resolves.toEqual({
errors: [{
code: 'K01',
detail: 'No user is logged in',
status: 'K01',
title: 'Not Logged In'
}]
})
it('should throw an error if not logged in', async () => {
expect.assertions(1)
try {
const kitsu = new Kitsu()
await kitsu.whoAmI()
} catch (e) {
expect(e.message).toEqual('Not authenticated')
}
})
})
})

0 comments on commit 5aaf154

Please sign in to comment.