Merge pull request #735 from cachethq/api-exceptions
Make sure only "application/*" error pages are returned from the api
This commit is contained in:
58
app/Exceptions/Filters/ApiFilter.php
Normal file
58
app/Exceptions/Filters/ApiFilter.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Cachet.
|
||||
*
|
||||
* (c) Cachet HQ <support@cachethq.io>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CachetHQ\Cachet\Exceptions\Filters;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ApiFilter
|
||||
{
|
||||
/**
|
||||
* The request instance.
|
||||
*
|
||||
* @var \Illuminate\Http\Request
|
||||
*/
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Create a new content type filter instance.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Request $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter and return the displayers.
|
||||
*
|
||||
* @param \GrahamCampbell\Exceptions\Displayers\DisplayerInterface[] $displayers
|
||||
* @param \Exception $exception
|
||||
*
|
||||
* @return \GrahamCampbell\Exceptions\Displayers\DisplayerInterface[]
|
||||
*/
|
||||
public function filter(array $displayers, Exception $exception)
|
||||
{
|
||||
if ($this->request->is('api*')) {
|
||||
foreach ($displayers as $index => $displayer) {
|
||||
if (!str_contains($displayer->contentType(), 'application/')) {
|
||||
unset($displayers[$index]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return array_values($displayers);
|
||||
}
|
||||
}
|
||||
@@ -48,6 +48,7 @@ return [
|
||||
'GrahamCampbell\Exceptions\Filters\VerboseFilter',
|
||||
'GrahamCampbell\Exceptions\Filters\CanDisplayFilter',
|
||||
'GrahamCampbell\Exceptions\Filters\ContentTypeFilter',
|
||||
'CachetHQ\Cachet\Exceptions\Filters\ApiFilter',
|
||||
],
|
||||
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user