jqh пре 5 година
родитељ
комит
dbb6c22640
6 измењених фајлова са 49 додато и 30 уклоњено
  1. 3 1
      resources/views/partials/exception.blade.php
  2. 37 24
      src/Exception/Handler.php
  3. 6 2
      src/Form.php
  4. 1 1
      src/Grid.php
  5. 1 1
      src/Show.php
  6. 1 1
      src/Tree.php

+ 3 - 1
resources/views/partials/exception.blade.php

@@ -7,6 +7,8 @@
             <i style="border-bottom: 1px dotted #fff;cursor: pointer;" title="{{ $error->get('type')[0] }}" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">{{ class_basename($error->get('type')[0]) }}</i>
             In <i title="{{ $error->get('file')[0] }} line {{ $error->get('line')[0] }}" style="border-bottom: 1px dotted #fff;cursor: pointer;" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">{{ basename($error->get('file')[0]) }} line {{ $error->get('line')[0] }}</i> :
         </h4>
-        <p>{!! $error->get('message')[0] !!}</p>
+        <p><a style="cursor: pointer;" onclick="$('#dcat-admin-exception-trace').toggleClass('hidden');$('i', this).toggleClass('fa-angle-double-down fa-angle-double-up');"><i class="fa fa-angle-double-down"></i>&nbsp;&nbsp;{!! $error->first('message') !!}</a></p>
+
+        <p class="hidden" id="dcat-admin-exception-trace"><br>{!! nl2br($error->first('trace')) !!}</p>
     </div>
 @endif

+ 37 - 24
src/Exception/Handler.php

@@ -2,29 +2,35 @@
 
 namespace Dcat\Admin\Exception;
 
+use Dcat\Admin\Support\Helper;
 use Illuminate\Support\MessageBag;
 use Illuminate\Support\ViewErrorBag;
 
 class Handler
 {
-    /**
-     * Render exception.
-     *
-     * @param \Exception $exception
-     *
-     * @return string
-     */
-    public function renderException(\Throwable $exception)
+    public function handle(\Throwable $e)
+    {
+        $this->report($e);
+
+        return $this->render($e);
+    }
+
+    public function render(\Throwable $exception)
     {
         if (config('app.debug')) {
             throw $exception;
         }
 
+        if (Helper::isAjaxRequest()) {
+            return;
+        }
+
         $error = new MessageBag([
             'type'    => get_class($exception),
             'message' => $exception->getMessage(),
             'file'    => $exception->getFile(),
             'line'    => $exception->getLine(),
+            'trace'   => $this->replaceBasePath($exception->getTraceAsString()),
         ]);
 
         $errors = new ViewErrorBag();
@@ -33,27 +39,34 @@ class Handler
         return view('admin::partials.exception', compact('errors'))->render();
     }
 
-    /**
-     * @param \Throwable $e
-     *
-     * @return mixed
-     */
-    public function handleDestroyException(\Throwable $e)
+    public function report(\Throwable $e)
     {
-        $root = dirname(app_path());
-
-        $context = [
-            'trace' => str_replace($root, '', $e->getTraceAsString()),
-        ];
+        $this->getLogger()->error($this->convertExceptionToString($e));
+    }
 
-        $message = sprintf(
-            '[%s] %s in %s(%s)',
+    protected function convertExceptionToString(\Throwable $e)
+    {
+        return sprintf(
+            "[%s] %s, called in %s(%s)\n%s",
             get_class($e),
             $e->getMessage(),
-            str_replace($root, '', $e->getFile()),
-            $e->getLine()
+            $this->replaceBasePath($e->getFile()),
+            $e->getLine(),
+            $this->replaceBasePath($e->getTraceAsString())
         );
+    }
 
-        logger()->error($message, $context);
+    protected function replaceBasePath(string $path)
+    {
+        return str_replace(
+            str_replace('\\', '/', base_path().'/'),
+            '',
+            str_replace('\\', '/', $path)
+        );
+    }
+
+    protected function getLogger()
+    {
+        return logger();
     }
 }

+ 6 - 2
src/Form.php

@@ -549,7 +549,11 @@ class Form implements Renderable
                 'message' => $result ? trans('admin.delete_succeeded') : trans('admin.delete_failed'),
             ];
         } catch (\Throwable $exception) {
-            $response = Admin::makeExceptionHandler()->handleDestroyException($exception);
+            $response = Admin::makeExceptionHandler()->handle($exception);
+
+            if ($response instanceof Response) {
+                return $response;
+            }
 
             $response = $response ?: [
                 'status'  => false,
@@ -1586,7 +1590,7 @@ class Form implements Renderable
 
             return $this->builder->render();
         } catch (\Throwable $e) {
-            return Admin::makeExceptionHandler()->renderException($e);
+            return Admin::makeExceptionHandler()->handle($e);
         }
     }
 

+ 1 - 1
src/Grid.php

@@ -879,7 +879,7 @@ HTML;
 
             $this->build();
         } catch (\Throwable $e) {
-            return Admin::makeExceptionHandler()->renderException($e);
+            return Admin::makeExceptionHandler()->handle($e);
         }
 
         return $this->doWrap();

+ 1 - 1
src/Show.php

@@ -695,7 +695,7 @@ class Show implements Renderable
 
             return view($this->view, $data)->render();
         } catch (\Throwable $e) {
-            return Admin::makeExceptionHandler()->renderException($e);
+            return Admin::makeExceptionHandler()->handle($e);
         }
     }
 

+ 1 - 1
src/Tree.php

@@ -546,7 +546,7 @@ JS;
 
             return $this->doWrap();
         } catch (\Throwable $e) {
-            return Admin::makeExceptionHandler()->renderException($e);
+            return Admin::makeExceptionHandler()->handle($e);
         }
     }