Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TECH] Tracer le message d'erreur du CDN #166

Merged
merged 1 commit into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions run/services/cdn.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,25 @@ async function invalidateCdnCache(application) {
const namespaceKey = await _getNamespaceKey(application);
const urlForInvalidate = `${CDN_URL}/cache/invalidations`;

await axios.post(
urlForInvalidate,
{
patterns: ['.'],
},
{
headers: {
'X-Api-Key': config.baleen.pat,
'Content-type': 'application/json',
Cookie: `baleen-namespace=${namespaceKey}`,
try {
await axios.post(
urlForInvalidate,
{
patterns: ['.'],
},
}
);
{
headers: {
'X-Api-Key': config.baleen.pat,
'Content-type': 'application/json',
Cookie: `baleen-namespace=${namespaceKey}`,
},
}
);
} catch (error) {
const cdnResponseMessage = JSON.stringify(error.response.data);
const message = `Request failed with status code ${error.response.status} and message ${cdnResponseMessage}`;
throw new Error(message);
}

return `Cache CDN invalidé pour l‘application ${application}.`;
}
Expand Down
72 changes: 56 additions & 16 deletions test/integration/run/services/cdn_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,62 @@ describe('Integration | CDN', () => {
getAccountDetails.done();
});

it('should throw an error when namespace does not exist', async () => {
// given
const applicationName = 'Not_existing_application';

const namespace = 'Pix_Namespace';
const namespaceKey = 'namespace-key1';

_stubAccountDetails(namespace);
_stubInvalidationCachePost(namespaceKey);

// when
const result = await catchErr(cdn.invalidateCdnCache)(applicationName);

// then
expect(result).to.be.instanceOf(cdn.NamespaceNotFoundError);
expect(result.message).to.be.equal('Namespace for the application: Not_existing_application are not found');
context('when cache invalidation fails', function () {
context('when namespace does not exist', function () {
it('should throw an NamespaceNotFoundError error ', async () => {
// given
const applicationName = 'Not_existing_application';

const namespace = 'Pix_Namespace';
const namespaceKey = 'namespace-key1';

_stubAccountDetails(namespace);
_stubInvalidationCachePost(namespaceKey);

// when
const result = await catchErr(cdn.invalidateCdnCache)(applicationName);

// then
expect(result).to.be.instanceOf(cdn.NamespaceNotFoundError);
expect(result.message).to.be.equal('Namespace for the application: Not_existing_application are not found');
});
});

context('when API returns an error', function () {
it('should throw an error with statusCode and message', async () => {
// given
const applicationName = 'Pix_Test';
const namespace = 'Pix_Namespace';
const namespaceKey = 'namespace-key1';

_stubAccountDetails(namespace);

nock('https://console.baleen.cloud/api', {
reqheaders: {
'X-Api-Key': config.baleen.pat,
'Content-type': 'application/json',
Cookie: `baleen-namespace=${namespaceKey}`,
},
})
.post('/cache/invalidations', { patterns: ['.'] })
.reply(400, {
type: 'https://www.jhipster.tech/problem/problem-with-message',
title: 'Bad Request',
status: 400,
detail: 'JSON parse error: Unexpected character',
path: '/api/cache/invalidations',
message: 'error.http.400',
});

// when
const result = await catchErr(cdn.invalidateCdnCache)(applicationName);

// then
const expected =
'Request failed with status code 400 and message {"type":"https://www.jhipster.tech/problem/problem-with-message","title":"Bad Request","status":400,"detail":"JSON parse error: Unexpected character","path":"/api/cache/invalidations","message":"error.http.400"}';
expect(result.message).to.be.equal(expected);
});
});
});
});
});