Skip to content

Commit 1209ce7

Browse files
committed
Working on exception handling. WIP.
1 parent 3c614ac commit 1209ce7

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

app/Console/Kernel.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public function handle($input, $output = null)
2929
}
3030
catch (Exception $e)
3131
{
32-
$output->writeln((string) $e);
32+
$this->reportException($e);
33+
34+
$this->renderException($output, $e);
3335

3436
return 1;
3537
}

app/Http/Kernel.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ public function handle($request)
3434
}
3535
catch (Exception $e)
3636
{
37-
throw $e;
37+
$this->reportException($e);
38+
39+
return $this->renderException($request, $e);
3840
}
3941
}
4042

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php namespace App\Infrastructure;
2+
3+
use Exception;
4+
use Psr\Log\LoggerInterface;
5+
use Symfony\Component\Debug\ExceptionHandler as SymfonyDisplayer;
6+
use Illuminate\Contracts\Debug\ExceptionHandler as ExceptionHandlerContract;
7+
8+
class ExceptionHandler implements ExceptionHandlerContract {
9+
10+
/**
11+
* The log implementation.
12+
*
13+
* @var \Psr\Log\LoggerInterface
14+
*/
15+
protected $log;
16+
17+
/**
18+
* Create a new exception handler instance.
19+
*
20+
* @param \Psr\Log\LoggerInterface $log
21+
* @return void
22+
*/
23+
public function __construct(LoggerInterface $log)
24+
{
25+
$this->log = $log;
26+
}
27+
28+
/**
29+
* Report or log an exception.
30+
*
31+
* @param \Exception $e
32+
* @return void
33+
*/
34+
public function report(Exception $e)
35+
{
36+
$this->log->error((string) $e);
37+
}
38+
39+
/**
40+
* Render an exception into a response.
41+
*
42+
* @param \Illuminate\Http\Request $request
43+
* @param \Exception $e
44+
* @return \Symfony\Component\HttpFoundation\Response
45+
*/
46+
public function render($request, Exception $e)
47+
{
48+
return (new SymfonyDisplayer)->createResponse($e);
49+
}
50+
51+
/**
52+
* Render an exception to the console.
53+
*
54+
* @param \Symfony\Component\Console\Output\OutputInterface $output
55+
* @param \Exception $e
56+
* @return void
57+
*/
58+
public function renderForConsole($output, Exception $e)
59+
{
60+
$output->writeln((string) $e);
61+
}
62+
63+
}

bootstrap/app.php

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
'App\Console\Kernel'
3737
);
3838

39+
$app->singleton(
40+
'Illuminate\Contracts\Debug\ExceptionHandler',
41+
'App\Infrastructure\ExceptionHandler'
42+
);
43+
3944
/*
4045
|--------------------------------------------------------------------------
4146
| Return The Application

0 commit comments

Comments
 (0)