Skip to content

Commit

Permalink
throw exception when no tokens available
Browse files Browse the repository at this point in the history
  • Loading branch information
calebcartwright committed Oct 28, 2021
1 parent e331c2e commit 8c912ba
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
2 changes: 2 additions & 0 deletions core/base-service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Inaccessible,
InvalidParameter,
Deprecated,
ImproperlyConfigured,
} from './errors.js'

export {
Expand All @@ -29,5 +30,6 @@ export {
InvalidResponse,
Inaccessible,
InvalidParameter,
ImproperlyConfigured,
Deprecated,
}
6 changes: 4 additions & 2 deletions services/librariesio/librariesio-api-provider.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ImproperlyConfigured } from '../index.js'
import log from '../../core/server/log.js'
import { TokenPool } from '../../core/token-pooling/token-pool.js'
import { userAgent } from '../../core/base-service/legacy-request-handler.js'
Expand Down Expand Up @@ -71,9 +72,10 @@ export default class LibrariesIoApiProvider {
try {
token = this.standardTokens.next()
} catch (e) {
console.error('Unable to select next Libraries.io token from pool')
log.error(e)
return
throw new ImproperlyConfigured({
prettyMessage: 'Unable to select next Libraries.io token from pool',
})
}
tokenString = token.id
} else {
Expand Down
25 changes: 19 additions & 6 deletions services/librariesio/librariesio-api-provider.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { expect } from 'chai'
import sinon from 'sinon'
import { ImproperlyConfigured } from '../index.js'
import log from '../../core/server/log.js'
import LibrariesIoApiProvider from './librariesio-api-provider.js'

describe('LibrariesIoApiProvider', function () {
Expand All @@ -20,7 +22,7 @@ describe('LibrariesIoApiProvider', function () {
buffer: {},
}

let token, provider
let token, provider, nextTokenStub
beforeEach(function () {
provider = new LibrariesIoApiProvider({ baseUrl, tokens })

Expand All @@ -29,7 +31,7 @@ describe('LibrariesIoApiProvider', function () {
invalidate: sinon.spy(),
decrementedUsesRemaining: remaining - 1,
}
sinon.stub(provider.standardTokens, 'next').returns(token)
nextTokenStub = sinon.stub(provider.standardTokens, 'next').returns(token)
})

afterEach(function () {
Expand All @@ -43,6 +45,20 @@ describe('LibrariesIoApiProvider', function () {
await provider.fetch(mockRequest, '/npm/badge-maker')
expect(provider.standardTokens.next).to.have.been.calledOnce
})

it('should throw an error when the next token fails', async function () {
nextTokenStub.throws(Error)
sinon.stub(log, 'error')
try {
await provider.fetch(mockRequest, '/npm/badge-maker')
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(ImproperlyConfigured)
expect(e.prettyMessage).to.equal(
'Unable to select next Libraries.io token from pool'
)
}
})
})

context('a valid API response', function () {
Expand All @@ -55,10 +71,7 @@ describe('LibrariesIoApiProvider', function () {
})

it('should return the response', async function () {
const res = await provider.fetch(
mockRequest,
'/npm/badge-maker'
)
const res = await provider.fetch(mockRequest, '/npm/badge-maker')
expect(Object.is(res, mockResponse)).to.be.true
})

Expand Down

0 comments on commit 8c912ba

Please sign in to comment.