diff --git a/app/Exceptions/Displayers/JsonValidationDisplayer.php b/app/Exceptions/Displayers/JsonValidationDisplayer.php new file mode 100644 index 00000000..5c3ee86f --- /dev/null +++ b/app/Exceptions/Displayers/JsonValidationDisplayer.php @@ -0,0 +1,54 @@ +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, $code) + { + return $transformed instanceof ValidationException; + } +} diff --git a/app/Http/Controllers/Api/ComponentController.php b/app/Http/Controllers/Api/ComponentController.php index 47710feb..f01dc53b 100644 --- a/app/Http/Controllers/Api/ComponentController.php +++ b/app/Http/Controllers/Api/ComponentController.php @@ -16,9 +16,9 @@ use CachetHQ\Cachet\Commands\Component\RemoveComponentCommand; use CachetHQ\Cachet\Commands\Component\UpdateComponentCommand; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\Tag; -use Exception; use GrahamCampbell\Binput\Facades\Binput; use Illuminate\Contracts\Auth\Guard; +use Illuminate\Database\QueryException; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Http\Request; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; @@ -75,7 +75,7 @@ class ComponentController extends AbstractApiController Binput::get('group_id'), (bool) Binput::get('enabled', true) )); - } catch (Exception $e) { + } catch (QueryException $e) { throw new BadRequestHttpException(); } @@ -116,7 +116,7 @@ class ComponentController extends AbstractApiController Binput::get('group_id'), (bool) Binput::get('enabled', true) )); - } catch (Exception $e) { + } catch (QueryException $e) { throw new BadRequestHttpException(); } diff --git a/app/Http/Controllers/Api/ComponentGroupController.php b/app/Http/Controllers/Api/ComponentGroupController.php index 1d0a1702..2c378e73 100644 --- a/app/Http/Controllers/Api/ComponentGroupController.php +++ b/app/Http/Controllers/Api/ComponentGroupController.php @@ -15,8 +15,8 @@ use CachetHQ\Cachet\Commands\ComponentGroup\AddComponentGroupCommand; use CachetHQ\Cachet\Commands\ComponentGroup\RemoveComponentGroupCommand; use CachetHQ\Cachet\Commands\ComponentGroup\UpdateComponentGroupCommand; use CachetHQ\Cachet\Models\ComponentGroup; -use Exception; use GrahamCampbell\Binput\Facades\Binput; +use Illuminate\Database\QueryException; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Http\Request; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; @@ -63,7 +63,7 @@ class ComponentGroupController extends AbstractApiController Binput::get('name'), Binput::get('order', 0) )); - } catch (Exception $e) { + } catch (QueryException $e) { throw new BadRequestHttpException(); } @@ -85,7 +85,7 @@ class ComponentGroupController extends AbstractApiController Binput::get('name'), Binput::get('order', 0) )); - } catch (Exception $e) { + } catch (QueryException $e) { throw new BadRequestHttpException(); } diff --git a/app/Http/Controllers/Api/IncidentController.php b/app/Http/Controllers/Api/IncidentController.php index c0d482a2..f373a9c6 100644 --- a/app/Http/Controllers/Api/IncidentController.php +++ b/app/Http/Controllers/Api/IncidentController.php @@ -15,9 +15,9 @@ use CachetHQ\Cachet\Commands\Incident\RemoveIncidentCommand; use CachetHQ\Cachet\Commands\Incident\ReportIncidentCommand; use CachetHQ\Cachet\Commands\Incident\UpdateIncidentCommand; use CachetHQ\Cachet\Models\Incident; -use Exception; use GrahamCampbell\Binput\Facades\Binput; use Illuminate\Contracts\Auth\Guard; +use Illuminate\Database\QueryException; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Http\Request; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; @@ -77,7 +77,7 @@ class IncidentController extends AbstractApiController Binput::get('template'), Binput::get('vars') )); - } catch (Exception $e) { + } catch (QueryException $e) { throw new BadRequestHttpException(); } @@ -107,7 +107,7 @@ class IncidentController extends AbstractApiController Binput::get('template'), Binput::get('vars') )); - } catch (Exception $e) { + } catch (QueryException $e) { throw new BadRequestHttpException(); } diff --git a/app/Http/Controllers/Api/MetricController.php b/app/Http/Controllers/Api/MetricController.php index 15b1fb45..88b199c6 100644 --- a/app/Http/Controllers/Api/MetricController.php +++ b/app/Http/Controllers/Api/MetricController.php @@ -15,8 +15,8 @@ use CachetHQ\Cachet\Commands\Metric\AddMetricCommand; use CachetHQ\Cachet\Commands\Metric\RemoveMetricCommand; use CachetHQ\Cachet\Commands\Metric\UpdateMetricCommand; use CachetHQ\Cachet\Models\Metric; -use Exception; use GrahamCampbell\Binput\Facades\Binput; +use Illuminate\Database\QueryException; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Http\Request; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; @@ -80,7 +80,7 @@ class MetricController extends AbstractApiController Binput::get('display_chart'), Binput::get('places', 2) )); - } catch (Exception $e) { + } catch (QueryException $e) { throw new BadRequestHttpException(); } @@ -107,7 +107,7 @@ class MetricController extends AbstractApiController Binput::get('display_chart'), Binput::get('places', 2) )); - } catch (Exception $e) { + } catch (QueryException $e) { throw new BadRequestHttpException(); } diff --git a/app/Http/Controllers/Api/MetricPointController.php b/app/Http/Controllers/Api/MetricPointController.php index 1c615ab4..9dc59a58 100644 --- a/app/Http/Controllers/Api/MetricPointController.php +++ b/app/Http/Controllers/Api/MetricPointController.php @@ -16,8 +16,8 @@ use CachetHQ\Cachet\Commands\Metric\RemoveMetricPointCommand; use CachetHQ\Cachet\Commands\Metric\UpdateMetricPointCommand; use CachetHQ\Cachet\Models\Metric; use CachetHQ\Cachet\Models\MetricPoint; -use Exception; use GrahamCampbell\Binput\Facades\Binput; +use Illuminate\Database\QueryException; use Illuminate\Foundation\Bus\DispatchesJobs; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; @@ -53,7 +53,7 @@ class MetricPointController extends AbstractApiController Binput::get('value'), Binput::get('timestamp')) ); - } catch (Exception $e) { + } catch (QueryException $e) { throw new BadRequestHttpException(); } diff --git a/app/Http/Controllers/Api/SubscriberController.php b/app/Http/Controllers/Api/SubscriberController.php index 0f5d75ab..1adbd23e 100644 --- a/app/Http/Controllers/Api/SubscriberController.php +++ b/app/Http/Controllers/Api/SubscriberController.php @@ -14,8 +14,8 @@ namespace CachetHQ\Cachet\Http\Controllers\Api; use CachetHQ\Cachet\Commands\Subscriber\SubscribeSubscriberCommand; use CachetHQ\Cachet\Commands\Subscriber\UnsubscribeSubscriberCommand; use CachetHQ\Cachet\Models\Subscriber; -use Exception; use GrahamCampbell\Binput\Facades\Binput; +use Illuminate\Database\QueryException; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Http\Request; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; @@ -47,7 +47,7 @@ class SubscriberController extends AbstractApiController { try { $subscriber = $this->dispatch(new SubscribeSubscriberCommand(Binput::get('email'), Binput::get('verify', false))); - } catch (Exception $e) { + } catch (QueryException $e) { throw new BadRequestHttpException(); } diff --git a/config/exceptions.php b/config/exceptions.php index ecc20d59..06dcaffe 100644 --- a/config/exceptions.php +++ b/config/exceptions.php @@ -45,6 +45,7 @@ return [ */ 'displayers' => [ + 'CachetHQ\Cachet\Exceptions\Displayers\JsonValidationDisplayer', 'CachetHQ\Cachet\Exceptions\Displayers\RedirectDisplayer', 'GrahamCampbell\Exceptions\Displayers\DebugDisplayer', 'GrahamCampbell\Exceptions\Displayers\HtmlDisplayer',