Skip to content

Commit

Permalink
fix: measurement results behaviour (#23)
Browse files Browse the repository at this point in the history
* ref: ping: report failed cmd

* ref: trace: report failed cmd

* ref: dns: return empty obj on missing answer

* ref: dns result schema

* fix: traceroute: missing trim

* fix: return empty string on missing error.stderr
  • Loading branch information
patrykcieszkowski authored Mar 31, 2022
1 parent 10cbe4d commit 5ff8b4b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
2 changes: 2 additions & 0 deletions @types/node-dig-dns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ declare module 'node-dig-dns' {

export type DnsQueryResult = {
answer: SingleDnsQueryResult[];
time: number;
server: string;
};

export function dig(args: string[]): Promise<DnsQueryResult>;
Expand Down
6 changes: 5 additions & 1 deletion src/command/dns-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ export class DnsCommand implements CommandInterface<DnsOptions> {
socket.emit('probe:measurement:result', {
testId,
measurementId,
result: result.answer,
result: {
answer: result.answer || [],
time: result.time,
server: result.server,
},
});
}
}
15 changes: 12 additions & 3 deletions src/command/ping-command.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Joi from 'joi';
import type {Socket} from 'socket.io-client';
import {execa, ExecaChildProcess} from 'execa';
import {execa, ExecaChildProcess, ExecaError} from 'execa';
import type {CommandInterface} from '../types.js';
import {InvalidOptionsException} from './exception/invalid-options-exception.js';

Expand Down Expand Up @@ -47,12 +47,21 @@ export class PingCommand implements CommandInterface<PingOptions> {
});
});

const result = await cmd;
let result = {};

try {
const cmdResult = await cmd;
result = this.parse(cmdResult.stdout);
} catch (error: unknown) {
result = {
rawOutput: (error as ExecaError).stderr?.toString() ?? '',
};
}

socket.emit('probe:measurement:result', {
testId,
measurementId,
result: this.parse(result.stdout),
result,
});
}

Expand Down
14 changes: 11 additions & 3 deletions src/command/traceroute-command.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Joi from 'joi';
import type {Socket} from 'socket.io-client';
import {execa, ExecaChildProcess} from 'execa';
import {execa, ExecaChildProcess, ExecaError} from 'execa';
import type {CommandInterface} from '../types.js';
import {InvalidOptionsException} from './exception/invalid-options-exception.js';

Expand Down Expand Up @@ -69,12 +69,20 @@ export class TracerouteCommand implements CommandInterface<TraceOptions> {
});
});

const result = await cmd;
let result = {};
try {
const cmdResult = await cmd;
result = this.parse(cmdResult.stdout.trim());
} catch (error: unknown) {
result = {
rawOutput: (error as ExecaError).stderr?.toString() ?? '',
};
}

socket.emit('probe:measurement:result', {
testId,
measurementId,
result: this.parse(result.stdout.trim()),
result,
});
}

Expand Down

0 comments on commit 5ff8b4b

Please sign in to comment.