Handle packet decoding errors
masterWhen parsing packets, two types of errors can occur:
Packet.DecodeError: Thrown byPacket.parse(buffer)if the message is fundamentally unreadable (e.g., shorter than the 12-octet header).- Malformed Records: If the header is valid but specific records are malformed,
Packet.parsedoes not throw. Instead, it drops the malformed records and populatespacket.errors.
For each error in packet.errors, you can inspect:
err.section: 'questions' | 'answers' | 'authorities' | 'additionals'err.index: position within the sectionerr.offset: octet offset in the messageerr.recovered:trueif decoding continued after the error;falseif the error caused misalignment and decoding stopped.
try {
Packet.parse(buffer);
} catch (err) {
// Handles undecodable headers
console.error(err.message);
}
const packet = Packet.parse(buffer);
if (packet.errors.length > 0) {
for (const err of packet.errors) {
console.error(err.message);
console.log(err.section, err.index, err.offset, err.recovered);
}
}