diff --git a/api/src/shared/infrastructure/lcms.js b/api/src/shared/infrastructure/lcms.js index cce7a0a7b81..09cd074a30a 100644 --- a/api/src/shared/infrastructure/lcms.js +++ b/api/src/shared/infrastructure/lcms.js @@ -35,6 +35,12 @@ const createRelease = async function () { url: lcmsConfig.url + '/releases', headers: { Authorization: `Bearer ${lcmsConfig.apiKey}` }, }); + + if (!response.isSuccessful) { + logger.error(`An error occurred while creating a release on ${lcmsConfig.url}: ${JSON.stringify(response)}`); + throw new Error(`An error occurred while creating a release on ${lcmsConfig.url}`); + } + return response.data.content; }; diff --git a/api/tests/shared/unit/infrastructure/lcms_test.js b/api/tests/shared/unit/infrastructure/lcms_test.js index cd23f8106f6..53737f043c0 100644 --- a/api/tests/shared/unit/infrastructure/lcms_test.js +++ b/api/tests/shared/unit/infrastructure/lcms_test.js @@ -77,5 +77,19 @@ describe('Unit | Infrastructure | LCMS', function () { // then expect(response).to.deep.equal(learningContent); }); + + it('throws when LCMS Api response is not successful', async function () { + // given + nock('https://lcms-test.pix.fr/api') + .post('/releases') + .matchHeader('Authorization', 'Bearer test-api-key') + .reply(403); + + // when + const error = await catchErr(lcms.createRelease)(); + + // then + expect(error.message).to.deep.equal('An error occurred while creating a release on https://lcms-test.pix.fr/api'); + }); }); });