Skip to content
This repository has been archived by the owner on Feb 28, 2022. It is now read-only.

Commit

Permalink
fix(fetch): revert error handling to current state
Browse files Browse the repository at this point in the history
  • Loading branch information
tripodsan committed May 16, 2019
1 parent 73ef930 commit a2040ae
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
12 changes: 8 additions & 4 deletions src/html/fetch-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,18 @@ async function fetch(context, { secrets = {}, request, logger }) {
setdefault(content, 'sources', []).push(options.uri);
} catch (e) {
if (e.statusCode === 404) {
context.error = e;
logger.error(`Could not find markdown! ${options.uri}`);
logger.error(`Could not find Markdown at ${options.uri}`);
setdefault(context, 'response', {}).status = 404;
} else if ((e.response && e.response.elapsedTime && e.response.elapsedTime > timeout) || (e.cause && e.cause.code && (e.cause.code === 'ESOCKETTIMEDOUT' || e.cause.code === 'ETIMEDOUT'))) {
// return gateway timeout
logger.error(`Gateway timout of ${timeout} milliseconds exceeded for ${options.uri}`);
setdefault(context, 'response', {}).status = 504;
} else {
logger.error(`Error while fetching URL ${options.uri} with the following `
logger.error(`Error while fetching Markdown from ${options.uri} with the following `
+ `options:\n${inspect(options, { depth: null })}`);
throw e;
setdefault(context, 'response', {}).status = 502;
}
context.error = e;
}
}

Expand Down
41 changes: 22 additions & 19 deletions test/testFetchMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,9 @@ describe('Test non-existing content', () => {
};

await coerce(myaction);
const dat = {};
await fetch(dat, myaction);
assert.ok(dat.error);
const context = {};
await fetch(context, myaction);
assert.ok(context.error);
});

it('Getting XDM README (with missing ref)', async () => {
Expand All @@ -224,10 +224,9 @@ describe('Test non-existing content', () => {
};

await coerce(myaction);

const data = {};
await fetch(data, myaction);
assert.ok(data.content.body);
const context = {};
await fetch(context, myaction);
assert.ok(context.content.body);
});
});

Expand Down Expand Up @@ -255,11 +254,10 @@ describe('Test requests', () => {
};

await coerce(myaction);

const data = {};
await fetch(data, myaction);
assert.ok(data.content.body);
assert.equal(data.content.body.split('\n')[0], '# Foo Data Model (XDM) Schema');
const context = {};
await fetch(context, myaction);
assert.ok(context.content.body);
assert.equal(context.content.body.split('\n')[0], '# Foo Data Model (XDM) Schema');
});
});

Expand Down Expand Up @@ -295,7 +293,10 @@ describe('Test misbehaved HTTP Responses', () => {
};

await coerce(myaction);
await assert.rejects(() => fetch({}, myaction));
const context = {};
await fetch(context, myaction);
assert.ok(context.error);
assert.equal(context.response.status, 502);
});

it('Getting XDM README with ultra-short Timeout', async function shortTimeout() {
Expand All @@ -321,14 +322,14 @@ describe('Test misbehaved HTTP Responses', () => {
};

await coerce(myaction);

await assert.rejects(() => fetch({}, myaction));
const context = {};
await fetch(context, myaction);
assert.ok(context.error);
assert.equal(context.response.status, 504);
});

it('Getting XDM README with Backend Timeout', async function badTimeout() {
const { server } = this.polly;


server
.get('https://raw.githubusercontent.com/adobe/xdm/master/README.md')
.intercept(async (_, res) => {
Expand All @@ -346,7 +347,9 @@ describe('Test misbehaved HTTP Responses', () => {
};

await coerce(myaction);

await assert.rejects(() => fetch({}, myaction));
const context = {};
await fetch(context, myaction);
assert.ok(context.error);
assert.equal(context.response.status, 504);
}).timeout(3000);
});

0 comments on commit a2040ae

Please sign in to comment.