1
|
|
|
<?php namespace Anomaly\Streams\Platform\Exception; |
2
|
|
|
|
3
|
|
|
use Exception; |
4
|
|
|
use GrahamCampbell\Exceptions\NewExceptionHandler; |
5
|
|
|
use Illuminate\Auth\AuthenticationException; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Illuminate\Http\Response; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class ExceptionHandler |
11
|
|
|
* |
12
|
|
|
* @link http://pyrocms.com/ |
13
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
14
|
|
|
* @author Ryan Thompson <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class ExceptionHandler extends NewExceptionHandler |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* A list of the exception types that should not be reported. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $dontReport = [ |
25
|
|
|
\Illuminate\Auth\AuthenticationException::class, |
26
|
|
|
\Illuminate\Auth\Access\AuthorizationException::class, |
27
|
|
|
\Symfony\Component\HttpKernel\Exception\HttpException::class, |
28
|
|
|
\Illuminate\Database\Eloquent\ModelNotFoundException::class, |
29
|
|
|
\Illuminate\Session\TokenMismatchException::class, |
30
|
|
|
\Illuminate\Validation\ValidationException::class, |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Render an exception into an HTTP response. |
35
|
|
|
* |
36
|
|
|
* @param Request $request |
37
|
|
|
* @param Exception $e |
38
|
|
|
* @return Response |
39
|
|
|
*/ |
40
|
|
|
public function render($request, Exception $e) |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* Have to catch this for some reason. |
44
|
|
|
* Not sure why our handler passes this. |
45
|
|
|
* |
46
|
|
|
* @todo: Clean up |
47
|
|
|
*/ |
48
|
|
View Code Duplication |
if ($e instanceof AuthenticationException) { |
|
|
|
|
49
|
|
|
if ($request->segment(1) === 'admin') { |
50
|
|
|
return redirect()->guest('admin/login'); |
|
|
|
|
51
|
|
|
} else { |
52
|
|
|
return redirect()->guest('login'); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return parent::render($request, $e); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Convert an authentication exception into an unauthenticated response. |
61
|
|
|
* |
62
|
|
|
* @param \Illuminate\Http\Request $request |
63
|
|
|
* @param \Illuminate\Auth\AuthenticationException $exception |
64
|
|
|
* @return \Illuminate\Http\Response |
65
|
|
|
*/ |
66
|
|
|
protected function unauthenticated($request, AuthenticationException $exception) |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
if ($request->expectsJson()) { |
69
|
|
|
return response()->json(['error' => 'Unauthenticated.'], 401); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
View Code Duplication |
if ($request->segment(1) === 'admin') { |
|
|
|
|
73
|
|
|
return redirect()->guest('admin/login'); |
74
|
|
|
} else { |
75
|
|
|
return redirect()->guest('login'); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.