mirror of
https://github.com/flarum/core.git
synced 2025-10-12 23:44:27 +02:00
Have a go at some error handling
Still not happy with how this is all fitting together. But good enough for now
This commit is contained in:
75
src/Api/ExceptionHandler.php
Normal file
75
src/Api/ExceptionHandler.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user