This repository has been archived by the owner on Sep 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathHandler.php
159 lines (136 loc) · 4.68 KB
/
Handler.php
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Http\Response;
use Illuminate\Database\QueryException;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Validation\ValidationException;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
\Illuminate\Auth\AuthenticationException::class,
\Illuminate\Auth\Access\AuthorizationException::class,
\Symfony\Component\HttpKernel\Exception\HttpException::class,
\Illuminate\Database\Eloquent\ModelNotFoundException::class,
\Illuminate\Session\TokenMismatchException::class,
\Illuminate\Validation\ValidationException::class,
];
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $exception
* @return void
*/
public function report(Exception $exception)
{
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
/*
* We add a custom exception renderer here since this will be an api only backend.
* So we need to convert every exception to a json response.
*/
if ($request->ajax() || $request->wantsJson()) {
return $this->getJsonResponse($exception);
}
return parent::render($request, $exception);
}
/**
* Get the json response for the exception.
*
* @param Exception $exception
* @return \Illuminate\Http\JsonResponse
*/
protected function getJsonResponse(Exception $exception)
{
$debugEnabled = config('app.debug');
$exception = $this->prepareException($exception);
/*
* Handle validation errors thrown using ValidationException.
*/
if ($exception instanceof ValidationException) {
$validationErrors = $exception->validator->errors()->getMessages();
/*
* Laravel validation error format example
* "attribute" => [
* "The attribute failed validation."
* ]
*
* What we need as per the api spec
* "attribute" => [
* "failed validation."
* ]
*/
$validationErrors = array_map(function($error) {
return array_map(function($message) {
return remove_words($message, 2);
}, $error);
}, $validationErrors);
return response()->json(['errors' => $validationErrors], 422);
}
/*
* Handle database errors thrown using QueryException.
* Prevent sensitive information from leaking in the error message.
*/
if ($exception instanceof QueryException) {
if ($debugEnabled) {
$message = $exception->getMessage();
} else {
$message = 'Internal Server Error';
}
}
$statusCode = $this->getStatusCode($exception);
if (! isset($message) && ! ($message = $exception->getMessage())) {
$message = sprintf('%d %s', $statusCode, Response::$statusTexts[$statusCode]);
}
$errors = [
'message' => $message,
'status_code' => $statusCode,
];
if ($debugEnabled) {
$errors['exception'] = get_class($exception);
$errors['trace'] = explode("\n", $exception->getTraceAsString());
}
return response()->json(['errors' => $errors], $statusCode);
}
/**
* Get the status code from the exception.
*
* @param \Exception $exception
* @return int
*/
protected function getStatusCode(Exception $exception)
{
return $this->isHttpException($exception) ? $exception->getStatusCode() : 500;
}
/**
* Convert an authentication exception into an unauthenticated response.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Auth\AuthenticationException $exception
* @return \Illuminate\Http\Response
*/
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
return redirect()->guest(route('login'));
}
}