From 4c85be0ebf29c0787eca413bb881182e8dd3dc4f Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 9 Oct 2017 15:11:32 -0600 Subject: [PATCH] catch and report parse / pack errors --- dns-request.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/dns-request.js b/dns-request.js index dca6f9c..d866191 100644 --- a/dns-request.js +++ b/dns-request.js @@ -102,12 +102,28 @@ function resolve(queryAb, opts) { } function resolveJson(query, opts) { - var queryAb = dnsjs.DNSPacket.write(query); + var queryAb; + try { + queryAb = dnsjs.DNSPacket.write(query); + } catch(e) { + if ('function' === typeof opts.onError) { opts.onError(e); return; } + throw e; + } + //console.log('[DEV] nameserver', opts.nameserver); var options = { onError: opts.onError , onMessage: function (nb) { - var packet = dnsjs.DNSPacket.parse(nb.buffer.slice(nb.byteOffset, nb.byteOffset + nb.byteLength)); + var packet; + try { + packet = dnsjs.DNSPacket.parse(nb.buffer.slice(nb.byteOffset, nb.byteOffset + nb.byteLength)); + } catch(e) { + if (opts.onError) { opts.onError(e); return; } + + console.error("[Error] couldn't parse incoming message"); + console.error(e); + return; + } opts.onMessage(packet); }