Skip to content

Commit

Permalink
fix(apex): consider unknown failures unsuccessful (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekremney authored Jan 29, 2024
1 parent c719560 commit 2b27be9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 45 deletions.
20 changes: 9 additions & 11 deletions src/apex/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,16 @@ async function probeUrlConnection(baseUrl, log) {
try {
resp = await fetch(baseUrl, { redirect: 'manual' });
} catch (e) {
if (e.erroredSysCall === 'connect') {
log.info(`Request to ${baseUrl} fails due to a connection issue`, e);
return {
url: baseUrl,
success: false,
};
}
// failures for unknown reasons (ie bot detection) are not marked as 'failure' as intended
// such failures are logged as error to receive an alert about it for investigation
log.error(`Request to ${baseUrl} fails for an unknown reason`, e);
log.info(`Request to ${baseUrl} fails for an unknown reason. Code: ${e.code}`, e);
return {
url: baseUrl,
success: false,
};
}
return {
url: baseUrl,
success: true,
status: resp ? resp.status : 'unknown',
status: resp.status,
};
}

Expand All @@ -98,6 +93,7 @@ export default async function audit(message, context) {

const site = await retrieveSiteBySiteId(dataAccess, siteId, log);
if (!site) {
log.error(`No site with siteId "${siteId}" exists.`);
return notFound('Site not found');
}

Expand All @@ -113,6 +109,8 @@ export default async function audit(message, context) {

const url = stripUrl(baseURL);

log.info(`Audit result for ${baseURL}:\n${JSON.stringify(results, null, 2)}`);

await sqs.sendMessage(queueUrl, {
type,
url,
Expand Down
34 changes: 0 additions & 34 deletions test/audits/apex.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,40 +150,6 @@ describe('Apex audit', () => {
.calledWith(context.env.AUDIT_RESULTS_QUEUE_URL, expectedMessage);
});

it('unknown failures considered successfull', async () => {
const expectedMessage = {
type: 'apex',
url: 'some-domain.com',
auditContext: {},
auditResult: [
{
url: 'https://some-domain.com',
success: true,
status: 'unknown',
},
{
url: 'https://www.some-domain.com',
success: true,
status: 200,
},
],
};

nock('https://some-domain.com')
.get('/')
.replyWithError({ code: 'UNKNOWN', syscall: 'unknown' });

nock('https://www.some-domain.com')
.get('/')
.reply(200);

const resp = await apexAudit(messageBodyJson, context);
expect(resp.status).to.equal(204);
expect(context.sqs.sendMessage).to.have.been.calledOnce;
expect(context.sqs.sendMessage).to.have.been
.calledWith(context.env.AUDIT_RESULTS_QUEUE_URL, expectedMessage);
});

it('apex audit successful when baseurl resolves', async () => {
const expectedMessage = {
type: 'apex',
Expand Down

0 comments on commit 2b27be9

Please sign in to comment.