diff --git a/src/Exceptions/CouldntFindFactory.php b/src/Exceptions/CouldntFindFactory.php new file mode 100644 index 00000000..bf21acd4 --- /dev/null +++ b/src/Exceptions/CouldntFindFactory.php @@ -0,0 +1,11 @@ +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); diff --git a/src/Extracting/Strategies/Responses/UseTransformerTags.php b/src/Extracting/Strategies/Responses/UseTransformerTags.php index bb702c3f..183c7d24 100644 --- a/src/Extracting/Strategies/Responses/UseTransformerTags.php +++ b/src/Extracting/Strategies/Responses/UseTransformerTags.php @@ -20,6 +20,7 @@ use Mpociot\Reflection\DocBlock\Tag; use ReflectionClass; use ReflectionFunctionAbstract; +use Throwable; /** * Parse a transformer response from the docblock ( @transformer || @transformercollection ). @@ -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); diff --git a/src/Tools/Utils.php b/src/Tools/Utils.php index 2945169a..96cddda0 100644 --- a/src/Tools/Utils.php +++ b/src/Tools/Utils.php @@ -8,6 +8,7 @@ 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; @@ -15,6 +16,7 @@ use ReflectionException; use ReflectionFunction; use ReflectionFunctionAbstract; +use Throwable; class Utils { @@ -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; }