Skip to content

Commit

Permalink
Emit errors instead of throwing on parse errors
Browse files Browse the repository at this point in the history
If an error is thrown while creating a Request object, such as when an
unknown resource record type is requested, there's no way for the client
to handle it. The error is thrown before the request is emitted and the
handler is called. This change leaves the underlying error throwing as
it is but catches the error in the server and emits them instead.
  • Loading branch information
sullman committed Aug 18, 2020
1 parent 4f661ae commit 925313d
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,16 @@ Server.prototype.on_tcp_connection = function(connection) {
}

if(length !== null && bytes_received == 2 + length) {
// All of the data (plus the 2-byte length prefix) is received.
var data = Buffer.concat(bufs)
, req = new Request(data, connection)
, res = new Response(data, connection)

self.emit('request', req, res)
try {
// All of the data (plus the 2-byte length prefix) is received.
var data = Buffer.concat(bufs)
, req = new Request(data, connection)
, res = new Response(data, connection)

self.emit('request', req, res)
} catch (err) {
self.emit('error', err);
}
}
})
}
Expand All @@ -164,10 +168,14 @@ Server.prototype.on_udp = function(data, rinfo) {
, 'end' : function() {}
}

var req = new Request(data, connection)
, res = new Response(data, connection)
try {
var req = new Request(data, connection)
, res = new Response(data, connection)

self.emit('request', req, res)
self.emit('request', req, res)
} catch (err) {
self.emit('error', err);
}
}


Expand Down

0 comments on commit 925313d

Please sign in to comment.