From a5e56dc2bebe19efdd0749387837bc6689dadb44 Mon Sep 17 00:00:00 2001 From: Aleksandr Nikitin Date: Sun, 19 Feb 2023 23:12:20 +0300 Subject: [PATCH] add status key for http error responses --- src/Controller/InstanceController.php | 16 ++++++++-------- src/Server.php | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Controller/InstanceController.php b/src/Controller/InstanceController.php index 76af439..37dff16 100644 --- a/src/Controller/InstanceController.php +++ b/src/Controller/InstanceController.php @@ -71,7 +71,7 @@ public function getInstance(ServerRequest $request, Response $response, string $ ->then( function ($result) use ($instanceId) { if (!$result) { - return new JsonResponse(['error' => 'Instance not found'], 404); + return new JsonResponse(['status' => 'error', 'error' => 'Instance not found'], 404); } return $this->trassirHelper->getInstance($instanceId) @@ -97,7 +97,7 @@ public function addInstance(ServerRequest $request, Response $response): Promise ) { $input = json_decode($request->getBody()->getContents(), true); if (json_last_error() !== JSON_ERROR_NONE) { - return resolve(new JsonResponse(['error' => 'Invalid JSON'], 400)); + return resolve(new JsonResponse(['status' => 'error', 'error' => 'Invalid JSON'], 400)); } } @@ -108,7 +108,7 @@ public function addInstance(ServerRequest $request, Response $response): Promise empty($input['login']) || empty($input['password']) ) { - return resolve(new JsonResponse(['error' => 'Invalid data'], 400)); + return resolve(new JsonResponse(['status' => 'error', 'error' => 'Invalid data'], 400)); } $instanceId = $this->dbInsert('instances', [ @@ -150,7 +150,7 @@ function() use ($instance, $instanceId) { ); }, function ($error) { - return new JsonResponse(['error' => $error]); + return new JsonResponse(['status' => 'error', 'error' => $error]); } ); } @@ -165,14 +165,14 @@ public function deleteInstance(ServerRequest $request, Response $response, $inpu function (int $deletedRows) { if ($deletedRows === 0) { return new JsonResponse( - ['error' => 'Instance not found'], StatusCodeInterface::STATUS_NOT_FOUND + ['status' => 'error', 'error' => 'Instance not found'], StatusCodeInterface::STATUS_NOT_FOUND ); } return new Response(StatusCodeInterface::STATUS_NO_CONTENT); }, function ($error) { - return new JsonResponse(['error' => $error]); + return new JsonResponse(['status' => 'error', 'error' => $error]); } ); } @@ -218,7 +218,7 @@ public function getChannelVideo( $instanceId = (int)$instanceId; if (!in_array($streamType, ['hls', 'rtsp'])) { - return resolve(new JsonResponse(['error' => 'Invalid stream type'], 400)); + return resolve(new JsonResponse(['status' => 'error', 'error' => 'Invalid stream type'], 400)); } return $this->trassirHelper->getInstance($instanceId) @@ -261,7 +261,7 @@ function ($video) use ($request, $response) { return $response; }, function (Exception $e) { - return new JsonResponse(['error' => $e->getMessage()], 404); + return new JsonResponse(['status' => 'error', 'error' => $e->getMessage()], 404); } ); } diff --git a/src/Server.php b/src/Server.php index ff99b2e..e92766a 100644 --- a/src/Server.php +++ b/src/Server.php @@ -131,7 +131,7 @@ private function initServer(string $ip, int $port): int $router ->map404("/(.*)", function () { - return resolve(new JsonResponse(['error' => 'Route not found'], 404)); + return resolve(new JsonResponse(['status' => 'error', 'error' => 'Route not found'], 404)); }) ->map500("/(.*)", function () { return resolve(new TextResponse("An internal error has occurred", 500));