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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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::swagger-error',
],

/*
Expand Down Expand Up @@ -184,5 +193,5 @@
'development',
],

'config_version' => '2.8',
'config_version' => '2.9',
];
1 change: 1 addition & 0 deletions resources/views/swagger-error.blade.php
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's rename file to error.blade.php, need to unlink logic from the swagger usage only

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ $message }}
4 changes: 4 additions & 0 deletions src/AutoDocServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public function boot()
__DIR__ . '/../resources/views/swagger-description.blade.php' => resource_path('views/vendor/auto-doc/swagger-description.blade.php'),
], 'view');

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

if (!$this->app->routesAreCached()) {
require __DIR__ . '/Http/routes.php';
}
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/LocalDriver.php
Original file line number Diff line number Diff line change
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("Documentation file not found :{$this->prodFilePath}");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's move error message to the exception and just pass the path as arg

Suggested change
throw new FileNotFoundException("Documentation file not found :{$this->prodFilePath}");
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 @@ -36,7 +36,7 @@ public function getDocumentation(): array
list($content, $statusCode) = $this->makeHttpRequest('get', $this->getUrl());

if (empty($content) || $statusCode !== 200) {
throw new FileNotFoundException();
throw new FileNotFoundException('Documentation file not found.');
}

return json_decode($content, true);
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/StorageDriver.php
Original file line number Diff line number Diff line change
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("Documentation file not found :{$this->prodFilePath}");
}

$fileContent = $this->disk->get($this->prodFilePath);
Expand Down
43 changes: 29 additions & 14 deletions src/Services/SwaggerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,21 @@ protected function generateEmptyData(): array
throw new EmptyContactEmailException();
}

$data = [
$data = $this->generateBaseDataObject();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure we need to make this change, could you please revert it?

$data['info'] = $this->prepareInfo($this->config['info']);

$securityDefinitions = $this->generateSecurityDefinition();

if (!empty($securityDefinitions)) {
$data['securityDefinitions'] = $securityDefinitions;
}

return $data;
}

protected function generateBaseDataObject(): array
{
return [
'openapi' => self::OPEN_API_VERSION,
'servers' => [
['url' => URL::query($this->config['basePath'])],
Expand All @@ -147,16 +161,7 @@ protected function generateEmptyData(): array
'components' => [
'schemas' => $this->config['definitions'],
],
'info' => $this->prepareInfo($this->config['info'])
];

$securityDefinitions = $this->generateSecurityDefinition();

if (!empty($securityDefinitions)) {
$data['securityDefinitions'] = $securityDefinitions;
}

return $data;
}

protected function generateSecurityDefinition(): ?array
Expand Down Expand Up @@ -804,9 +809,19 @@ public function saveProductionData()

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

$this->openAPIValidator->validate($documentation);
} catch (\Exception $exception) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} catch (\Exception $exception) {
} catch (Exception $exception) {

$data = $this->generateBaseDataObject();

$this->openAPIValidator->validate($documentation);
$this->config['info'] = Arr::set($this->config, 'info.description', Arr::get($this->config, 'defaults.error'));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's pass the description view as arg, it would be better to redefine the config field


$data['info'] = $this->prepareInfo($this->config['info'], ['message' => $exception->getMessage()]);

return $data;
}

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

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

protected function prepareInfo(array $info): array
protected function prepareInfo(array $info, array $descriptionData = []): array
{
if (empty($info)) {
return $info;
Expand All @@ -948,7 +963,7 @@ protected function prepareInfo(array $info): array
}

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

return $info;
Expand Down
Loading