Skip to content

Commit

Permalink
feat: refine PositionAdNotAvailable errors (#93)
Browse files Browse the repository at this point in the history
* feat: refine PositionAdNotAvailable errors

* test: fix test

* test: fix integration test

Co-authored-by: Pablo Rey Sobral <pablo.rey@adevinta.com>
  • Loading branch information
paulusrex and Pablo Rey Sobral authored Aug 12, 2020
1 parent fd66073 commit 49de165
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/itest/openads/AddPositionUseCaseTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe('Add Position use case', function() {
expect(
error.name,
`AddPosition return a rejected Promise, in this case, we expect an error with name: 'PositionAdNotAvailableError', but we have received a ${error.name}`
).to.be.equals('PositionAdNotAvailableError')
).to.be.equals('PositionAdError')
expect(
error.position.id,
`AddPosition return a rejected Promise, in this case, we expect that the position id was: 'ad1', but we have received a position id ${error.position.id}`
Expand Down
6 changes: 3 additions & 3 deletions src/openads/application/service/AddPositionUseCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ export default class AddPositionUseCase {

_filterPositionAdIsAvailable(position) {
if (position.ad.status !== AD_AVAILABLE) {
if (position.ad.data && position.ad.data.errMessage) {
throw new PositionAdError({position})
} else {
if (position.ad.data && position.ad.data.nobid) {
throw new PositionAdNotAvailableError({position})
} else {
throw new PositionAdError({position})
}
}
return position
Expand Down
8 changes: 5 additions & 3 deletions src/openads/domain/position/PositionAdError.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ export default class PositionAdError extends Error {
constructor({position = {}} = {}) {
super()
this.name = 'PositionAdError'
this.message = `Error loading Ad ${position.id}: ${(position.ad &&
position.ad.data &&
position.ad.data.errMessage) ||
let msg = position.ad && position.ad.data && position.ad.data.errMessage
if (!msg && typeof position.ad.data === 'string') {
msg = position.ad.data
}
this.message = `Error loading Ad ${position.id}: ${msg ||
'unexpected error'}.`
this.stack = new Error().stack
this.position = position
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,13 @@ describe('Add Position use case', function() {
done(new Error('Should fail with position not available error'))
})
.catch(error => {
expect(error.name).to.equal('PositionAdNotAvailableError')
expect(error.name).to.equal('PositionAdError')
expect(error.position.ad.status).to.equal(AD_ERROR)
done()
})
})
it('should return the position with an unresolved Ad with AD_NO_BID status ad server returns a adNoBid response', function(done) {
const givenAd = {data: 'ad no bid', status: AD_NO_BID}
const givenAd = {data: {nobid: true}, status: AD_NO_BID}
const givenPositionRequest = {
id: 'ad-1',
name: 'TOP',
Expand Down

0 comments on commit 49de165

Please sign in to comment.