Skip to content

Commit 925313d

Browse files
committed
Emit errors instead of throwing on parse errors
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.
1 parent 4f661ae commit 925313d

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

server.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,16 @@ Server.prototype.on_tcp_connection = function(connection) {
141141
}
142142

143143
if(length !== null && bytes_received == 2 + length) {
144-
// All of the data (plus the 2-byte length prefix) is received.
145-
var data = Buffer.concat(bufs)
146-
, req = new Request(data, connection)
147-
, res = new Response(data, connection)
148-
149-
self.emit('request', req, res)
144+
try {
145+
// All of the data (plus the 2-byte length prefix) is received.
146+
var data = Buffer.concat(bufs)
147+
, req = new Request(data, connection)
148+
, res = new Response(data, connection)
149+
150+
self.emit('request', req, res)
151+
} catch (err) {
152+
self.emit('error', err);
153+
}
150154
}
151155
})
152156
}
@@ -164,10 +168,14 @@ Server.prototype.on_udp = function(data, rinfo) {
164168
, 'end' : function() {}
165169
}
166170

167-
var req = new Request(data, connection)
168-
, res = new Response(data, connection)
171+
try {
172+
var req = new Request(data, connection)
173+
, res = new Response(data, connection)
169174

170-
self.emit('request', req, res)
175+
self.emit('request', req, res)
176+
} catch (err) {
177+
self.emit('error', err);
178+
}
171179
}
172180

173181

0 commit comments

Comments
 (0)