Browse Source

exception handler

jqh 5 years ago
parent
commit
5f68ecd48d
5 changed files with 65 additions and 46 deletions
  1. 11 0
      src/Admin.php
  2. 2 2
      src/Form.php
  3. 1 1
      src/Grid.php
  4. 22 18
      src/Show.php
  5. 29 25
      src/Tree.php

+ 11 - 0
src/Admin.php

@@ -3,6 +3,7 @@
 namespace Dcat\Admin;
 
 use Closure;
+use Dcat\Admin\Exception\Handler;
 use Dcat\Admin\Models\HasPermissions;
 use Dcat\Admin\Controllers\AuthController;
 use Dcat\Admin\Layout\SectionManager;
@@ -319,6 +320,16 @@ class Admin
         return static::updateExtensionConfig($config);
     }
 
+    /**
+     * @return Handler
+     */
+    public static function makeExceptionHandler()
+    {
+        return app(
+            config('admin.exception_handler') ?: Handler::class
+        );
+    }
+
     /**
      * @param array $config
      * @return bool

+ 2 - 2
src/Form.php

@@ -489,7 +489,7 @@ class Form implements Renderable
                 'message' => trans('admin.delete_succeeded'),
             ];
         } catch (\Throwable $exception) {
-            $response = app(config('admin.exception_handler'))->handleDestroyException($exception);
+            $response = Admin::makeExceptionHandler()->handleDestroyException($exception);
 
             $response = $response ?: [
                 'status'  => false,
@@ -1381,7 +1381,7 @@ class Form implements Renderable
 
             return $this->builder->render();
         } catch (\Throwable $e) {
-            return app(config('admin.exception_handler'))->renderException($e);
+            return Admin::makeExceptionHandler()->renderException($e);
         }
     }
 

+ 1 - 1
src/Grid.php

@@ -1168,7 +1168,7 @@ HTML;
 
             $this->build();
         } catch (\Throwable $e) {
-            return app(config('admin.exception_handler'))->renderException($e);
+            return Admin::makeExceptionHandler()->renderException($e);
         }
 
         return $this->doWrap();

+ 22 - 18
src/Show.php

@@ -550,31 +550,35 @@ class Show implements Renderable
      */
     public function render()
     {
-        $model = $this->getModel();
+        try {
+            $model = $this->getModel();
 
-        if (is_callable($this->builder)) {
-            call_user_func($this->builder, $this);
-        }
+            if (is_callable($this->builder)) {
+                call_user_func($this->builder, $this);
+            }
 
-        if ($this->fields->isEmpty()) {
-            $this->all();
-        }
+            if ($this->fields->isEmpty()) {
+                $this->all();
+            }
 
-        if (is_array($this->builder)) {
-            $this->fields($this->builder);
-        }
+            if (is_array($this->builder)) {
+                $this->fields($this->builder);
+            }
 
-        $this->fields->each->setValue($model);
-        $this->relations->each->setModel($model);
+            $this->fields->each->setValue($model);
+            $this->relations->each->setModel($model);
 
-        $this->callComposing();
+            $this->callComposing();
 
-        $data = [
-            'panel'     => $this->panel->fill($this->fields),
-            'relations' => $this->relations,
-        ];
+            $data = [
+                'panel'     => $this->panel->fill($this->fields),
+                'relations' => $this->relations,
+            ];
 
-        return view($this->view, $data)->render();
+            return view($this->view, $data)->render();
+        } catch (\Throwable $e) {
+            return Admin::makeExceptionHandler()->renderException($e);
+        }
     }
 
     /**

+ 29 - 25
src/Tree.php

@@ -468,38 +468,42 @@ JS;
      */
     public function render()
     {
-        $this->callResolving();
+        try {
+            $this->callResolving();
 
-        if ($this->useQuickEdit) {
-            list($width, $height) = $this->dialogFormDimensions;
+            if ($this->useQuickEdit) {
+                list($width, $height) = $this->dialogFormDimensions;
 
-            Form::popup(trans('admin.edit'))
-                ->click('.tree-quick-edit')
-                ->success('LA.reload()')
-                ->dimensions($width, $height)
-                ->render();
-        }
+                Form::popup(trans('admin.edit'))
+                    ->click('.tree-quick-edit')
+                    ->success('LA.reload()')
+                    ->dimensions($width, $height)
+                    ->render();
+            }
 
-        if ($this->useQuickCreate) {
-            list($width, $height) = $this->dialogFormDimensions;
+            if ($this->useQuickCreate) {
+                list($width, $height) = $this->dialogFormDimensions;
 
-            Form::popup(trans('admin.new'))
-                ->click('.tree-quick-create')
-                ->success('LA.reload()')
-                ->dimensions($width, $height)
-                ->render();
-        }
+                Form::popup(trans('admin.new'))
+                    ->click('.tree-quick-create')
+                    ->success('LA.reload()')
+                    ->dimensions($width, $height)
+                    ->render();
+            }
 
-        Admin::script($this->script());
+            Admin::script($this->script());
 
-        view()->share([
-            'path'           => url($this->path),
-            'keyName'        => $this->model->getKeyName(),
-            'branchView'     => $this->view['branch'],
-            'branchCallback' => $this->branchCallback,
-        ]);
+            view()->share([
+                'path'           => url($this->path),
+                'keyName'        => $this->model->getKeyName(),
+                'branchView'     => $this->view['branch'],
+                'branchCallback' => $this->branchCallback,
+            ]);
 
-        return $this->doWrap();
+            return $this->doWrap();
+        } catch (\Throwable $e) {
+            return Admin::makeExceptionHandler()->renderException($e);
+        }
     }
 
     /**