From 708a5aeb09f055ff25aec599bfa54e2e4997cf94 Mon Sep 17 00:00:00 2001 From: Dennis Mwea <mweadennis2@gmail.com> Date: Sun, 4 Oct 2020 00:10:39 +0300 Subject: [PATCH] Check whether file exists and give a more descriptive message --- src/Exceptions/FileNotFoundException.php | 13 +++++++++++++ src/Http/Controller.php | 5 +++++ 2 files changed, 18 insertions(+) create mode 100644 src/Exceptions/FileNotFoundException.php diff --git a/src/Exceptions/FileNotFoundException.php b/src/Exceptions/FileNotFoundException.php new file mode 100644 index 00000000..3cbc7c1e --- /dev/null +++ b/src/Exceptions/FileNotFoundException.php @@ -0,0 +1,13 @@ +<?php + +namespace Mpociot\ApiDoc\Exceptions; + +use Symfony\Component\HttpKernel\Exception\HttpException; + +class FileNotFoundException extends HttpException +{ + public function __construct(int $statusCode, string $message = null, Throwable $previous = null, array $headers = [], ?int $code = 0) + { + parent::__construct($statusCode, $message); + } +} diff --git a/src/Http/Controller.php b/src/Http/Controller.php index b4c9a28c..8b456c2a 100644 --- a/src/Http/Controller.php +++ b/src/Http/Controller.php @@ -3,6 +3,7 @@ namespace Mpociot\ApiDoc\Http; use Illuminate\Support\Facades\Storage; +use Mpociot\ApiDoc\Exceptions\FileNotFoundException; class Controller { @@ -18,6 +19,10 @@ public function html() */ public function json() { + if (!Storage::disk(config('apidoc.storage'))->has('apidoc/collection.json')) { + throw new FileNotFoundException(400, 'Please run php artisan apidoc:generate.'); + } + return response()->json( json_decode(Storage::disk(config('apidoc.storage'))->get('apidoc/collection.json')) );