Skip to content

Modify 500 code error response page #164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion config/auto-doc.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@
'204' => 'Operation successfully done',
'404' => 'This entity not found',
],

/*
|--------------------------------------------------------------------------
| Error Template
|--------------------------------------------------------------------------
|
| You can use your custom description view for errors.
*/
'error' => 'auto-doc::error',
],

/*
Expand Down Expand Up @@ -211,5 +220,5 @@
],
],

'config_version' => '2.9',
'config_version' => '2.10',
];
1 change: 1 addition & 0 deletions resources/views/error.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ $message }}
1 change: 1 addition & 0 deletions src/AutoDocServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function boot()

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

if (!$this->app->routesAreCached()) {
Expand Down
4 changes: 2 additions & 2 deletions src/Drivers/LocalDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace RonasIT\AutoDoc\Drivers;

use Illuminate\Contracts\Filesystem\FileNotFoundException;
use RonasIT\AutoDoc\Exceptions\FileNotFoundException;
use RonasIT\AutoDoc\Exceptions\MissedProductionFilePathException;

class LocalDriver extends BaseDriver
Expand Down Expand Up @@ -30,7 +30,7 @@ public function saveData(): void
public function getDocumentation(): array
{
if (!file_exists($this->prodFilePath)) {
throw new FileNotFoundException();
throw new FileNotFoundException($this->prodFilePath);
}

$fileContent = file_get_contents($this->prodFilePath);
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/RemoteDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace RonasIT\AutoDoc\Drivers;

use Illuminate\Contracts\Filesystem\FileNotFoundException;
use RonasIT\AutoDoc\Exceptions\FileNotFoundException;
use RonasIT\AutoDoc\Exceptions\MissedRemoteDocumentationUrlException;

class RemoteDriver extends BaseDriver
Expand Down
4 changes: 2 additions & 2 deletions src/Drivers/StorageDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace RonasIT\AutoDoc\Drivers;

use Illuminate\Contracts\Filesystem\FileNotFoundException;
use RonasIT\AutoDoc\Exceptions\FileNotFoundException;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Facades\Storage;
use RonasIT\AutoDoc\Exceptions\MissedProductionFilePathException;
Expand Down Expand Up @@ -34,7 +34,7 @@ public function saveData(): void
public function getDocumentation(): array
{
if (!$this->disk->exists($this->prodFilePath)) {
throw new FileNotFoundException();
throw new FileNotFoundException($this->prodFilePath);
}

$fileContent = $this->disk->get($this->prodFilePath);
Expand Down
13 changes: 13 additions & 0 deletions src/Exceptions/FileNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace RonasIT\AutoDoc\Exceptions;

use Illuminate\Contracts\Filesystem\FileNotFoundException as BaseException;

class FileNotFoundException extends BaseException
{
public function __construct(string $filePath = null)
{
parent::__construct("Documentation file not found {$filePath}");
}
}
54 changes: 31 additions & 23 deletions src/Services/SwaggerService.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use RonasIT\AutoDoc\Traits\GetDependenciesTrait;
use RonasIT\AutoDoc\Validators\SwaggerSpecValidator;
use Symfony\Component\HttpFoundation\Response;
use Exception;

/**
* @property SwaggerDriverContract $driver
Expand Down Expand Up @@ -131,14 +132,18 @@ protected function setDriver()
}
}

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

if (empty($view) && !empty($this->config['info'])) {
$view = $this->config['info']['description'];
}

$data = [
'openapi' => self::OPEN_API_VERSION,
'servers' => [
Expand All @@ -148,7 +153,7 @@ protected function generateEmptyData(): array
'components' => [
'schemas' => $this->config['definitions'],
],
'info' => $this->prepareInfo($this->config['info'])
'info' => $this->prepareInfo($view, $viewData, $license),
];

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

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

Expand Down Expand Up @@ -596,8 +601,13 @@ protected function saveParameterType(&$data, $parameter, $parameterType)
];
}

protected function saveParameterDescription(&$data, $parameter, array $rulesArray, array $attributes, array $annotations)
{
protected function saveParameterDescription(
array &$data,
string $parameter,
array $rulesArray,
array $attributes,
array $annotations
) {
$description = Arr::get($annotations, $parameter);

if (empty($description)) {
Expand Down Expand Up @@ -803,7 +813,7 @@ public function saveProductionData()
if (ParallelTesting::token()) {
$this->driver->appendProcessDataToTmpFile(function (array $sharedTmpData) {
$resultDocContent = (empty($sharedTmpData))
? $this->generateEmptyData()
? $this->generateEmptyData($this->config['info']['description'])
: $sharedTmpData;

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

public function getDocFileContent()
{
$documentation = $this->driver->getDocumentation();
try {
$documentation = $this->driver->getDocumentation();

$this->openAPIValidator->validate($documentation);
$this->openAPIValidator->validate($documentation);
} catch (Exception $exception) {
return $this->generateEmptyData($this->config['defaults']['error'], ['message' => $exception->getMessage()]);
}

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

Expand Down Expand Up @@ -946,27 +960,21 @@ protected function getDefaultValueByType($type)
return $values[$type];
}

protected function prepareInfo(array $info): array
protected function prepareInfo(?string $view = null, array $viewData = [], array $license = []): array
{
if (empty($info)) {
return $info;
}
$info = [];

foreach ($info['license'] as $key => $value) {
if (empty($value)) {
unset($info['license'][$key]);
}
}
$license = array_filter($license);

if (empty($info['license'])) {
unset($info['license']);
if (!empty($license)) {
$info['license'] = $license;
}

if (!empty($info['description'])) {
$info['description'] = view($info['description'])->render();
if (!empty($view)) {
$info['description'] = view($view, $viewData)->render();
}

return $info;
return array_merge($this->config['info'], $info);
}

protected function getOpenAPIFileContent(string $filePath): array
Expand Down
2 changes: 0 additions & 2 deletions tests/AutoDocControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

use Illuminate\Http\Response;
use phpmock\phpunit\PHPMock;
use RonasIT\Support\Traits\MockTrait;

class AutoDocControllerTest extends TestCase
{
use MockTrait;
use PHPMock;

protected static array $documentation;
Expand Down
4 changes: 2 additions & 2 deletions tests/LocalDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace RonasIT\AutoDoc\Tests;

use RonasIT\AutoDoc\Drivers\LocalDriver;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use RonasIT\AutoDoc\Exceptions\FileNotFoundException;
use RonasIT\AutoDoc\Exceptions\MissedProductionFilePathException;

class LocalDriverTest extends TestCase
Expand Down Expand Up @@ -126,7 +126,7 @@ public function testGetDocumentation()

public function testGetDocumentationFileNotExists()
{
$this->expectException(FileNotFoundException::class);
$this->assertExceptionThrew(FileNotFoundException::class, 'Documentation file not found not_exists_file');

config(['auto-doc.drivers.local.production_path' => 'not_exists_file']);

Expand Down
7 changes: 2 additions & 5 deletions tests/RemoteDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@

use RonasIT\AutoDoc\Drivers\RemoteDriver;
use RonasIT\AutoDoc\Exceptions\MissedRemoteDocumentationUrlException;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use RonasIT\Support\Traits\MockTrait;
use RonasIT\AutoDoc\Exceptions\FileNotFoundException;

class RemoteDriverTest extends TestCase
{
use MockTrait;

protected static array $tmpData;
protected static RemoteDriver $remoteDriverClass;
protected static string $tmpDocumentationFilePath;
Expand Down Expand Up @@ -151,7 +148,7 @@ public function testGetDocumentation()

public function testGetDocumentationNoFile()
{
$this->expectException(FileNotFoundException::class);
$this->assertExceptionThrew(FileNotFoundException::class, 'Documentation file not found ');

config(['auto-doc.drivers.remote.key' => 'mocked_key']);
config(['auto-doc.drivers.remote.url' => 'mocked_url']);
Expand Down
4 changes: 2 additions & 2 deletions tests/StorageDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace RonasIT\AutoDoc\Tests;

use Illuminate\Contracts\Filesystem\FileNotFoundException;
use RonasIT\AutoDoc\Exceptions\FileNotFoundException;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Facades\Storage;
use RonasIT\AutoDoc\Drivers\StorageDriver;
Expand Down Expand Up @@ -120,7 +120,7 @@ public function testGetDocumentation()

public function testGetDocumentationFileNotExists()
{
$this->expectException(FileNotFoundException::class);
$this->assertExceptionThrew(FileNotFoundException::class, 'Documentation file not found documentation.json');

self::$storageDriverClass->getDocumentation();
}
Expand Down
Loading