From b2e69220eaad96c2a41d6f522f78a6816ccc9f3f Mon Sep 17 00:00:00 2001 From: Jasper Zonneveld Date: Wed, 29 Mar 2023 11:10:22 +0200 Subject: [PATCH] feat: throw meaningful exception when no resources are found --- src/ResourceContainer.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/ResourceContainer.php b/src/ResourceContainer.php index b66a65c..a12ba05 100644 --- a/src/ResourceContainer.php +++ b/src/ResourceContainer.php @@ -30,7 +30,13 @@ public function resource($model): JsonApiResource $this->loadResources($fqn); } - return $this->resources[$fqn]->first(); + $resource = $this->resources[$fqn]->first(); + + if (!$resource) { + throw new \RuntimeException(sprintf('No resource found for model [%s], make sure your database is seeded!', $fqn)); + } + + return $resource; } /** @@ -45,7 +51,13 @@ public function resources($model): array $this->loadResources($fqn); } - return $this->resources[$fqn]->toArray(); + $resources = $this->resources[$fqn]->toArray(); + + if (empty($resources)) { + throw new \RuntimeException(sprintf('No resources found for model [%s], make sure your database is seeded!', $fqn)); + } + + return $resources; } protected function getFQN($model): string