Browse Source

Envoi d'une 404 propre en cas de get sur un id inexistant

Sangfroid 1 year ago
parent
commit
1d9f39cbce
1 changed files with 12 additions and 2 deletions
  1. 12 2
      src/Controller/CounterController.php

+ 12 - 2
src/Controller/CounterController.php

@@ -64,9 +64,19 @@ class CounterController extends AbstractController
     }
 
     #[Route('/{id}', name: 'app_counter_id', methods: ['GET'])]
-    public function getCounter(Counter $counter): JsonResponse
+    public function getCounter(?Counter $counter = null): JsonResponse
     {
-        return $this->json($counter, 200);
+        if (!$counter) {
+            $counter = [
+                'error' => [
+                    'code' => 404,
+                    'message' => 'La ressource n\'existe pas'
+                ]
+                ];
+            $code = 404;
+        }
+
+        return $this->json($counter, $code ?? 200);
     }
 
     #[Route('/clear/{id}', name: 'app_counter_clear')]