Skip to content
This repository was archived by the owner on Jul 26, 2022. It is now read-only.

Commit e6171c8

Browse files
authored
fix: status update conflicts should not cause crash, fixes #199 (#215)
1 parent 9d6c2f9 commit e6171c8

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

lib/poller.js

+17-9
Original file line numberDiff line numberDiff line change
@@ -159,17 +159,25 @@ class Poller {
159159
}
160160

161161
async _updateStatus (status) {
162-
this._logger.debug(`updating status for ${this._namespace}/${this._name} to: ${status}`)
163-
await this._status.put({
164-
body: {
165-
...this._externalSecret,
166-
status: {
167-
lastSync: `${new Date().toISOString()}`,
168-
observedGeneration: this._externalSecret.metadata.generation,
169-
status
162+
try {
163+
this._logger.debug(`updating status for ${this._namespace}/${this._name} to: ${status}`)
164+
await this._status.put({
165+
body: {
166+
...this._externalSecret,
167+
status: {
168+
lastSync: `${new Date().toISOString()}`,
169+
observedGeneration: this._externalSecret.metadata.generation,
170+
status
171+
}
170172
}
173+
})
174+
} catch (err) {
175+
if (err.statusCode !== 409) {
176+
this._logger.error(err, `failure while updating status for externalsecret ${this._namespace}/${this._name}`)
177+
throw err
171178
}
172-
})
179+
this._logger.info(`status update failed for externalsecret ${this._namespace}/${this._name}, due to modification, new poller should start`)
180+
}
173181
}
174182

175183
/**

lib/poller.test.js

+32
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,38 @@ describe('Poller', () => {
371371
})
372372
})
373373

374+
describe('_updateStatus', () => {
375+
it('handles 409 - Conflict', async () => {
376+
const conflictError = new Error('Conflict')
377+
conflictError.statusCode = 409
378+
externalSecretsApiMock.status.put.throws(conflictError)
379+
380+
const poller = pollerFactory()
381+
382+
await poller._updateStatus('SUCCESS')
383+
384+
expect(loggerMock.info.calledWith(`status update failed for externalsecret ${poller._namespace}/${poller._name}, due to modification, new poller should start`)).to.equal(true)
385+
})
386+
387+
it('rethrows other errors', async () => {
388+
const notFoundError = new Error('Not Found')
389+
notFoundError.statusCode = 404
390+
externalSecretsApiMock.status.put.throws(notFoundError)
391+
392+
const poller = pollerFactory()
393+
let error
394+
395+
try {
396+
await poller._updateStatus('SUCCESS')
397+
} catch (err) {
398+
error = err
399+
}
400+
401+
expect(error).to.not.equal(undefined)
402+
expect(error.message).equals('Not Found')
403+
})
404+
})
405+
374406
describe('_scheduleNextPoll', () => {
375407
let poller
376408
let clock

0 commit comments

Comments
 (0)