Skip to content

Commit 909e429

Browse files
Symfony Exceptions to be returned in title and detail
1 parent a0ea866 commit 909e429

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

src/Handler.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
use Illuminate\Http\Response;
88
use Illuminate\Validation\ValidationException;
99
use KamranAhmed\Faulty\Exceptions\BaseException;
10+
use KamranAhmed\Faulty\Exceptions\NotFoundException;
1011
use Laravel\Lumen\Exceptions\Handler as LumenExceptionHandler;
1112
use Symfony\Component\HttpKernel\Exception\HttpException;
13+
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
1214
use Whoops\Handler\PrettyPageHandler;
1315
use Whoops\Run;
1416

@@ -75,7 +77,7 @@ protected function renderExceptionWithWhoops(Exception $e)
7577
* the array with status and render. If there is any other e.g. fatal or anything
7678
*
7779
* @param $request
78-
* @param Exception $e
80+
* @param Exception $e
7981
*
8082
* @return Response|\Symfony\Component\HttpFoundation\Response
8183
*/
@@ -86,10 +88,19 @@ public function handle($request, Exception $e)
8688
if ($e instanceOf BaseException) {
8789
$data = $e->toArray();
8890
} else if ($e instanceof HttpException) {
91+
92+
if ($e instanceof NotFoundException) {
93+
$detail = 'Resource not found';
94+
} else if ($e instanceof MethodNotAllowedHttpException) {
95+
$detail = 'Method not allowed';
96+
} else {
97+
$detail = $this->generateHttpExceptionMessage($e);
98+
}
99+
89100
$data = [
90101
'status' => $e->getStatusCode(),
91-
'title' => str_ireplace('HttpException', '', get_class($e)),
92-
'detail' => $e->getMessage(),
102+
'title' => $detail,
103+
'detail' => $detail,
93104
];
94105
} else if ($e instanceof ValidationException) {
95106
parent::render($request, $e);
@@ -126,4 +137,20 @@ protected function getExceptionDefaults(Exception $e)
126137
'type' => 'https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html',
127138
];
128139
}
140+
141+
/**
142+
* @param \Symfony\Component\HttpKernel\Exception\HttpException $e
143+
*
144+
* @return string
145+
*/
146+
private function generateHttpExceptionMessage(HttpException $e)
147+
{
148+
$class = get_class($e);
149+
$parts = explode('\\', $class);
150+
151+
$title = $parts[count($parts) - 1] ?? $class;
152+
$title = str_replace('HttpException', '', $title);
153+
154+
return $title;
155+
}
129156
}

0 commit comments

Comments
 (0)