Centralize error handling inside UpdateDnsCommand

This commit is contained in:
Ramon Caballero 2026-04-07 00:13:10 +01:00
parent 1cbe008a86
commit 448eebd1d6
1 changed files with 11 additions and 8 deletions

View File

@ -41,26 +41,22 @@ class UpdateDnsCommand
if ($result === null) if ($result === null)
{ {
Console::echo("Invalid JSON returned by Porkbun API:\n$raw\n"); return $this->fail("Invalid JSON returned by Porkbun API: " . $raw);
return self::STATUS_ERROR;
} }
if (!isset($result->status)) if (!isset($result->status))
{ {
Console::echo("Porkbun API response missing 'status' field:\n$raw\n"); return $this->fail("Porkbun API response missing 'status' field: " . $raw);
return self::STATUS_ERROR;
} }
if ($result->status !== "SUCCESS") if ($result->status !== "SUCCESS")
{ {
Console::echo("Porkbun API returned an error:\n$raw\n"); return $this->fail("Porkbun API returned an error: " . $raw);
return self::STATUS_ERROR;
} }
if (!isset($result->yourIp)) if (!isset($result->yourIp))
{ {
Console::echo("Porkbun API did not return your public IP.\n"); return $this->fail("Porkbun API did not return your public IP.");
return self::STATUS_ERROR;
} }
$myIp = $myIp ?? $result->yourIp; $myIp = $myIp ?? $result->yourIp;
@ -119,4 +115,11 @@ class UpdateDnsCommand
return $updated ? self::STATUS_UPDATED : self::STATUS_NOTHING_TO_CHANGE; return $updated ? self::STATUS_UPDATED : self::STATUS_NOTHING_TO_CHANGE;
} }
private function fail(string $message): int
{
Console::echo($message);
$this->logger->log("ERROR: " . $message);
return self::STATUS_ERROR;
}
} }