Skip to content

Commit 95322dd

Browse files
committed
Improve the Exceptions Handling
1 parent 8b4428a commit 95322dd

1 file changed

Lines changed: 28 additions & 18 deletions

File tree

app/Platform/Exceptions/Handler.php

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,30 @@ protected function renderHttpException(HttpException $e, Request $request)
9898

9999
// If exists a View for this HTTP error.
100100
else if (View::exists("Errors/{$status}")) {
101-
$view = View::make('Layouts/Default')
102-
->shares('title', "Error {$status}")
103-
->nest('content', "Errors/{$status}", array('exception' => $e));
104-
105-
return Response::make($view->render(), $status, $e->getHeaders());
101+
return $this->createErrorResponse($status, $e);
106102
}
107103

108104
return parent::renderHttpException($e, $request);
109105
}
110106

107+
/**
108+
* Convert the given exception into a Response instance which contains an error page.
109+
*
110+
* @param int $status
111+
* @param \Exception $exception
112+
* @return \Symfony\Component\HttpFoundation\Response
113+
*/
114+
protected function createErrorResponse($status, Exception $e)
115+
{
116+
$exception = FlattenException::create($e, $status);
117+
118+
$view = View::make('Layouts/Default')
119+
->shares('title', "Error {$status}")
120+
->nest('content', "Errors/{$status}", compact('exception'));
121+
122+
return Response::make($view->render(), $status, $exception->getHeaders());
123+
}
124+
111125
/**
112126
* Convert the given exception into a Response instance.
113127
*
@@ -116,19 +130,12 @@ protected function renderHttpException(HttpException $e, Request $request)
116130
*/
117131
protected function convertExceptionToResponse(Exception $e, Request $request)
118132
{
119-
$debug = Config::get('app.debug');
120-
121-
if (! $debug) {
122-
$e = FlattenException::create($e);
123-
133+
if (! Config::get('app.debug', false)) {
124134
if ($request->ajax() || $request->wantsJson()) {
125135
return Response::json('Internal Server Error', 500);
126136
}
127137

128-
// Not an AJAX request.
129-
else if (View::exists('Errors/500')) {
130-
return $this->renderHttpException($e, $request);
131-
}
138+
return $this->createErrorResponse(500, new Exception('Internal Server Error'));
132139
}
133140

134141
// We will instruct Whoops to not exit after it displays the exception as it
@@ -146,10 +153,13 @@ protected function convertExceptionToResponse(Exception $e, Request $request)
146153

147154
$whoops->pushHandler($handler);
148155

149-
// Compute the status code and headers.
150-
$status = ($e instanceof HttpExceptionInterface) ? $e->getStatusCode() : 500;
151-
152-
$headers = ($e instanceof HttpExceptionInterface) ? $e->getHeaders() : array();
156+
if ($e instanceof HttpExceptionInterface) {
157+
$status = $e->getStatusCode();
158+
$headers = $e->getHeaders();
159+
} else {
160+
$status = 500;
161+
$headers = array();
162+
}
153163

154164
return Response::make($whoops->handleException($e), $status, $headers);
155165
}

0 commit comments

Comments
 (0)