浏览代码

Dont crash if unable to fetch from factory

shalvah 3 年之前
父节点
当前提交
a768c4733a

+ 11 - 0
src/Exceptions/CouldntFindFactory.php

@@ -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?");
+    }
+}

+ 5 - 5
src/Extracting/Strategies/Responses/UseApiResourceTags.php

@@ -20,8 +20,8 @@ use Knuckles\Scribe\Tools\AnnotationParser as a;
 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 ).
@@ -187,23 +187,23 @@ class UseApiResourceTags extends Strategy
 
             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);

+ 5 - 4
src/Extracting/Strategies/Responses/UseTransformerTags.php

@@ -20,6 +20,7 @@ use League\Fractal\Resource\Item;
 use Mpociot\Reflection\DocBlock\Tag;
 use ReflectionClass;
 use ReflectionFunctionAbstract;
+use Throwable;
 
 /**
  * Parse a transformer response from the docblock ( @transformer || @transformercollection ).
@@ -159,23 +160,23 @@ class UseTransformerTags extends Strategy
 
             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);

+ 4 - 2
src/Tools/Utils.php

@@ -8,6 +8,7 @@ use Exception;
 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 ReflectionClass;
 use ReflectionException;
 use ReflectionFunction;
 use ReflectionFunctionAbstract;
+use Throwable;
 
 class Utils
 {
@@ -199,9 +201,9 @@ class Utils
         } 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;
                 }