Skip to content

Commit

Permalink
add status key for http error responses
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmorbo committed Feb 19, 2023
1 parent 6130dd8 commit a5e56dc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
16 changes: 8 additions & 8 deletions src/Controller/InstanceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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));
}
}

Expand All @@ -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', [
Expand Down Expand Up @@ -150,7 +150,7 @@ function() use ($instance, $instanceId) {
);
},
function ($error) {
return new JsonResponse(['error' => $error]);
return new JsonResponse(['status' => 'error', 'error' => $error]);
}
);
}
Expand All @@ -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]);
}
);
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit a5e56dc

Please sign in to comment.