diff --git a/lib/dgram.js b/lib/dgram.js index 221afcf5bb0c84a..5dbc2f22dab6585 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -159,7 +159,7 @@ function startListening(socket) { const state = socket[kStateSymbol]; state.handle.onmessage = onMessage; - // Todo: handle errors + state.handle.onerror = onError; state.handle.recvStart(); state.receiving = true; state.bindState = BIND_STATE_BOUND; @@ -923,6 +923,12 @@ function onMessage(nread, handle, buf, rinfo) { } +function onError(nread, handle, error) { + const self = handle[owner_symbol]; + return self.emit('error', error); +} + + Socket.prototype.ref = function() { const handle = this[kStateSymbol].handle; diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index 5fe1134ecf5af1f..0fb15ce0a0d4ad7 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -22,6 +22,7 @@ #include "udp_wrap.h" #include "env-inl.h" #include "node_buffer.h" +#include "node_errors.h" #include "node_sockaddr-inl.h" #include "handle_wrap.h" #include "req_wrap-inl.h" @@ -29,6 +30,7 @@ namespace node { +using errors::TryCatchScope; using v8::Array; using v8::ArrayBuffer; using v8::BackingStore; @@ -729,10 +731,32 @@ void UDPWrap::OnRecv(ssize_t nread, } Local address; - if (!AddressToJS(env, addr).ToLocal(&address)) return; + { + TryCatchScope try_catch(env); + try_catch.SetVerbose(true); + DCHECK(try_catch.IsVerbose()); + if (!AddressToJS(env, addr).ToLocal(&address)) { + DCHECK(try_catch.HasCaught() && !try_catch.HasTerminated()); + argv[2] = try_catch.Exception(); + DCHECK(!argv[2].IsEmpty()); + MakeCallback(env->onerror_string(), arraysize(argv), argv); + return; + } + } Local ab = ArrayBuffer::New(isolate, std::move(bs)); - argv[2] = Buffer::New(env, ab, 0, ab->ByteLength()).ToLocalChecked(); + { + TryCatchScope try_catch(env); + try_catch.SetVerbose(true); + DCHECK(try_catch.IsVerbose()); + if (!Buffer::New(env, ab, 0, ab->ByteLength()).ToLocal(&argv[2])) { + DCHECK(try_catch.HasCaught() && !try_catch.HasTerminated()); + argv[2] = try_catch.Exception(); + DCHECK(!argv[2].IsEmpty()); + MakeCallback(env->onerror_string(), arraysize(argv), argv); + return; + } + } argv[3] = address; MakeCallback(env->onmessage_string(), arraysize(argv), argv); }