Return errors from set op
phpunit / Integration Tests (push) Successful in 11m30s

This commit is contained in:
2024-09-28 01:59:49 +02:00
parent 3d5ba64f87
commit ee8ed2736c
+16 -5
View File
@@ -72,12 +72,21 @@ class Daemon
$collectionName = array_shift($paths);
$collectionOp = array_shift($paths);
try {
return $this->handleRequest($collectionName, $collectionOp, $request);
}
catch (\Throwable $t) {
return Response::plaintext($t)->withStatus(500);
}
}
private function handleRequest(?string $collectionName, ?string $collectionOp, ServerRequestInterface $request): ResponseInterface
{
if ($collectionName == null) {
$collections = $this->db->getCollectionNames();
return Response::json($collections);
}
switch ($collectionOp) {
case null:
@@ -89,8 +98,7 @@ class Daemon
}
return Response::json($result);
} elseif ($request->getMethod() == 'POST') {
$this->opCollectionSet($request, $collectionName);
return Response::json(true);
return $this->opCollectionSet($request, $collectionName);
}
return Response::json([ 'error' => "Invalid request method" ]);
@@ -182,7 +190,7 @@ class Daemon
}
}
private function opCollectionSet(ServerRequestInterface $request, string $collectionName): void
private function opCollectionSet(ServerRequestInterface $request, string $collectionName): ResponseInterface
{
$collection = $this->db->getOrCreateCollection($collectionName);
@@ -197,7 +205,7 @@ class Daemon
foreach ((array)$body as $change) {
if (!(isset($change->key) && isset($change->value))) {
throw new \Exception("Missing key or value on update item");
return Response::json([ 'error' => "Missing key or value on update item" ])->withStatus(500);
}
// echo "Applying change (collection={$collectionName}): ".json_encode($change)."\n";
$id = $change->id??null;
@@ -207,7 +215,10 @@ class Daemon
$validUntil = isset($change->validity->until) ? new DateTime($change->validity->until) : null;
$collection->setValue($name, $value, $validFrom, $validUntil, $id);
}
return Response::json(true);
}
}