From d0b827e79e2bf5846b75e8fe323a887473572ee2 Mon Sep 17 00:00:00 2001 From: TinyMemoria <144521819+TinyMemoria@users.noreply.github.com> Date: Mon, 26 Jan 2026 12:59:33 +1000 Subject: [PATCH 1/3] fix subdomain edit and improved error handling --- subdomains/src/Models/Subdomain.php | 59 ++++++++++++-------- subdomains/src/Services/SubdomainService.php | 17 ++++-- 2 files changed, 49 insertions(+), 27 deletions(-) diff --git a/subdomains/src/Models/Subdomain.php b/subdomains/src/Models/Subdomain.php index 105b427f..be375d01 100644 --- a/subdomains/src/Models/Subdomain.php +++ b/subdomains/src/Models/Subdomain.php @@ -62,27 +62,6 @@ public function upsertOnCloudflare(): void throw new Exception('Server has no allocation'); } - // @phpstan-ignore staticMethod.notFound - $response = Http::cloudflare()->get("zones/{$this->domain->cloudflare_id}/dns_records/$this->cloudflare_id", ['search' => $this->name])->json(); - - if ($response['success']) { - if (!empty($response['result'])) { - throw new Exception('A subdomain with that name already exists'); - } - } else { - if ($response['errors'] && count($response['errors']) > 0) { - throw new Exception($response['errors'][0]['message']); - } - } - - $payload = [ - 'name' => $this->name, - 'type' => $this->record_type, - 'comment' => 'Created by Pelican Subdomains plugin', - 'content' => $this->server->allocation->ip, - 'proxied' => false, - ]; - if ($this->record_type === 'SRV') { $srvTarget = $this->server->node->srv_target; // @phpstan-ignore property.notFound @@ -96,8 +75,10 @@ public function upsertOnCloudflare(): void throw new Exception('Server has no SRV type'); } + $searchName = "{$srvServiceType->value}.{$this->name}"; + $payload = [ - 'name' => "$srvServiceType->value.$this->name", + 'name' => $searchName, 'type' => $this->record_type, 'comment' => 'Created by Pelican Subdomains plugin', 'data' => [ @@ -112,11 +93,43 @@ public function upsertOnCloudflare(): void if ($this->server->allocation->ip === '0.0.0.0' || $this->server->allocation->ip === '::') { throw new Exception('Server has invalid allocation ip (0.0.0.0 or ::)'); } + + $searchName = $this->name; + + $payload = [ + 'name' => $searchName, + 'type' => $this->record_type, + 'comment' => 'Created by Pelican Subdomains plugin', + 'content' => $this->server->allocation->ip, + 'proxied' => false, + ]; + } + + // @phpstan-ignore staticMethod.notFound + $searchResponse = Http::cloudflare()->get("zones/{$this->domain->cloudflare_id}/dns_records", [ + 'name' => $searchName, + 'type' => $this->record_type, + ])->json(); + + if (!$searchResponse['success']) { + if (!empty($searchResponse['errors'])) { + throw new Exception($searchResponse['errors'][0]['message']); + } + } else { + $results = $searchResponse['result'] ?? []; + + if (!empty($results)) { + foreach ($results as $record) { + if ($record['id'] !== $this->cloudflare_id) { + throw new Exception('A subdomain with that name already exists'); + } + } + } } if ($this->cloudflare_id) { // @phpstan-ignore staticMethod.notFound - $response = Http::cloudflare()->patch("zones/{$this->domain->cloudflare_id}/dns_records/$this->cloudflare_id", $payload)->json(); + $response = Http::cloudflare()->patch("zones/{$this->domain->cloudflare_id}/dns_records/{$this->cloudflare_id}", $payload)->json(); } else { // @phpstan-ignore staticMethod.notFound $response = Http::cloudflare()->post("zones/{$this->domain->cloudflare_id}/dns_records", $payload)->json(); diff --git a/subdomains/src/Services/SubdomainService.php b/subdomains/src/Services/SubdomainService.php index b2490784..85e9804e 100644 --- a/subdomains/src/Services/SubdomainService.php +++ b/subdomains/src/Services/SubdomainService.php @@ -14,14 +14,23 @@ class SubdomainService */ public function handle(array $data, ?Subdomain $subdomain = null): Subdomain { - $subdomain ??= Subdomain::create($data); - $subdomain->update($data); + $NewSubdomain = false; + + if (is_null($subdomain)) { + $subdomain = Subdomain::create($data); + $NewSubdomain = true; + } else { + $subdomain->update($data); + } + + $subdomain->refresh(); try { $subdomain->upsertOnCloudflare(); } catch (Exception $exception) { - $subdomain->delete(); - + if ($NewSubdomain) { + $subdomain->delete(); + } throw $exception; } From a584e79ced99ac58d4eb4a58080999303e09e1c1 Mon Sep 17 00:00:00 2001 From: TinyMemoria <144521819+TinyMemoria@users.noreply.github.com> Date: Mon, 26 Jan 2026 13:34:04 +1000 Subject: [PATCH 2/3] changed variable to camelCase and response handling --- subdomains/src/Models/Subdomain.php | 1 + subdomains/src/Services/SubdomainService.php | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/subdomains/src/Models/Subdomain.php b/subdomains/src/Models/Subdomain.php index be375d01..59f5637d 100644 --- a/subdomains/src/Models/Subdomain.php +++ b/subdomains/src/Models/Subdomain.php @@ -115,6 +115,7 @@ public function upsertOnCloudflare(): void if (!empty($searchResponse['errors'])) { throw new Exception($searchResponse['errors'][0]['message']); } + throw new Exception('Failed to search for existing DNS records'); } else { $results = $searchResponse['result'] ?? []; diff --git a/subdomains/src/Services/SubdomainService.php b/subdomains/src/Services/SubdomainService.php index 85e9804e..f52ebe64 100644 --- a/subdomains/src/Services/SubdomainService.php +++ b/subdomains/src/Services/SubdomainService.php @@ -14,11 +14,11 @@ class SubdomainService */ public function handle(array $data, ?Subdomain $subdomain = null): Subdomain { - $NewSubdomain = false; + $newSubdomain = false; if (is_null($subdomain)) { $subdomain = Subdomain::create($data); - $NewSubdomain = true; + $newSubdomain = true; } else { $subdomain->update($data); } @@ -28,7 +28,7 @@ public function handle(array $data, ?Subdomain $subdomain = null): Subdomain try { $subdomain->upsertOnCloudflare(); } catch (Exception $exception) { - if ($NewSubdomain) { + if ($newSubdomain) { $subdomain->delete(); } throw $exception; From 10ac7c56b4ed6513ba7dad49313896078f2c0d93 Mon Sep 17 00:00:00 2001 From: Boy132 Date: Mon, 26 Jan 2026 08:39:00 +0100 Subject: [PATCH 3/3] cleanup --- subdomains/src/Models/Subdomain.php | 23 +++++++++----------- subdomains/src/Services/SubdomainService.php | 5 +++-- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/subdomains/src/Models/Subdomain.php b/subdomains/src/Models/Subdomain.php index 59f5637d..e501bc00 100644 --- a/subdomains/src/Models/Subdomain.php +++ b/subdomains/src/Models/Subdomain.php @@ -75,7 +75,7 @@ public function upsertOnCloudflare(): void throw new Exception('Server has no SRV type'); } - $searchName = "{$srvServiceType->value}.{$this->name}"; + $searchName = "$srvServiceType->value.$this->name"; $payload = [ 'name' => $searchName, @@ -111,26 +111,23 @@ public function upsertOnCloudflare(): void 'type' => $this->record_type, ])->json(); - if (!$searchResponse['success']) { - if (!empty($searchResponse['errors'])) { - throw new Exception($searchResponse['errors'][0]['message']); - } - throw new Exception('Failed to search for existing DNS records'); - } else { + if ($searchResponse['success']) { $results = $searchResponse['result'] ?? []; - if (!empty($results)) { - foreach ($results as $record) { - if ($record['id'] !== $this->cloudflare_id) { - throw new Exception('A subdomain with that name already exists'); - } + foreach ($results as $record) { + if ($record['id'] !== $this->cloudflare_id) { + throw new Exception('A subdomain with that name already exists'); } } + } else { + if ($searchResponse['errors'] && count($searchResponse['errors']) > 0) { + throw new Exception($searchResponse['errors'][0]['message']); + } } if ($this->cloudflare_id) { // @phpstan-ignore staticMethod.notFound - $response = Http::cloudflare()->patch("zones/{$this->domain->cloudflare_id}/dns_records/{$this->cloudflare_id}", $payload)->json(); + $response = Http::cloudflare()->patch("zones/{$this->domain->cloudflare_id}/dns_records/$this->cloudflare_id", $payload)->json(); } else { // @phpstan-ignore staticMethod.notFound $response = Http::cloudflare()->post("zones/{$this->domain->cloudflare_id}/dns_records", $payload)->json(); diff --git a/subdomains/src/Services/SubdomainService.php b/subdomains/src/Services/SubdomainService.php index f52ebe64..3592993b 100644 --- a/subdomains/src/Services/SubdomainService.php +++ b/subdomains/src/Services/SubdomainService.php @@ -14,13 +14,13 @@ class SubdomainService */ public function handle(array $data, ?Subdomain $subdomain = null): Subdomain { - $newSubdomain = false; + $newSubdomain = true; if (is_null($subdomain)) { $subdomain = Subdomain::create($data); - $newSubdomain = true; } else { $subdomain->update($data); + $newSubdomain = false; } $subdomain->refresh(); @@ -31,6 +31,7 @@ public function handle(array $data, ?Subdomain $subdomain = null): Subdomain if ($newSubdomain) { $subdomain->delete(); } + throw $exception; }