forked from ushahidi/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandler.php
More file actions
141 lines (122 loc) · 4.71 KB
/
Copy pathHandler.php
File metadata and controls
141 lines (122 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
namespace Ushahidi\App\Exceptions;
use Exception;
use Illuminate\Validation\ValidationException as IlluminateValidationException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Illuminate\Http\Exceptions\HttpResponseException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use League\OAuth2\Server\Exception\OAuthServerException;
use Illuminate\Auth\AuthenticationException;
use Asm89\Stack\CorsService;
use Illuminate\Http\Request;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
AuthorizationException::class,
AuthenticationException::class,
HttpException::class,
ModelNotFoundException::class,
IlluminateValidationException::class,
OAuthServerException::class,
];
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $e
* @return void
*/
public function report(Exception $e)
{
if (app()->bound('sentry') && $this->shouldReport($e)) {
app('sentry')->captureException($e);
}
parent::report($e);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
// @todo we should try app('request') first but we can't guarantee its been created
$request = Request::capture();
// First handle some special cases
if ($e instanceof HttpResponseException) {
// @todo check if we should still reformat this for json
return $e->getResponse();
} elseif ($e instanceof ModelNotFoundException) {
$e = new NotFoundHttpException($e->getMessage(), $e);
} elseif ($e instanceof AuthorizationException) {
$e = new HttpException(403, $e->getMessage());
} elseif ($e instanceof AuthenticationException) {
$e = new HttpException(401, $e->getMessage());
} elseif ($e instanceof IlluminateValidationException && $e->getResponse()) {
// @todo check if we should still reformat this for json
return $e->getResponse();
}
// If request asks for JSON then we return the error as JSON
if ($request->ajax() || $request->wantsJson()) {
$statusCode = 500;
$headers = [];
if ($e instanceof HttpExceptionInterface) {
$statusCode = $e->getStatusCode();
$headers = $e->getHeaders();
}
$defaultError = [
'status' => $statusCode
];
$message = $e->getMessage();
if ($message) {
if (is_object($message)) {
$message = $message->toArray();
}
$defaultError['message'] = $message;
}
$errors = [];
$errors[] = $defaultError;
if ($e instanceof ValidationException) {
foreach ($e->getErrors() as $key => $value) {
$errors[] = [
'status' => $statusCode,
'title' => $value,
'message' => $value,
'source' => [
'pointer' => "/" . $key
]
];
}
}
$response = response()->json([
'errors' => $errors
], $statusCode, $headers);
// In the circumstance where an exception is raised
// before the lumen request cycle reaches the middleware stage,
// it is necessary to force the inclusion of the CORS headers.
// This uses the app's CORS config.
// This config must be loaded in the bootstrap process
// before exception handlers are expected to be used.
$options = config('cors');
if (! $response->headers->has('Access-Control-Allow-Origin')) {
// This CorsService relies on Asm89\Stack
$cors = new CorsService($options);
$response = $cors->addActualRequestHeaders($response, $request);
}
return $response;
}
return parent::render($request, $e);
}
}