dgram: make send cb act as "error" event handler · nodejs/node@6b3f046 · GitHub
Skip to content

Commit 6b3f046

Browse files
chrisdickinsonrvagg
authored andcommitted
dgram: make send cb act as "error" event handler
This allows users to provide a callback that handles potential errors resulting from a `socket.send` call. The original behavior of emitting the error event on the socket is preserved. Fixes: nodejs/node-v0.x-archive#4846 PR-URL: nodejs/node-v0.x-archive#7738 PR-URL: #1796 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 81f12be commit 6b3f046

3 files changed

Lines changed: 66 additions & 2 deletions

File tree

doc/api/dgram.markdown

Lines changed: 3 additions & 1 deletion

lib/dgram.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,15 @@ Socket.prototype.send = function(buffer,
300300

301301
self._handle.lookup(address, function(ex, ip) {
302302
if (ex) {
303-
if (callback) callback(ex);
303+
if (callback) {
304+
callback(ex);
305+
306+
if (self.listeners('error').length)
307+
self.emit('error', ex);
308+
309+
return;
310+
}
311+
304312
self.emit('error', ex);
305313
} else if (self._handle) {
306314
var req = new SendWrap();
Lines changed: 54 additions & 0 deletions

0 commit comments

Comments
 (0)