Skip to content

Commit

Permalink
Add Scramble::throwOnError() to control exception handling (#645)
Browse files Browse the repository at this point in the history
* Add Scramble::throwOnError() to control exception handling

* Update src/Scramble.php

* Update src/Scramble.php

* Update src/Scramble.php

* Update src/Support/OperationExtensions/RequestBodyExtension.php

* throw when testing

---------

Co-authored-by: romalytvynenko <[email protected]>
  • Loading branch information
AbdulkarimOmer and romalytvynenko authored Dec 14, 2024
1 parent 8baa2de commit 48698ff
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/Scramble.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ class Scramble
*/
public static array $extensions = [];

/**
* Whether to throw an exception during docs generation. When `false`,
* documentation will be generated and issues added to the endpoint description
* that failed generation. When `true`, the exception will be thrown and docs
* generation will fail.
*/
public static bool $throwOnError = false;

/**
* Disables registration of default API documentation routes.
*/
Expand Down Expand Up @@ -185,4 +193,14 @@ public static function getGeneratorConfig(string $api)

return Scramble::$apis[$api];
}

public static function throwOnError(bool $throw = true): void
{
static::$throwOnError = $throw;
}

public function shouldThrowOnError(): bool
{
return static::$throwOnError;
}
}
2 changes: 1 addition & 1 deletion src/Support/OperationExtensions/RequestBodyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function handle(Operation $operation, RouteInfo $routeInfo)
try {
$rulesResults = collect($this->extractRouteRequestValidationRules($routeInfo, $routeInfo->methodNode()));
} catch (Throwable $exception) {
if (app()->environment('testing')) {
if (Scramble::shouldThrowOnError()) {
throw $exception;
}
$description = $description->append('⚠️Cannot generate request documentation: '.$exception->getMessage());
Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ protected function setUp(): void
{
parent::setUp();

Scramble::throwOnError();

$this->app->when(RulesToParameters::class)
->needs('$validationNodesResults')
->give([]);
Expand Down

0 comments on commit 48698ff

Please sign in to comment.