Skip to content

Commit

Permalink
Merge 43eaee7 into 8cbade4
Browse files Browse the repository at this point in the history
  • Loading branch information
waltherjj committed Mar 1, 2021
2 parents 8cbade4 + 43eaee7 commit 238531a
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/errors.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
class AuthError extends Error {
constructor (err) {
super(err.error_description)
super(err.message || err.error_description)
this.name = 'AuthError'
Object.assign(this, err)
}
}

class FetchError extends Error {
constructor (resp, body) {
super(resp.statusText)
super(resp.message || resp.statusText)
this.name = 'FetchError'
this.resp = resp
this.status = resp.status
Expand Down
2 changes: 1 addition & 1 deletion src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async function request (apiRoot, token, route, method, query = {}, options = {},
}).then(throwWhenStatusNotOk)
} catch (err) {
if (err.resp?.headers.get('WWW-Authenticate')) {
const authError = splitToObject(err.resp.headers.get('WWW-Authenticate').replace(/^Bearer /, ''))
const authError = Object.assign(err, splitToObject(err.resp.headers.get('WWW-Authenticate').replace(/^Bearer /, '')))
throw new AuthError(authError)
} else {
throw err
Expand Down
15 changes: 13 additions & 2 deletions tests/fetch.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,12 @@ export default async req => {
case '/404':
case '/api/404':
return {
body: '{}',
body: JSON.stringify({
hint: '404 error hint',
details: '404 error details',
code: '404 error code',
message: '404 error message'
}),
init: {
status: 404,
statusText: 'Not found',
Expand All @@ -160,9 +165,15 @@ export default async req => {
default:
if (req.headers.get('Authorization') === 'Bearer expired-token') {
return {
body: '',
body: JSON.stringify({
hint: '401 error hint',
details: '401 error details',
code: '401 error code',
message: '401 error message'
}),
init: {
status: 401,
statusText: 'Not authorized',
headers: {
'WWW-Authenticate': 'Bearer error="invalid_token", error_description="JWT expired"'
}
Expand Down
47 changes: 47 additions & 0 deletions tests/unit/Errors.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { AuthError, FetchError } from '@/errors'

describe('AuthError', () => {
it('uses message when set', () => {
try {
throw new AuthError({
message: 'message',
error_description: 'description'
})
} catch (e) {
expect(e.message).toBe('message')
}
})

it('uses error_description when message not set', () => {
try {
throw new AuthError({
error_description: 'description'
})
} catch (e) {
expect(e.message).toBe('description')
}
})
})

describe('FetchError', () => {
it('uses message when set', () => {
try {
throw new FetchError({
message: 'message',
statusText: 'status text'
})
} catch (e) {
expect(e.message).toBe('message')
}
})

it('uses statusText when message not set', () => {
try {
throw new FetchError({
statusText: 'status text'
})
} catch (e) {
expect(e.message).toBe('status text')
}
})
})
28 changes: 28 additions & 0 deletions tests/unit/Request.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import request from '@/request'
import GenericModel from '@/GenericModel'
import { FetchError, AuthError } from '@/errors'

describe('request method', () => {
beforeEach(() => {
Expand Down Expand Up @@ -34,6 +35,33 @@ describe('request method', () => {
}))
})

it('correctly throws FetchError with full error body', async () => {
expect.assertions(5)
try {
await request('/api', '', '404', 'GET', {})
} catch (e) {
expect(e instanceof FetchError).toBe(true)
expect(e.code).toBe('404 error code')
expect(e.hint).toBe('404 error hint')
expect(e.details).toBe('404 error details')
expect(e.message).toBe('404 error message')
}
})

it('correctly throws AuthError with full error body', async () => {
expect.assertions(5)
try {
await request('/autherror', 'expired-token', '', 'GET', {})
console.log(fetch.mock.calls[0][1].headers)
} catch (e) {
expect(e instanceof AuthError).toBe(true)
expect(e.code).toBe('401 error code')
expect(e.hint).toBe('401 error hint')
expect(e.details).toBe('401 error details')
expect(e.message).toBe('401 error message')
}
})

it('does not throw if query argument is undefined', async () => {
await expect(request('/api', '', 'clients', 'GET')).resolves.toBeTruthy()
})
Expand Down

0 comments on commit 238531a

Please sign in to comment.