wip
This commit is contained in:
54
app/Exceptions/Displayers/JsonValidationDisplayer.php
Normal file
54
app/Exceptions/Displayers/JsonValidationDisplayer.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Cachet.
|
||||
*
|
||||
* (c) Alt Three Services Limited
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CachetHQ\Cachet\Exceptions\Displayers;
|
||||
|
||||
use AltThree\Validator\ValidationException;
|
||||
use Exception;
|
||||
use GrahamCampbell\Exceptions\Displayers\DisplayerInterface;
|
||||
use GrahamCampbell\Exceptions\Displayers\JsonDisplayer;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
|
||||
class JsonValidationDisplayer extends JsonDisplayer implements DisplayerInterface
|
||||
{
|
||||
/**
|
||||
* Get the error response associated with the given exception.
|
||||
*
|
||||
* @param \Exception $exception
|
||||
* @param string $id
|
||||
* @param int $code
|
||||
* @param string[] $headers
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
public function display(Exception $exception, string $id, int $code, array $headers)
|
||||
{
|
||||
$info = $this->info->generate($exception, $id, 400);
|
||||
|
||||
$error = ['id' => $id, 'status' => $info['code'], 'title' => $info['name'], 'detail' => $info['detail'], 'meta' => ['details' => $exception->getMessageBag()->all()]];
|
||||
|
||||
return new JsonResponse(['errors' => [$error]], 400, array_merge($headers, ['Content-Type' => $this->contentType()]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Can we display the exception?
|
||||
*
|
||||
* @param \Exception $original
|
||||
* @param \Exception $transformed
|
||||
* @param int $code
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function canDisplay(Exception $original, Exception $transformed, int $code)
|
||||
{
|
||||
return $transformed instanceof ValidationException;
|
||||
}
|
||||
}
|
||||
104
app/Exceptions/Displayers/MaintenanceDisplayer.php
Normal file
104
app/Exceptions/Displayers/MaintenanceDisplayer.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Cachet.
|
||||
*
|
||||
* (c) Alt Three Services Limited
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CachetHQ\Cachet\Exceptions\Displayers;
|
||||
|
||||
use Exception;
|
||||
use GrahamCampbell\Exceptions\Displayers\DisplayerInterface;
|
||||
use Illuminate\Contracts\View\Factory;
|
||||
use Illuminate\Foundation\Http\Exceptions\MaintenanceModeException;
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
/**
|
||||
* This is the maintenance displayer class.
|
||||
*
|
||||
* @author James Brooks <james@alt-three.com>
|
||||
*/
|
||||
class MaintenanceDisplayer implements DisplayerInterface
|
||||
{
|
||||
/**
|
||||
* The view factory instance.
|
||||
*
|
||||
* @var \Illuminate\Contracts\View\Factory
|
||||
*/
|
||||
protected $view;
|
||||
|
||||
/**
|
||||
* Create a new maintenance displayer instance.
|
||||
*
|
||||
* @param \Illuminate\Contracts\View\Factory $view
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Factory $view)
|
||||
{
|
||||
$this->view = $view;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the error response associated with the given exception.
|
||||
*
|
||||
* @param \Exception $exception
|
||||
* @param string $id
|
||||
* @param int $code
|
||||
* @param string[] $headers
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
public function display(Exception $exception, string $id, int $code, array $headers)
|
||||
{
|
||||
return new Response($this->render(), $code, array_merge($headers, ['Content-Type' => $this->contentType()]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the page.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function render()
|
||||
{
|
||||
return $this->view->make('errors.maintenance')->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the supported content type.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function contentType()
|
||||
{
|
||||
return 'text/html';
|
||||
}
|
||||
|
||||
/**
|
||||
* Can we display the exception?
|
||||
*
|
||||
* @param \Exception $original
|
||||
* @param \Exception $transformed
|
||||
* @param int $code
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function canDisplay(Exception $original, Exception $transformed, int $code)
|
||||
{
|
||||
return $transformed instanceof MaintenanceModeException;
|
||||
}
|
||||
|
||||
/**
|
||||
* Do we provide verbose information about the exception?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isVerbose()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
90
app/Exceptions/Displayers/RedirectDisplayer.php
Normal file
90
app/Exceptions/Displayers/RedirectDisplayer.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Cachet.
|
||||
*
|
||||
* (c) Alt Three Services Limited
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CachetHQ\Cachet\Exceptions\Displayers;
|
||||
|
||||
use Exception;
|
||||
use GrahamCampbell\Exceptions\Displayers\DisplayerInterface;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
|
||||
|
||||
class RedirectDisplayer implements DisplayerInterface
|
||||
{
|
||||
/**
|
||||
* The request instance.
|
||||
*
|
||||
* @var \Illuminate\Http\Request
|
||||
*/
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Create a new redirect displayer instance.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Request $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the error response associated with the given exception.
|
||||
*
|
||||
* @param \Exception $exception
|
||||
* @param string $id
|
||||
* @param int $code
|
||||
* @param string[] $headers
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
public function display(Exception $exception, string $id, int $code, array $headers)
|
||||
{
|
||||
return redirect()->guest('auth/login');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the supported content type.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function contentType()
|
||||
{
|
||||
return 'text/html';
|
||||
}
|
||||
|
||||
/**
|
||||
* Can we display the exception?
|
||||
*
|
||||
* @param \Exception $original
|
||||
* @param \Exception $transformed
|
||||
* @param int $code
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function canDisplay(Exception $original, Exception $transformed, int $code)
|
||||
{
|
||||
$redirect = $transformed instanceof HttpExceptionInterface && $transformed->getStatusCode() === 401;
|
||||
|
||||
return $redirect && !$this->request->is('api*');
|
||||
}
|
||||
|
||||
/**
|
||||
* Do we provide verbose information about the exception?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isVerbose()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
88
app/Exceptions/Displayers/SettingsDisplayer.php
Normal file
88
app/Exceptions/Displayers/SettingsDisplayer.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Cachet.
|
||||
*
|
||||
* (c) Alt Three Services Limited
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CachetHQ\Cachet\Exceptions\Displayers;
|
||||
|
||||
use CachetHQ\Cachet\Settings\ReadException;
|
||||
use Exception;
|
||||
use GrahamCampbell\Exceptions\Displayers\DisplayerInterface;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class SettingsDisplayer implements DisplayerInterface
|
||||
{
|
||||
/**
|
||||
* The request instance.
|
||||
*
|
||||
* @var \Illuminate\Http\Request
|
||||
*/
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Create a new redirect displayer instance.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Request $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the error response associated with the given exception.
|
||||
*
|
||||
* @param \Exception $exception
|
||||
* @param string $id
|
||||
* @param int $code
|
||||
* @param string[] $headers
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
public function display(Exception $exception, string $id, int $code, array $headers)
|
||||
{
|
||||
return cachet_redirect('setup');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the supported content type.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function contentType()
|
||||
{
|
||||
return 'text/html';
|
||||
}
|
||||
|
||||
/**
|
||||
* Can we display the exception?
|
||||
*
|
||||
* @param \Exception $original
|
||||
* @param \Exception $transformed
|
||||
* @param int $code
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function canDisplay(Exception $original, Exception $transformed, int $code)
|
||||
{
|
||||
return ($transformed instanceof ReadException) && !$this->request->is('setup*');
|
||||
}
|
||||
|
||||
/**
|
||||
* Do we provide verbose information about the exception?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isVerbose()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
88
app/Exceptions/Displayers/ThrottleDisplayer.php
Normal file
88
app/Exceptions/Displayers/ThrottleDisplayer.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Cachet.
|
||||
*
|
||||
* (c) Alt Three Services Limited
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CachetHQ\Cachet\Exceptions\Displayers;
|
||||
|
||||
use Exception;
|
||||
use GrahamCampbell\Exceptions\Displayers\DisplayerInterface;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
|
||||
|
||||
class ThrottleDisplayer implements DisplayerInterface
|
||||
{
|
||||
/**
|
||||
* The request instance.
|
||||
*
|
||||
* @var \Illuminate\Http\Request
|
||||
*/
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Create a new redirect displayer instance.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Request $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the error response associated with the given exception.
|
||||
*
|
||||
* @param \Exception $exception
|
||||
* @param string $id
|
||||
* @param int $code
|
||||
* @param string[] $headers
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
public function display(Exception $exception, string $id, int $code, array $headers)
|
||||
{
|
||||
return cachet_redirect('auth.login')->withError(trans('forms.login.rate-limit'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the supported content type.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function contentType()
|
||||
{
|
||||
return 'text/html';
|
||||
}
|
||||
|
||||
/**
|
||||
* Can we display the exception?
|
||||
*
|
||||
* @param \Exception $original
|
||||
* @param \Exception $transformed
|
||||
* @param int $code
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function canDisplay(Exception $original, Exception $transformed, int $code)
|
||||
{
|
||||
return $transformed instanceof TooManyRequestsHttpException && $this->request->is('auth*');
|
||||
}
|
||||
|
||||
/**
|
||||
* Do we provide verbose information about the exception?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isVerbose()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
43
app/Exceptions/Filters/ApiFilter.php
Normal file
43
app/Exceptions/Filters/ApiFilter.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Cachet.
|
||||
*
|
||||
* (c) Alt Three Services Limited
|
||||
*
|
||||
* 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;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class ApiFilter
|
||||
{
|
||||
/**
|
||||
* Filter and return the displayers.
|
||||
*
|
||||
* @param \GrahamCampbell\Exceptions\Displayers\DisplayerInterface[] $displayers
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Exception $original
|
||||
* @param \Exception $transformed
|
||||
* @param int $code
|
||||
*
|
||||
* @return \GrahamCampbell\Exceptions\Displayers\DisplayerInterface[]
|
||||
*/
|
||||
public function filter(array $displayers, Request $request, Exception $original, Exception $transformed, int $code)
|
||||
{
|
||||
if ($request->is('api*')) {
|
||||
foreach ($displayers as $index => $displayer) {
|
||||
if (!Str::contains($displayer->contentType(), 'application/')) {
|
||||
unset($displayers[$index]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return array_values($displayers);
|
||||
}
|
||||
}
|
||||
41
app/Exceptions/Transformers/BusTransformer.php
Normal file
41
app/Exceptions/Transformers/BusTransformer.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Cachet.
|
||||
*
|
||||
* (c) Alt Three Services Limited
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CachetHQ\Cachet\Exceptions\Transformers;
|
||||
|
||||
use CachetHQ\Cachet\Bus\Exceptions\ExceptionInterface;
|
||||
use Exception;
|
||||
use GrahamCampbell\Exceptions\Transformers\TransformerInterface;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
|
||||
/**
|
||||
* This is the bus transformer class.
|
||||
*
|
||||
* @author Graham Campbell <graham@alt-three.com>
|
||||
*/
|
||||
class BusTransformer implements TransformerInterface
|
||||
{
|
||||
/**
|
||||
* Transform the provided exception.
|
||||
*
|
||||
* @param \Exception $exception
|
||||
*
|
||||
* @return \Exception
|
||||
*/
|
||||
public function transform(Exception $exception)
|
||||
{
|
||||
if ($exception instanceof ExceptionInterface) {
|
||||
$exception = new BadRequestHttpException($exception->getMessage());
|
||||
}
|
||||
|
||||
return $exception;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user