소스 검색

:hammer: 重构异常处理功能

jqh 4 년 전
부모
커밋
da7b53f330
11개의 변경된 파일103개의 추가작업 그리고 30개의 파일을 삭제
  1. 32 5
      src/Admin.php
  2. 3 0
      src/AdminServiceProvider.php
  3. 31 0
      src/Contracts/ExceptionHandler.php
  4. 29 19
      src/Exception/Handler.php
  5. 1 1
      src/Extend/Manager.php
  6. 1 1
      src/Form.php
  7. 1 1
      src/Grid.php
  8. 1 1
      src/Http/Forms/InstallFromLocal.php
  9. 1 1
      src/Show.php
  10. 2 0
      src/Support/Setting.php
  11. 1 1
      src/Tree.php

+ 32 - 5
src/Admin.php

@@ -3,6 +3,7 @@
 namespace Dcat\Admin;
 
 use Closure;
+use Dcat\Admin\Contracts\ExceptionHandler;
 use Dcat\Admin\Contracts\Repository;
 use Dcat\Admin\Exception\InvalidArgumentException;
 use Dcat\Admin\Http\Controllers\AuthController;
@@ -238,13 +239,39 @@ class Admin
     }
 
     /**
-     * @return Handler
+     * 处理异常.
+     *
+     * @param \Throwable $e
+     *
+     * @return mixed
+     */
+    public static function handleException(\Throwable $e)
+    {
+        return app(ExceptionHandler::class)->handle($e);
+    }
+
+    /**
+     * 上报异常.
+     *
+     * @param \Throwable $e
+     *
+     * @return mixed
+     */
+    public static function reportException(\Throwable $e)
+    {
+        return app(ExceptionHandler::class)->report($e);
+    }
+
+    /**
+     * 显示异常信息.
+     *
+     * @param \Throwable $e
+     *
+     * @return mixed
      */
-    public static function makeExceptionHandler()
+    public static function renderException(\Throwable $e)
     {
-        return app(
-            config('admin.exception_handler') ?: Handler::class
-        );
+        return app(ExceptionHandler::class)->render($e);
     }
 
     /**

+ 3 - 0
src/AdminServiceProvider.php

@@ -3,6 +3,8 @@
 namespace Dcat\Admin;
 
 use Dcat\Admin\Console\ExtensionInstallCommand;
+use Dcat\Admin\Contracts\ExceptionHandler;
+use Dcat\Admin\Exception\Handler;
 use Dcat\Admin\Extend\UpdateManager;
 use Dcat\Admin\Extend\VersionManager;
 use Dcat\Admin\Layout\Asset;
@@ -230,6 +232,7 @@ class AdminServiceProvider extends ServiceProvider
             return Setting::fromDatabase();
         });
         $this->app->singleton('admin.web-uploader', WebUploader::class);
+        $this->app->singleton(ExceptionHandler::class, config('admin.exception_handler') ?: Handler::class);
     }
 
     protected function registerExtensions()

+ 31 - 0
src/Contracts/ExceptionHandler.php

@@ -0,0 +1,31 @@
+<?php
+
+namespace Dcat\Admin\Contracts;
+
+interface ExceptionHandler
+{
+    /**
+     * 处理异常.
+     *
+     * @param \Throwable $e
+     *
+     * @return array|string|void
+     */
+    public function handle(\Throwable $e);
+
+    /**
+     * 显示异常信息.
+     *
+     * @param \Throwable $exception
+     *
+     * @return array|string|void
+     */
+    public function render(\Throwable $exception);
+
+    /**
+     * 上报异常信息.
+     *
+     * @param \Throwable $e
+     */
+    public function report(\Throwable $e);
+}

+ 29 - 19
src/Exception/Handler.php

@@ -2,12 +2,20 @@
 
 namespace Dcat\Admin\Exception;
 
+use Dcat\Admin\Contracts\ExceptionHandler;
 use Dcat\Admin\Support\Helper;
 use Illuminate\Support\MessageBag;
 use Illuminate\Support\ViewErrorBag;
 
-class Handler
+class Handler implements ExceptionHandler
 {
+    /**
+     * 处理异常.
+     *
+     * @param \Throwable $e
+     *
+     * @return array|string|void
+     */
     public function handle(\Throwable $e)
     {
         $this->report($e);
@@ -15,6 +23,15 @@ class Handler
         return $this->render($e);
     }
 
+    /**
+     * 显示异常信息.
+     *
+     * @param \Throwable $exception
+     *
+     * @return array|string|void
+     *
+     * @throws \Throwable
+     */
     public function render(\Throwable $exception)
     {
         if (config('app.debug')) {
@@ -39,23 +56,21 @@ class Handler
         return view('admin::partials.exception', compact('errors'))->render();
     }
 
+    /**
+     * 上报异常信息.
+     *
+     * @param \Throwable $e
+     */
     public function report(\Throwable $e)
     {
-        $this->logger()->error($this->convertExceptionToString($e));
-    }
-
-    protected function convertExceptionToString(\Throwable $e)
-    {
-        return sprintf(
-            "[%s] %s, called in %s(%s)\n%s",
-            get_class($e),
-            $e->getMessage(),
-            $this->replaceBasePath($e->getFile()),
-            $e->getLine(),
-            $this->replaceBasePath($e->getTraceAsString())
-        );
+        report($e);
     }
 
+    /**
+     * @param string $path
+     *
+     * @return mixed
+     */
     protected function replaceBasePath(string $path)
     {
         return str_replace(
@@ -64,9 +79,4 @@ class Handler
             str_replace('\\', '/', $path)
         );
     }
-
-    protected function logger()
-    {
-        return logger();
-    }
 }

+ 1 - 1
src/Extend/Manager.php

@@ -469,6 +469,6 @@ class Manager
      */
     protected function reportException(\Throwable $e)
     {
-        report($e);
+        Admin::reportException($e);
     }
 }

+ 1 - 1
src/Form.php

@@ -576,7 +576,7 @@ class Form implements Renderable
      */
     protected function handleException(\Throwable $e)
     {
-        return Admin::makeExceptionHandler()->handle($e);
+        return Admin::handleException($e);
     }
 
     /**

+ 1 - 1
src/Grid.php

@@ -973,7 +973,7 @@ HTML;
 
     protected function handleException(\Throwable $e)
     {
-        return Admin::makeExceptionHandler()->handle($e);
+        return Admin::handleException($e);
     }
 
     /**

+ 1 - 1
src/Http/Forms/InstallFromLocal.php

@@ -47,7 +47,7 @@ class InstallFromLocal extends Form implements LazyRenderable
                 ->success(implode('<br>', $manager->updateManager()->notes))
                 ->refresh();
         } catch (\Throwable $e) {
-            report($e);
+            Admin::reportException($e);
 
             return $this->response()->error($e->getMessage());
         } finally {

+ 1 - 1
src/Show.php

@@ -707,7 +707,7 @@ class Show implements Renderable
 
     protected function handleException(\Throwable $e)
     {
-        return Admin::makeExceptionHandler()->handle($e);
+        return Admin::handleException($e);
     }
 
     /**

+ 2 - 0
src/Support/Setting.php

@@ -2,6 +2,7 @@
 
 namespace Dcat\Admin\Support;
 
+use Dcat\Admin\Admin;
 use Dcat\Admin\Models\Setting as Model;
 use Illuminate\Database\QueryException;
 use Illuminate\Support\Arr;
@@ -81,6 +82,7 @@ class Setting extends Fluent
         try {
             $values = Model::pluck('value', 'slug')->toArray();
         } catch (QueryException $e) {
+            Admin::reportException($e);
         }
 
         return new static($values);

+ 1 - 1
src/Tree.php

@@ -577,7 +577,7 @@ JS;
 
     protected function handleException(\Throwable $e)
     {
-        return Admin::makeExceptionHandler()->handle($e);
+        return Admin::handleException($e);
     }
 
     /**