diff --git a/src/index.js b/src/index.js index d806ca7..8d59b7b 100644 --- a/src/index.js +++ b/src/index.js @@ -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 = ` + let body = ` OK ${Math.abs(Date.now() - start)} `; - body += checkresults - .map(({ key, response }) => ` <${key}>${Math.floor(response.timings.end)}`) - .join('\n'); + body += checkresults + .map(({ key, response }) => ` <${key}>${Math.floor(response.timings.end)}`) + .join('\n'); - body += ` + body += ` `; - 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: ` + failed + ${Math.abs(Date.now() - start)} + + ${e.options.uri} + ${e.response.statusCode} + + + + ${process.env.__OW_ACTIVATION_ID} + +` + } + } } function wrap(func, checks) { diff --git a/test/fixtures/recordings/Index-Tests_647266286/index-function-fails-with-useful-error-message_1959879595/recording.har b/test/fixtures/recordings/Index-Tests_647266286/index-function-fails-with-useful-error-message_1959879595/recording.har new file mode 100644 index 0000000..cdb6d51 --- /dev/null +++ b/test/fixtures/recordings/Index-Tests_647266286/index-function-fails-with-useful-error-message_1959879595/recording.har @@ -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": "\n\n\n Example Domain\n\n \n \n \n \n\n\n\n
\n

Example Domain

\n

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.

\n

More information...

\n
\n\n\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" + } +} diff --git a/test/index.test.js b/test/index.test.js index b72f3c2..241f142 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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(/500/)); + assert.ok(result.body.match(/failed/)); + assert.deepEqual(result.statusCode, 503); + }); + it('index function throws if passed invalid arguments', async () => { try { await index();