bugsnag-build-reporter
Advanced tools
Comparing version 1.0.1 to 1.0.2
# Changelog | ||
## 1.0.1 (2018-11-04) | ||
## 1.0.2 (2019-05-23) | ||
- Retry in more failing scenarios (#5) | ||
## 1.0.1 (2018-12-04) | ||
- Retry failed requests up to 5 times before failing (#1) |
@@ -7,4 +7,7 @@ 'use strict' | ||
const url = require('url') | ||
const once = require('once') | ||
const MAX_ATTEMPTS = 5 | ||
const RETRY_INTERVAL = process.env.BUGSNAG_RETRY_INTERVAL || 1000 | ||
const RETRY_INTERVAL = parseInt(process.env.BUGSNAG_RETRY_INTERVAL) || 1000 | ||
const TIMEOUT = parseInt(process.env.BUGSNAG_TIMEOUT) || 30000 | ||
@@ -15,3 +18,3 @@ module.exports = (endpoint, data, onSuccess, onError) => { | ||
attempts++ | ||
if (err && err.isRetryable && attempts < MAX_ATTEMPTS) return setTimeout(go, RETRY_INTERVAL) | ||
if (err && err.isRetryable !== false && attempts < MAX_ATTEMPTS) return setTimeout(go, RETRY_INTERVAL) | ||
return onError(err) | ||
@@ -24,2 +27,3 @@ } | ||
const send = (endpoint, data, onSuccess, onError) => { | ||
onError = once(onError) | ||
const parsedUrl = url.parse(endpoint) | ||
@@ -41,3 +45,5 @@ const payload = JSON.stringify(data) | ||
const err = new Error(`HTTP status ${res.statusCode} received from builds API`) | ||
err.isRetryable = true | ||
if (!isRetryable(res.statusCode)) { | ||
err.isRetryable = false | ||
} | ||
return onError(err) | ||
@@ -48,5 +54,9 @@ } | ||
err.errors = JSON.parse(body).errors | ||
// never retry a 400 | ||
err.isRetryable = false | ||
return onError(err) | ||
} catch (e) { | ||
return onError(new Error(`HTTP status ${res.statusCode} received from builds API`)) | ||
} catch (_) { | ||
const e = new Error(`HTTP status ${res.statusCode} received from builds API`) | ||
e.isRetryable = false | ||
return onError(e) | ||
} | ||
@@ -57,3 +67,17 @@ })) | ||
req.write(payload) | ||
req.setTimeout(TIMEOUT, () => { | ||
onError(new Error('Connection timed out')) | ||
req.abort() | ||
}) | ||
req.end() | ||
} | ||
const isRetryable = status => { | ||
return ( | ||
status < 400 || | ||
status > 499 || | ||
[ | ||
408, // timeout | ||
429 // too many requests | ||
].indexOf(status) !== -1) | ||
} |
'use strict' | ||
process.env.BUGSNAG_RETRY_INTERVAL = 50 | ||
process.env.BUGSNAG_TIMEOUT = 100 | ||
const test = require('tape') | ||
const http = require('http') | ||
const net = require('net') | ||
const send = require('../send') | ||
@@ -120,4 +122,6 @@ | ||
test('send(): unsuccessful (400, bad json)', t => { | ||
t.plan(2) | ||
t.plan(3) | ||
let n = 0 | ||
const server = http.createServer((req, res) => { | ||
n++ | ||
res.statusCode = 400 | ||
@@ -135,4 +139,45 @@ res.setHeader('content-type', 'application/json') | ||
t.equal(err.message, 'HTTP status 400 received from builds API') | ||
t.equal(n, 1) | ||
t.end() | ||
}) | ||
}) | ||
test('send(): retry on timeouts', t => { | ||
t.plan(3) | ||
let n = 0 | ||
const server = net.createServer((req, res) => { | ||
n++ | ||
}) | ||
server.listen() | ||
send(`http://localhost:${server.address().port}`, {}, () => { | ||
server.close() | ||
t.fail('send should not succeed') | ||
}, (err) => { | ||
server.close() | ||
t.equal(n, 5, 'should retry after timeouts') | ||
t.ok(err) | ||
t.equal(err.message, 'Connection timed out') | ||
t.end() | ||
}) | ||
}) | ||
test('send(): retry on socket hangups', t => { | ||
t.plan(4) | ||
let n = 0 | ||
const server = http.createServer((req, res) => { | ||
n++ | ||
req.connection.end() | ||
}) | ||
server.listen() | ||
send(`http://localhost:${server.address().port}`, {}, () => { | ||
server.close() | ||
t.fail('send should not succeed') | ||
}, (err) => { | ||
server.close() | ||
t.equal(n, 5, 'should retry after timeouts') | ||
t.ok(err) | ||
t.equal(err.code, 'ECONNRESET') | ||
t.equal(err.message, 'socket hang up') | ||
t.end() | ||
}) | ||
}) |
{ | ||
"name": "bugsnag-build-reporter", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "A tool for reporting your application’s builds to Bugsnag", | ||
@@ -37,2 +37,3 @@ "keywords": [ | ||
"minimist": "^1.2.0", | ||
"once": "^1.4.0", | ||
"pino": "^4.10.3", | ||
@@ -39,0 +40,0 @@ "run-parallel": "^1.1.6" |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
66776
8
108381
68
740
8
7
+ Addedonce@^1.4.0