Permalink
Browse files

Fix unhandled error when UDP sending fails.

  • Loading branch information...
1 parent ba9f665 commit 3cb4643ce69bdc10b808148c5aa7c1ab7a8b2098 @philbooth philbooth committed Jun 9, 2015
Showing with 17 additions and 20 deletions.
  1. +15 −18 src/forwarders/udp.js
  2. +1 −1 src/index.js
  3. +1 −1 test/index.js
View
@@ -47,40 +47,37 @@ function normaliseSize (size) {
}
function send (host, port, size, data, type, separator, callback) {
- var count = 0, length = 1, socket, chunks;
+ var count = 0, length = 1, socket, chunks, failed;
- try {
- socket = udp.createSocket('udp4');
+ socket = udp.createSocket('udp4');
- if (data.length <= size) {
- return sendToSocket(data);
- }
-
- chunks = chunkData(data, size, separator || '', []);
- length = chunks.length;
- chunks.forEach(function (chunk) {
- sendToSocket(chunk);
- });
- } catch (error) {
- socket.close();
- callback(error);
+ if (data.length <= size) {
+ return sendToSocket(data);
}
+ chunks = chunkData(data, size, separator || '', []);
+ length = chunks.length;
+ chunks.forEach(sendToSocket);
+
function sendToSocket (data) {
var buffer = new Buffer(data);
socket.send(buffer, 0, buffer.length, port, host, finish);
}
function finish (error, bytesSent) {
- if (error) {
- throw new Error(error);
+ if (error && !failed) {
+ callback(error);
+ failed = true;
}
count += 1;
if (count === length) {
socket.close();
- callback(null, bytesSent);
+
+ if (!failed) {
+ callback(null, bytesSent);
+ }
}
}
}
View
@@ -15,7 +15,7 @@
// You should have received a copy of the GNU General Public License
// along with boomcatch. If not, see <http://www.gnu.org/licenses/>.
-/*globals require, exports, process, setTimeout, console */
+/*globals require, exports, process, setTimeout */
'use strict';
View
@@ -1643,7 +1643,7 @@ suite('index:', function () {
});
test('response.end was called correctly', function () {
- assert.strictEqual(log.args.end[0][0], '{ "error": "wibble" }');
+ assert.strictEqual(log.args.end[0][0], '{ "error": "Forwarder failed" }');
});
test('response.statusCode was set correctly', function () {

0 comments on commit 3cb4643

Please sign in to comment.