From 00bac407fd35bf65ef65ea6ae7482ecd6e363118 Mon Sep 17 00:00:00 2001 From: Okada Shohei Date: Mon, 18 Mar 2019 08:36:07 +0900 Subject: [PATCH] =?UTF-8?q?=E7=8B=AC=E8=87=AA=E4=BE=8B=E5=A4=96=E7=94=A8?= =?UTF-8?q?=20ExceptionHandler=20=E3=81=AE=E3=82=AB=E3=82=B9=E3=82=BF?= =?UTF-8?q?=E3=83=9E=E3=82=A4=E3=82=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/Exceptions/Handler.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/app/Exceptions/Handler.php b/src/app/Exceptions/Handler.php index 043cad6..2b62122 100644 --- a/src/app/Exceptions/Handler.php +++ b/src/app/Exceptions/Handler.php @@ -26,6 +26,11 @@ class Handler extends ExceptionHandler 'password_confirmation', ]; + /** + * @var array + */ + protected $additionalContext = []; + /** * Report or log an exception. * @@ -34,9 +39,23 @@ class Handler extends ExceptionHandler */ public function report(Exception $exception) { + if ($exception instanceof MyAppException) { + $this->additionalContext = $exception->getContext(); + } + parent::report($exception); } + /** + * Get the default context variables for logging. + * + * @return array + */ + protected function context() + { + return array_merge(parent::context(), $this->additionalContext); + } + /** * Render an exception into an HTTP response. * @@ -46,6 +65,11 @@ public function report(Exception $exception) */ public function render($request, Exception $exception) { + // 既存の render の仕組みを活用するため、HttpException に変換する + if ($exception instanceof MyAppException) { + $exception = $exception->toHttpException(); + } + return parent::render($request, $exception); } }