Skip to content

163 modify 500 code error response page #174

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 5 commits 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
12 changes: 11 additions & 1 deletion resources/views/error.blade.php
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
{{ $message }}
# ❗️ **ERROR** ❗️

## 🚨🚨 {{$type}} 🚨🚨

---

### **Details:**

{{$message}}

---
20 changes: 15 additions & 5 deletions src/Services/SwaggerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
use RonasIT\AutoDoc\Traits\GetDependenciesTrait;
use RonasIT\AutoDoc\Validators\SwaggerSpecValidator;
use Symfony\Component\HttpFoundation\Response;
use Throwable;
use Exception;

/**
* @property SwaggerDriverContract $driver
*/
Expand Down Expand Up @@ -137,7 +137,12 @@ protected function generateEmptyData(?string $view = null, array $viewData = [],
// 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();
$exception = new EmptyContactEmailException();

$viewData = [
'message' => $exception->getMessage(),
'type' => $exception::class,
];
}

if (empty($view) && !empty($this->config['info'])) {
Expand Down Expand Up @@ -831,8 +836,13 @@ public function getDocFileContent()
$documentation = $this->driver->getDocumentation();

$this->openAPIValidator->validate($documentation);
} catch (Exception $exception) {
return $this->generateEmptyData($this->config['defaults']['error'], ['message' => $exception->getMessage()]);
} catch (Throwable $exception) {
$message = $exception instanceof Exception ? $exception->getMessage() : '[]';
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
$message = $exception instanceof Exception ? $exception->getMessage() : '[]';
$message = ($exception instanceof Exception) ? $exception->getMessage() : '[]';

Copy link
Contributor

Choose a reason for hiding this comment

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

@RGO230 @DenTray

Why we can't always write message?
For me always show $exception->getMessage() is fine. What are we trying to hide here?

if we trying hide only next Throwable message 'RonasIT\AutoDoc\Drivers\LocalDriver::getDocumentation(): Return value must be of type array, null returned'. Maybe we can write like kind "Documentation file is empty or have bad format" insted []


return $this->generateEmptyData($this->config['defaults']['error'], [
'message' => $message,
'type' => $exception::class,
]);
}

$additionalDocs = config('auto-doc.additional_paths', []);
Expand Down Expand Up @@ -973,7 +983,7 @@ protected function prepareInfo(?string $view = null, array $viewData = [], array
if (!empty($view)) {
$info['description'] = view($view, $viewData)->render();
}

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

Expand Down
9 changes: 7 additions & 2 deletions tests/SwaggerServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Http\Testing\File;
use PHPUnit\Framework\Attributes\DataProvider;
use RonasIT\AutoDoc\Exceptions\EmptyContactEmailException;
use RonasIT\AutoDoc\Exceptions\InvalidDriverClassException;
use RonasIT\AutoDoc\Exceptions\LegacyConfigException;
use RonasIT\AutoDoc\Exceptions\SwaggerDriverClassNotFoundException;
Expand Down Expand Up @@ -222,6 +221,10 @@ public static function getConstructorInvalidTmpData(): array
'tmpDoc' => 'documentation/invalid_format__response__invalid_items',
'fixture' => 'invalid_format_response_invalid_items.html',
],
[
'tmpDoc' => 'documentation/tmp_data_incorrect_documentation_structure_request',
'fixture' => 'invalid_format_incorrect_documentation_structure_request.html',
],
];
}

Expand All @@ -241,7 +244,9 @@ public function testEmptyContactEmail()
{
config(['auto-doc.info.contact.email' => null]);

$this->expectException(EmptyContactEmailException::class);
$content = app(SwaggerService::class)->getDocFileContent();

$this->assertEqualsFixture('invalid_config_email.html', $content['info']['description'], true);

app(SwaggerService::class);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function assertEqualsJsonFixture(string $fixtureName, $data, bool $export
public function assertEqualsFixture(string $fixtureName, $data, bool $exportMode = false): void
{
if ($exportMode || $this->globalExportMode) {
$this->exportContent($fixtureName, $data);
$this->exportContent($data, $fixtureName);
}

$this->assertEquals($this->getFixture($fixtureName), $data);
Expand Down
Loading