Jelajahi Sumber

Delete server on 404 instead of giving 500

ok236449 2 tahun lalu
induk
melakukan
c26a47a982
2 mengubah file dengan 10 tambahan dan 4 penghapusan
  1. 8 2
      app/Classes/Pterodactyl.php
  2. 2 2
      app/Http/Controllers/ServerController.php

+ 8 - 2
app/Classes/Pterodactyl.php

@@ -287,7 +287,7 @@ class Pterodactyl
      * @param int $pterodactylId
      * @return mixed
      */
-    public static function getServerAttributes(int $pterodactylId)
+    public static function getServerAttributes(int $pterodactylId, bool $deleteOn404 = false)
     {
         try {
             $response = self::client()->get("/application/servers/{$pterodactylId}?include=egg,node,nest,location");
@@ -299,7 +299,13 @@ class Pterodactyl
 
 
 
-        if ($response->failed()) throw self::getException("Failed to get server attributes from pterodactyl - ", $response->status());
+        if ($response->failed()){
+            if($deleteOn404){  //Delete the server if it does not exist (server deleted on pterodactyl)
+                Server::where('pterodactyl_id', $pterodactylId)->first()->delete();
+                return;
+            }
+            else throw self::getException("Failed to get server attributes from pterodactyl - ", $response->status());
+        }
         return $response->json()['attributes'];
     }
 

+ 2 - 2
app/Http/Controllers/ServerController.php

@@ -30,8 +30,8 @@ class ServerController extends Controller
         foreach ($servers as $server) {
 
             //Get server infos from ptero
-            $serverAttributes = Pterodactyl::getServerAttributes($server->pterodactyl_id);
-
+            $serverAttributes = Pterodactyl::getServerAttributes($server->pterodactyl_id, true);
+            if(!$serverAttributes) continue;
             $serverRelationships = $serverAttributes['relationships'];
             $serverLocationAttributes = $serverRelationships['location']['attributes'];