Skip to content

Commit

Permalink
fix(publishing): Quick fix to prevent crash when attemping to publish…
Browse files Browse the repository at this point in the history
… an undefined entity

For some reason sometimes the entities to publish array will contain an undefined object which will
cause the tool to crash, the hot fix for now is to prevent the crash and log the undefined entity.
  • Loading branch information
Khaledgarbaya committed Jun 24, 2016
1 parent e9caf74 commit 6fc2f87
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
13 changes: 11 additions & 2 deletions lib/push/publishing.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import errorBuffer from '../utils/error-buffer'
* to an error buffer instead.
*/
export function publishEntities (entities) {
return Promise.map(entities, (entity) => {
return Promise.map(entities, (entity, index) => {
if (!entity) {
errorBuffer.push({'publishEntitiesError': 'undefined entity: at index ' + index})
return Promise.resolve(entity)
}
return entity.publish()
.then((entity) => {
log.info(`Published ${entity.sys.type} ${getEntityName(entity)}`)
Expand All @@ -26,7 +30,12 @@ export function publishEntities (entities) {
* Returns a reject promise if unpublishing fails.
*/
export function unpublishEntities (entities) {
return Promise.map(entities, (entity) => {
return Promise.map(entities, (entity, index) => {
if (!entity) {
errorBuffer.push({'publishEntitiesError': 'undefined entity: at index ' + index})
return Promise.resolve(entity)
}

return entity.unpublish()
.then((entity) => {
log.info(`Unpublished ${entity.sys.type} ${getEntityName(entity)}`)
Expand Down
5 changes: 3 additions & 2 deletions test/push/publishing-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,12 @@ test('Fails to publish entities', (t) => {
}))
publishEntities([
{ sys: {id: '123'}, publish: publishStub },
undefined,
{ sys: {id: '456'}, publish: publishStub }
])
.then((errors) => {
t.equals(publishStub.callCount, 2, 'tries to publish assets')
t.equals(errorBufferMock.push.callCount, 2, 'logs 2 errors')
t.equals(errorBufferMock.push.callCount, 3, 'logs 3 errors')
teardown()
t.end()
})
Expand Down Expand Up @@ -113,7 +114,7 @@ test('Fails to unpublish entities because theyre already unpublished', (t) => {
setup()
const unpublishStub = sinon.stub().returns(Promise.reject({name: 'BadRequest'}))
unpublishEntities([
{ sys: {id: '123', type: 'Asset'}, unpublish: unpublishStub }
{ sys: {id: '123', type: 'Asset'}, undefined, unpublish: unpublishStub }
])
.then((entities) => {
t.equals(unpublishStub.callCount, 1, 'tries to unpublish assets')
Expand Down

0 comments on commit 6fc2f87

Please sign in to comment.