Skip to content

Commit

Permalink
feat(index): respond with a useful error message when one of the chec…
Browse files Browse the repository at this point in the history
…ks fails
  • Loading branch information
trieloff committed Jun 16, 2019
1 parent d45e2e6 commit 70fae6d
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 18 deletions.
59 changes: 41 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,56 @@ const request = require('request-promise-native');
async function report(checks) {
const start = Date.now();

const runchecks = Object.keys(checks)
.filter(key => key.match('^[a-z0-9]+$'))
.map(key => [key, checks[key]])
.map(([key, url]) => request.get(url, { resolveWithFullResponse: true, time: true })
.then(response => ({ key, response })));
try {
const runchecks = Object.keys(checks)
.filter(key => key.match('^[a-z0-9]+$'))
.map(key => [key, checks[key]])
.map(([key, url]) => request.get(url, { resolveWithFullResponse: true, time: true })
.then(response => ({ key, response })));

const checkresults = await Promise.all(runchecks);
const checkresults = await Promise.all(runchecks);

let body = `<pingdom_http_custom_check>
let body = `<pingdom_http_custom_check>
<status>OK</status>
<response_time>${Math.abs(Date.now() - start)}</response_time>
`;

body += checkresults
.map(({ key, response }) => ` <${key}>${Math.floor(response.timings.end)}</${key}>`)
.join('\n');
body += checkresults
.map(({ key, response }) => ` <${key}>${Math.floor(response.timings.end)}</${key}>`)
.join('\n');

body += `
body += `
</pingdom_http_custom_check>`;

return {
statusCode: 200,
headers: {
'Content-Type': 'application/xml',
},
body,
};
return {
statusCode: 200,
headers: {
'Content-Type': 'application/xml',
},
body,
};
} catch (e) {
return {
statusCode: 503,
headers: {
'Content-Type': 'application/xml',
},
body: `<pingdom_http_custom_check>
<status>failed</status>
<response_time>${Math.abs(Date.now() - start)}</response_time>
<error>
<url>${e.options.uri}</url>
<statuscode>${e.response.statusCode}</statuscode>
<body><![CDATA[
${e.response.body}
]></body>
</error>
<process>
<activation>${process.env.__OW_ACTIVATION_ID}</activation>
</process>
</pingdom_http_custom_check>`
}
}
}

function wrap(func, checks) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{
"log": {
"_recordingName": "Index Tests/index function fails with useful error message",
"creator": {
"comment": "persister:fs",
"name": "Polly.JS",
"version": "2.5.0"
},
"entries": [
{
"_id": "013c7aafcd9aa79cc2119918d88de8a7",
"_order": 0,
"cache": {},
"request": {
"bodySize": 0,
"cookies": [],
"headers": [
{
"name": "host",
"value": "www.example.com"
}
],
"headersSize": 63,
"httpVersion": "HTTP/1.1",
"method": "GET",
"queryString": [],
"url": "http://www.example.com/"
},
"response": {
"bodySize": 1270,
"content": {
"mimeType": "text/html; charset=UTF-8",
"size": 1270,
"text": "<!doctype html>\n<html>\n<head>\n <title>Example Domain</title>\n\n <meta charset=\"utf-8\" />\n <meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n <style type=\"text/css\">\n body {\n background-color: #f0f0f2;\n margin: 0;\n padding: 0;\n font-family: \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n \n }\n div {\n width: 600px;\n margin: 5em auto;\n padding: 50px;\n background-color: #fff;\n border-radius: 1em;\n }\n a:link, a:visited {\n color: #38488f;\n text-decoration: none;\n }\n @media (max-width: 700px) {\n body {\n background-color: #fff;\n }\n div {\n width: auto;\n margin: 0 auto;\n border-radius: 0;\n padding: 1em;\n }\n }\n </style> \n</head>\n\n<body>\n<div>\n <h1>Example Domain</h1>\n <p>This domain is established to be used for illustrative examples in documents. You may use this\n domain in examples without prior coordination or asking for permission.</p>\n <p><a href=\"http://www.iana.org/domains/example\">More information...</a></p>\n</div>\n</body>\n</html>\n"
},
"cookies": [],
"headers": [
{
"name": "cache-control",
"value": "max-age=604800"
},
{
"name": "content-type",
"value": "text/html; charset=UTF-8"
},
{
"name": "date",
"value": "Sun, 16 Jun 2019 19:41:42 GMT"
},
{
"name": "etag",
"value": "\"1541025663+gzip+ident\""
},
{
"name": "expires",
"value": "Sun, 23 Jun 2019 19:41:42 GMT"
},
{
"name": "last-modified",
"value": "Fri, 09 Aug 2013 23:54:35 GMT"
},
{
"name": "server",
"value": "ECS (nyb/1D07)"
},
{
"name": "vary",
"value": "Accept-Encoding"
},
{
"name": "x-cache",
"value": "HIT"
},
{
"name": "content-length",
"value": "1270"
},
{
"name": "connection",
"value": "close"
}
],
"headersSize": 329,
"httpVersion": "HTTP/1.1",
"redirectURL": "",
"status": 200,
"statusText": "OK"
},
"startedDateTime": "2019-06-16T19:41:42.040Z",
"time": 1080,
"timings": {
"blocked": -1,
"connect": -1,
"dns": -1,
"receive": 0,
"send": 0,
"ssl": -1,
"wait": 1080
}
}
],
"pages": [],
"version": "1.2"
}
}
16 changes: 16 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ describe('Index Tests', () => {
assert.deepEqual(result.statusCode, 200);
});

it('index function fails with useful error message', async function test() {
const { server } = this.polly;

server.get('http://www.fail.com/').intercept((_, res) => res.sendStatus(500).json({}));

const result = await index({
example: 'http://www.example.com',
fail: 'http://www.fail.com/',
__ow_method: 'get',
});

assert.ok(result.body.match(/<statuscode>500/));
assert.ok(result.body.match(/<status>failed/));
assert.deepEqual(result.statusCode, 503);
});

it('index function throws if passed invalid arguments', async () => {
try {
await index();
Expand Down

0 comments on commit 70fae6d

Please sign in to comment.