Skip to content

Commit 3d909d9

Browse files
authored
Merge pull request #164 from RonasIT/163-modify-500-code-error-response-page
Modify 500 code error response page
2 parents d0c5e78 + 822934d commit 3d909d9

File tree

55 files changed

+165
-167
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+165
-167
lines changed

composer.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/auto-doc.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@
101101
'204' => 'Operation successfully done',
102102
'404' => 'This entity not found',
103103
],
104+
105+
/*
106+
|--------------------------------------------------------------------------
107+
| Error Template
108+
|--------------------------------------------------------------------------
109+
|
110+
| You can use your custom description view for errors.
111+
*/
112+
'error' => 'auto-doc::error',
104113
],
105114

106115
/*
@@ -211,5 +220,5 @@
211220
],
212221
],
213222

214-
'config_version' => '2.9',
223+
'config_version' => '2.10',
215224
];

resources/views/error.blade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ $message }}

src/AutoDocServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function boot()
1717

1818
$this->publishes([
1919
__DIR__ . '/../resources/views/swagger-description.blade.php' => resource_path('views/vendor/auto-doc/swagger-description.blade.php'),
20+
__DIR__ . '/../resources/views/error.blade.php' => resource_path('views/vendor/auto-doc/error.blade.php'),
2021
], 'view');
2122

2223
if (!$this->app->routesAreCached()) {

src/Drivers/LocalDriver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace RonasIT\AutoDoc\Drivers;
44

5-
use Illuminate\Contracts\Filesystem\FileNotFoundException;
5+
use RonasIT\AutoDoc\Exceptions\FileNotFoundException;
66
use RonasIT\AutoDoc\Exceptions\MissedProductionFilePathException;
77

88
class LocalDriver extends BaseDriver
@@ -30,7 +30,7 @@ public function saveData(): void
3030
public function getDocumentation(): array
3131
{
3232
if (!file_exists($this->prodFilePath)) {
33-
throw new FileNotFoundException();
33+
throw new FileNotFoundException($this->prodFilePath);
3434
}
3535

3636
$fileContent = file_get_contents($this->prodFilePath);

src/Drivers/RemoteDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace RonasIT\AutoDoc\Drivers;
44

5-
use Illuminate\Contracts\Filesystem\FileNotFoundException;
5+
use RonasIT\AutoDoc\Exceptions\FileNotFoundException;
66
use RonasIT\AutoDoc\Exceptions\MissedRemoteDocumentationUrlException;
77

88
class RemoteDriver extends BaseDriver

src/Drivers/StorageDriver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace RonasIT\AutoDoc\Drivers;
44

5-
use Illuminate\Contracts\Filesystem\FileNotFoundException;
5+
use RonasIT\AutoDoc\Exceptions\FileNotFoundException;
66
use Illuminate\Contracts\Filesystem\Filesystem;
77
use Illuminate\Support\Facades\Storage;
88
use RonasIT\AutoDoc\Exceptions\MissedProductionFilePathException;
@@ -34,7 +34,7 @@ public function saveData(): void
3434
public function getDocumentation(): array
3535
{
3636
if (!$this->disk->exists($this->prodFilePath)) {
37-
throw new FileNotFoundException();
37+
throw new FileNotFoundException($this->prodFilePath);
3838
}
3939

4040
$fileContent = $this->disk->get($this->prodFilePath);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace RonasIT\AutoDoc\Exceptions;
4+
5+
use Illuminate\Contracts\Filesystem\FileNotFoundException as BaseException;
6+
7+
class FileNotFoundException extends BaseException
8+
{
9+
public function __construct(string $filePath = null)
10+
{
11+
parent::__construct("Documentation file not found {$filePath}");
12+
}
13+
}

src/Services/SwaggerService.php

100755100644
Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use RonasIT\AutoDoc\Traits\GetDependenciesTrait;
2424
use RonasIT\AutoDoc\Validators\SwaggerSpecValidator;
2525
use Symfony\Component\HttpFoundation\Response;
26+
use Exception;
2627

2728
/**
2829
* @property SwaggerDriverContract $driver
@@ -131,14 +132,18 @@ protected function setDriver()
131132
}
132133
}
133134

134-
protected function generateEmptyData(): array
135+
protected function generateEmptyData(?string $view = null, array $viewData = [], array $license = []): array
135136
{
136137
// client must enter at least `contact.email` to generate a default `info` block
137138
// otherwise an exception will be called
138139
if (!empty($this->config['info']) && !Arr::get($this->config, 'info.contact.email')) {
139140
throw new EmptyContactEmailException();
140141
}
141142

143+
if (empty($view) && !empty($this->config['info'])) {
144+
$view = $this->config['info']['description'];
145+
}
146+
142147
$data = [
143148
'openapi' => self::OPEN_API_VERSION,
144149
'servers' => [
@@ -148,7 +153,7 @@ protected function generateEmptyData(): array
148153
'components' => [
149154
'schemas' => $this->config['definitions'],
150155
],
151-
'info' => $this->prepareInfo($this->config['info'])
156+
'info' => $this->prepareInfo($view, $viewData, $license),
152157
];
153158

154159
$securityDefinitions = $this->generateSecurityDefinition();
@@ -260,7 +265,7 @@ protected function generatePathDescription(string $key): string
260265

261266
foreach ($exploded as $value) {
262267
if (!preg_match('/^[a-zA-Z0-9\.]+$/', $value)) {
263-
return "regexp: {$expression}";
268+
return "regexp: {$expression}";
264269
}
265270
}
266271

@@ -596,8 +601,13 @@ protected function saveParameterType(&$data, $parameter, $parameterType)
596601
];
597602
}
598603

599-
protected function saveParameterDescription(&$data, $parameter, array $rulesArray, array $attributes, array $annotations)
600-
{
604+
protected function saveParameterDescription(
605+
array &$data,
606+
string $parameter,
607+
array $rulesArray,
608+
array $attributes,
609+
array $annotations
610+
) {
601611
$description = Arr::get($annotations, $parameter);
602612

603613
if (empty($description)) {
@@ -803,7 +813,7 @@ public function saveProductionData()
803813
if (ParallelTesting::token()) {
804814
$this->driver->appendProcessDataToTmpFile(function (array $sharedTmpData) {
805815
$resultDocContent = (empty($sharedTmpData))
806-
? $this->generateEmptyData()
816+
? $this->generateEmptyData($this->config['info']['description'])
807817
: $sharedTmpData;
808818

809819
$this->mergeOpenAPIDocs($resultDocContent, $this->data);
@@ -817,9 +827,13 @@ public function saveProductionData()
817827

818828
public function getDocFileContent()
819829
{
820-
$documentation = $this->driver->getDocumentation();
830+
try {
831+
$documentation = $this->driver->getDocumentation();
821832

822-
$this->openAPIValidator->validate($documentation);
833+
$this->openAPIValidator->validate($documentation);
834+
} catch (Exception $exception) {
835+
return $this->generateEmptyData($this->config['defaults']['error'], ['message' => $exception->getMessage()]);
836+
}
823837

824838
$additionalDocs = config('auto-doc.additional_paths', []);
825839

@@ -946,27 +960,21 @@ protected function getDefaultValueByType($type)
946960
return $values[$type];
947961
}
948962

949-
protected function prepareInfo(array $info): array
963+
protected function prepareInfo(?string $view = null, array $viewData = [], array $license = []): array
950964
{
951-
if (empty($info)) {
952-
return $info;
953-
}
965+
$info = [];
954966

955-
foreach ($info['license'] as $key => $value) {
956-
if (empty($value)) {
957-
unset($info['license'][$key]);
958-
}
959-
}
967+
$license = array_filter($license);
960968

961-
if (empty($info['license'])) {
962-
unset($info['license']);
969+
if (!empty($license)) {
970+
$info['license'] = $license;
963971
}
964972

965-
if (!empty($info['description'])) {
966-
$info['description'] = view($info['description'])->render();
973+
if (!empty($view)) {
974+
$info['description'] = view($view, $viewData)->render();
967975
}
968-
969-
return $info;
976+
977+
return array_merge($this->config['info'], $info);
970978
}
971979

972980
protected function getOpenAPIFileContent(string $filePath): array

tests/AutoDocControllerTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44

55
use Illuminate\Http\Response;
66
use phpmock\phpunit\PHPMock;
7-
use RonasIT\Support\Traits\MockTrait;
87

98
class AutoDocControllerTest extends TestCase
109
{
11-
use MockTrait;
1210
use PHPMock;
1311

1412
protected static array $documentation;

0 commit comments

Comments
 (0)