Skip to content

Commit

Permalink
dns/resource: add error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
tynes committed Apr 16, 2020
1 parent 0268638 commit 06a704f
Showing 1 changed file with 36 additions and 32 deletions.
68 changes: 36 additions & 32 deletions lib/dns/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,11 @@ class Resource extends Struct {
}

fromJSON(json) {
assert(json && typeof json === 'object');
assert(Array.isArray(json.records));
assert(json && typeof json === 'object', 'Invalid json.');
assert(Array.isArray(json.records), 'Invalid records.');

for (const item of json.records) {
assert(item && typeof item === 'object');
assert(item && typeof item === 'object', 'Invalid record.');

const RD = stringToClass(item.type);

Expand Down Expand Up @@ -437,13 +437,17 @@ class DS extends Struct {
}

fromJSON(json) {
assert(json && typeof json === 'object');
assert(json.type === 'DS');
assert((json.keyTag & 0xffff) === json.keyTag);
assert((json.algorithm & 0xff) === json.algorithm);
assert((json.digestType & 0xff) === json.digestType);
assert(typeof json.digest === 'string');
assert((json.digest.length >>> 1) <= 255);
assert(json && typeof json === 'object', 'Invalid json in DS record.');
assert(json.type === 'DS', 'Invalid type in DS record.');
assert((json.keyTag & 0xffff) === json.keyTag,
'Invalid keyTag in DS record.');
assert((json.algorithm & 0xff) === json.algorithm,
'Invalid algorithm in DS record.');
assert((json.digestType & 0xff) === json.digestType,
'Invalid digestType in DS record.');
assert(typeof json.digest === 'string', 'Invalid digest in DS record.');
assert((json.digest.length >>> 1) <= 255,
'Invalid digest length in DS record.');

this.keyTag = json.keyTag;
this.algorithm = json.algorithm;
Expand Down Expand Up @@ -495,9 +499,9 @@ class NS extends Struct {
}

fromJSON(json) {
assert(json && typeof json === 'object');
assert(json.type === 'NS');
assert(isName(json.ns));
assert(json && typeof json === 'object', 'Invalid json in NS record.');
assert(json.type === 'NS', 'Invalid type in NS record.');
assert(isName(json.ns), 'Invalid ns in NS record.');

this.ns = json.ns;

Expand Down Expand Up @@ -554,10 +558,10 @@ class GLUE4 extends Struct {
}

fromJSON(json) {
assert(json && typeof json === 'object');
assert(json.type === 'GLUE4');
assert(isName(json.ns));
assert(IP.isIPv4String(json.address));
assert(json && typeof json === 'object', 'Invalid json in GLUE4 record.');
assert(json.type === 'GLUE4', 'Invalid type in GLUE4 record.');
assert(isName(json.ns), 'Invalid ns in GLUE4 record.');
assert(IP.isIPv4String(json.address), 'Invalid address in GLUE4 record.');

this.ns = json.ns;
this.address = IP.normalize(json.address);
Expand Down Expand Up @@ -615,10 +619,10 @@ class GLUE6 extends Struct {
}

fromJSON(json) {
assert(json && typeof json === 'object');
assert(json.type === 'GLUE6');
assert(isName(json.ns));
assert(IP.isIPv6String(json.address));
assert(json && typeof json === 'object', 'Invalid JSON in GLUE6 record.');
assert(json.type === 'GLUE6', 'Invalid type in GLUE6 record.');
assert(isName(json.ns), 'Invalid ns in GLUE6 record.');
assert(IP.isIPv6String(json.address), 'Invalid address in GLUE6 record.');

this.ns = json.ns;
this.address = IP.normalize(json.address);
Expand Down Expand Up @@ -677,9 +681,9 @@ class SYNTH4 extends Struct {
}

fromJSON(json) {
assert(json && typeof json === 'object');
assert(json.type === 'SYNTH4');
assert(IP.isIPv4String(json.address));
assert(json && typeof json === 'object', 'Invalid json in SYNTH4 record.');
assert(json.type === 'SYNTH4', 'Invalid type in SYNTH4 record.');
assert(IP.isIPv4String(json.address), 'Invalid address in SYNTH4 record.');

this.address = IP.normalize(json.address);

Expand Down Expand Up @@ -737,9 +741,9 @@ class SYNTH6 extends Struct {
}

fromJSON(json) {
assert(json && typeof json === 'object');
assert(json.type === 'SYNTH6');
assert(IP.isIPv6String(json.address));
assert(json && typeof json === 'object', 'Invalid json in SYNTH6 record.');
assert(json.type === 'SYNTH6', 'Invalid type in SYNTH6 record.');
assert(IP.isIPv6String(json.address), 'Invalid address in SYNTH6 record.');

this.address = IP.normalize(json.address);

Expand Down Expand Up @@ -812,13 +816,13 @@ class TXT extends Struct {
}

fromJSON(json) {
assert(json && typeof json === 'object');
assert(json.type === 'TXT');
assert(Array.isArray(json.txt));
assert(json && typeof json === 'object', 'Invalid json in TXT record.');
assert(json.type === 'TXT', 'Invalid type in TXT record.');
assert(Array.isArray(json.txt), 'Invalid txt in TXT record.');

for (const txt of json.txt) {
assert(typeof txt === 'string');
assert(txt.length <= 255);
assert(typeof txt === 'string', 'Invalid txt entry in TXT record.');
assert(txt.length <= 255, 'Invalid txt entry length in TXT record.');

this.txt.push(txt);
}
Expand Down

0 comments on commit 06a704f

Please sign in to comment.