1
0
mirror of https://github.com/flarum/core.git synced 2025-10-12 07:24:27 +02:00
Files
php-flarum/src/Api/ExceptionHandler.php
Toby Zerner 57f4dc6091 Have a go at some error handling
Still not happy with how this is all fitting together. But good enough
for now
2015-02-26 12:48:23 +10:30

76 lines
2.3 KiB
PHP

<?php namespace Flarum\Api;
use Exception;
use Illuminate\Foundation\Exceptions\Handler;
use Illuminate\Http\Response;
use Illuminate\Http\JsonResponse;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Flarum\Core\Exceptions\ValidationFailureException;
use Flarum\Core\Exceptions\PermissionDeniedException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Config;
class ExceptionHandler extends Handler
{
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
'Symfony\Component\HttpKernel\Exception\HttpException'
];
/**
* 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)
{
if ($request->is('api/*')) {
if ($e instanceof ValidationFailureException) {
return $this->renderValidationException($e);
}
if ($e instanceof PermissionDeniedException) {
return new Response(null, 401);
}
$error = [];
if (Config::get('app.debug')) {
$error['code'] = (new \ReflectionClass($e))->getShortName();
}
if ($detail = $e->getMessage()) {
$error['detail'] = $detail;
}
$statusCode = $e instanceof HttpException ? $e->getStatusCode() : 500;
if (count($error)) {
return $this->renderErrors([$error], $statusCode);
} else {
return new Response(null, $statusCode);
}
}
return parent::render($request, $e);
}
protected function renderErrors($errors, $httpCode = 500)
{
return new JsonResponse(['errors' => $errors], $httpCode);
}
protected function renderValidationException(ValidationFailureException $e)
{
$errors = [];
foreach ($e->getErrors()->getMessages() as $field => $messages) {
$errors[] = [
'detail' => implode("\n", $messages),
'path' => $field
];
}
return $this->renderErrors($errors, 422);
}
}