1
0
mirror of https://github.com/flarum/core.git synced 2025-10-13 07:54:25 +02:00

Use exception handlers instead of JsonApiSerializableInterface

This commit is contained in:
Toby Zerner
2015-10-26 11:12:10 +10:30
parent f3612261ec
commit 68498cedae
14 changed files with 326 additions and 141 deletions

45
src/Api/ErrorHandler.php Normal file
View File

@@ -0,0 +1,45 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Api;
use Exception;
use Tobscure\JsonApi\Document;
use Tobscure\JsonApi\ErrorHandler as JsonApiErrorHandler;
class ErrorHandler
{
/**
* @var JsonApiErrorHandler
*/
protected $errorHandler;
/**
* @param JsonApiErrorHandler $errorHandler
*/
public function __construct(JsonApiErrorHandler $errorHandler)
{
$this->errorHandler = $errorHandler;
}
/**
* @param Exception $e
* @return JsonApiResponse
*/
public function handle(Exception $e)
{
$response = $this->errorHandler->handle($e);
$document = new Document;
$document->setErrors($response->getErrors());
return new JsonApiResponse($document, $response->getStatus());
}
}