Skip to content

Commit

Permalink
独自例外用 ExceptionHandler のカスタマイズ
Browse files Browse the repository at this point in the history
  • Loading branch information
okashoi committed Mar 17, 2019
1 parent 00fd572 commit 00bac40
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class Handler extends ExceptionHandler
'password_confirmation',
];

/**
* @var array
*/
protected $additionalContext = [];

/**
* Report or log an exception.
*
Expand All @@ -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.
*
Expand All @@ -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);
}
}

0 comments on commit 00bac40

Please sign in to comment.