Skip to content

Commit

Permalink
Dont crash if unable to fetch from factory
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed Jul 28, 2021
1 parent 3189f48 commit a768c47
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
11 changes: 11 additions & 0 deletions src/Exceptions/CouldntFindFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Knuckles\Scribe\Exceptions;

class CouldntFindFactory extends \RuntimeException implements ScribeException
{
public static function forModel(string $modelName): CouldntFindFactory
{
return new self("Couldn't find the Eloquent model factory. Did you add the HasFactory trait to your $modelName model?");
}
}
10 changes: 5 additions & 5 deletions src/Extracting/Strategies/Responses/UseApiResourceTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
use Knuckles\Scribe\Tools\ConsoleOutputUtils as c;
use Knuckles\Scribe\Tools\ErrorHandlingUtils as e;
use Knuckles\Scribe\Tools\Utils;
use Mpociot\Reflection\DocBlock;
use Mpociot\Reflection\DocBlock\Tag;
use Throwable;

/**
* Parse an Eloquent API resource response from the docblock ( @apiResource || @apiResourcecollection ).
Expand Down Expand Up @@ -187,23 +187,23 @@ protected function instantiateApiResourceModel(string $type, array $factoryState

try {
return $factory->create();
} catch (Exception $e) {
} catch (Throwable $e) {
// If there was no working database, ->create() would fail. Try ->make() instead
return $factory->make();
}
} catch (Exception $e) {
} catch (Throwable $e) {
c::warn("Eloquent model factory failed to instantiate {$type}; trying to fetch from database.");
e::dumpExceptionIfVerbose($e, true);

$instance = new $type();
if ($instance instanceof \Illuminate\Database\Eloquent\Model) {
try {
// we can't use a factory but can try to get one from the database
// We can't use a factory but can try to get one from the database
$firstInstance = $type::with($relations)->first();
if ($firstInstance) {
return $firstInstance;
}
} catch (Exception $e) {
} catch (Throwable $e) {
// okay, we'll stick with `new`
c::warn("Failed to fetch first {$type} from database; using `new` to instantiate.");
e::dumpExceptionIfVerbose($e);
Expand Down
9 changes: 5 additions & 4 deletions src/Extracting/Strategies/Responses/UseTransformerTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Mpociot\Reflection\DocBlock\Tag;
use ReflectionClass;
use ReflectionFunctionAbstract;
use Throwable;

/**
* Parse a transformer response from the docblock ( @transformer || @transformercollection ).
Expand Down Expand Up @@ -159,23 +160,23 @@ protected function instantiateTransformerModel(string $type, array $factoryState

try {
return $factory->create();
} catch (Exception $e) {
} catch (Throwable $e) {
// If there was no working database, ->create() would fail. Try ->make() instead
return $factory->make();
}
} catch (Exception $e) {
} catch (Throwable $e) {
c::warn("Eloquent model factory failed to instantiate {$type}; trying to fetch from database.");
e::dumpExceptionIfVerbose($e, true);

$instance = new $type();
if ($instance instanceof IlluminateModel) {
try {
// we can't use a factory but can try to get one from the database
// We can't use a factory but can try to get one from the database
$firstInstance = $type::with($relations)->first();
if ($firstInstance) {
return $firstInstance;
}
} catch (Exception $e) {
} catch (Throwable $e) {
// okay, we'll stick with `new`
c::warn("Failed to fetch first {$type} from database; using `new` to instantiate.");
e::dumpExceptionIfVerbose($e);
Expand Down
6 changes: 4 additions & 2 deletions src/Tools/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
use FastRoute\RouteParser\Std;
use Illuminate\Routing\Route;
use Illuminate\Support\Str;
use Knuckles\Scribe\Exceptions\CouldntFindFactory;
use Knuckles\Scribe\Tools\ConsoleOutputUtils as c;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use ReflectionClass;
use ReflectionException;
use ReflectionFunction;
use ReflectionFunctionAbstract;
use Throwable;

class Utils
{
Expand Down Expand Up @@ -199,9 +201,9 @@ public static function getModelFactory(string $modelName, array $states = [], ar
} else {
try {
$factory = factory($modelName);
} catch (\Throwable $e) {
} catch (Throwable $e) {
if (Str::contains($e->getMessage(), "Call to undefined function Knuckles\\Scribe\\Tools\\factory()")) {
throw new \Exception("Couldn't find the Eloquent model factory. Did you add the HasFactory trait to your $modelName model?");
throw CouldntFindFactory::forModel($modelName);
} else {
throw $e;
}
Expand Down

0 comments on commit a768c47

Please sign in to comment.