Skip to content

Commit 6fefa1c

Browse files
committed
imporove error handling of dig
1 parent e8b53d9 commit 6fefa1c

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

src/Exceptions/CouldNotFetchDns.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ public static function noHandlerFound(): self
1212
return new static('A runnable handler could not be found');
1313
}
1414

15-
public static function digReturnedWithError(Process $process): self
15+
public static function digReturnedWithError(Process $process, string $command): self
1616
{
1717
$output = trim($process->getErrorOutput());
1818

19-
return new static("Dig command failed with message: `{$output}`");
19+
if (empty($output)) {
20+
$output = trim($process->getOutput());
21+
}
22+
23+
return new static("Dig command `{$command}` failed with message: `{$output}`");
2024
}
2125

2226
public static function dnsGetRecordReturnedWithError(string $error): self

src/Handlers/Dig.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@ class Dig extends Handler
1010
{
1111
public function __invoke(string $domain, int $flag, string $type): array
1212
{
13+
$command = $this->buildCommand($domain, $type);
14+
1315
$process = new Process($this->buildCommand($domain, $type));
1416

1517
$process
1618
->enableOutput()
1719
->run();
1820

1921
if (! $process->isSuccessful()) {
20-
throw CouldNotFetchDns::digReturnedWithError($process);
22+
$command = implode(' ', $command);
23+
24+
throw CouldNotFetchDns::digReturnedWithError($process, $command);
2125
}
2226

2327
return $this->transform(

tests/DnsTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ public function it_uses_default_nameserver_if_not_set()
145145
public function it_throws_exception_on_failed_to_fetch_dns_record()
146146
{
147147
$this->expectException(CouldNotFetchDns::class);
148-
$this->expectExceptionMessage("Dig command failed with message: `dig: couldn't get address for 'dns.spatie.be': not found`");
149148

150149
$this->dns
151150
->useNameserver('dns.spatie.be')

0 commit comments

Comments
 (0)