diff --git a/app/Exceptions/Filters/ApiFilter.php b/app/Exceptions/Filters/ApiFilter.php new file mode 100644 index 00000000..cd324238 --- /dev/null +++ b/app/Exceptions/Filters/ApiFilter.php @@ -0,0 +1,58 @@ + + * + * 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); + } +} diff --git a/config/exceptions.php b/config/exceptions.php index 602b1a34..be2192d2 100644 --- a/config/exceptions.php +++ b/config/exceptions.php @@ -48,6 +48,7 @@ return [ 'GrahamCampbell\Exceptions\Filters\VerboseFilter', 'GrahamCampbell\Exceptions\Filters\CanDisplayFilter', 'GrahamCampbell\Exceptions\Filters\ContentTypeFilter', + 'CachetHQ\Cachet\Exceptions\Filters\ApiFilter', ], ];