From 5eedfdd3fec10b9aaeb7d7cc76c0edc9909a6d8d Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Sun, 8 Mar 2020 17:38:51 +0200 Subject: [PATCH] Better error handling --- app/controllers/api/database.php | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/app/controllers/api/database.php b/app/controllers/api/database.php index 04edc2ea79..acec83c99a 100644 --- a/app/controllers/api/database.php +++ b/app/controllers/api/database.php @@ -70,6 +70,10 @@ $utopia->post('/v1/database/collections') throw new Exception('Failed saving document to DB', 500); } + if (false === $data) { + throw new Exception('Failed saving collection to DB', 500); + } + $data = $data->getArrayCopy(); $webhook @@ -193,16 +197,24 @@ $utopia->put('/v1/database/collections/:collectionId') ], $rule); } - $collection = $projectDB->updateDocument(array_merge($collection->getArrayCopy(), [ - 'name' => $name, - 'structure' => true, - 'dateUpdated' => time(), - '$permissions' => [ - 'read' => $read, - 'write' => $write, - ], - 'rules' => $rules, - ])); + try { + $collection = $projectDB->updateDocument(array_merge($collection->getArrayCopy(), [ + 'name' => $name, + 'structure' => true, + 'dateUpdated' => time(), + '$permissions' => [ + 'read' => $read, + 'write' => $write, + ], + 'rules' => $parsedRules, + ])); + } catch (AuthorizationException $exception) { + throw new Exception('Unauthorized action', 401); + } catch (StructureException $exception) { + throw new Exception('Bad structure. '.$exception->getMessage(), 400); + } catch (\Exception $exception) { + throw new Exception('Failed saving document to DB', 500); + } if (false === $collection) { throw new Exception('Failed saving collection to DB', 500);