Skip to content
Draft
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
8 changes: 5 additions & 3 deletions src/Console/Commands/AnalyzeDocumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ public function handle(Generator $generator): int

$generator(Scramble::getGeneratorConfig($this->option('api')));

$exceptions = $generator->context->diagnostics->toExceptions();

$i = 1;
$this->groupExceptions($generator->exceptions)->each(function (Collection $exceptions, string $group) use (&$i) {
$this->groupExceptions($exceptions)->each(function (Collection $exceptions, string $group) use (&$i) {
$this->renderExceptionsGroup($exceptions, $group, $i);
});

if (count($generator->exceptions)) {
$this->error('[ERROR] Found '.count($generator->exceptions).' errors.');
if (count($exceptions)) {
$this->error('[ERROR] Found '.count($exceptions).' errors.');

return static::FAILURE;
}
Expand Down
19 changes: 19 additions & 0 deletions src/Diagnostics/Diagnostic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Dedoc\Scramble\Diagnostics;

use Illuminate\Routing\Route;
use Throwable;

interface Diagnostic
{
public function message(): string;

public function severity(): DiagnosticSeverity;

public function toException(): Throwable;

public function withRoute(?Route $route): self;

public function withSeverity(DiagnosticSeverity $severity): self;
}
9 changes: 9 additions & 0 deletions src/Diagnostics/DiagnosticSeverity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Dedoc\Scramble\Diagnostics;

enum DiagnosticSeverity
{
case Error;
case Warning;
}
43 changes: 43 additions & 0 deletions src/Diagnostics/DiagnosticsCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Dedoc\Scramble\Diagnostics;

use Illuminate\Routing\Route;
use Illuminate\Support\Collection;

class DiagnosticsCollector
{
public function __construct(
public Collection $diagnostics = new Collection,
public bool $throwOnError = false,
public ?Route $route = null,
) {}

public function report(Diagnostic $diagnostic): void
{
$diagnostic = $diagnostic->withRoute($this->route);

$this->diagnostics->push($diagnostic);

if ($this->throwOnError && $diagnostic->severity() === DiagnosticSeverity::Error) {
throw $diagnostic->toException();
}
}

public function reportQuietly(GenericDiagnostic $diagnostic): void
{
$diagnostic = $diagnostic->withRoute($this->route);

$this->diagnostics->push($diagnostic);
}

public function toExceptions(): array
{
return array_map(fn (Diagnostic $d) => $d->toException(), $this->diagnostics->all());
}

public function forRoute(Route $route): self
{
return new self($this->diagnostics, $this->throwOnError, $route);
}
}
58 changes: 58 additions & 0 deletions src/Diagnostics/GenericDiagnostic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Dedoc\Scramble\Diagnostics;

use Dedoc\Scramble\Exceptions\RouteAware;
use Exception;
use Illuminate\Routing\Route;
use Throwable;

class GenericDiagnostic implements Diagnostic
{
public function __construct(
public string $message,
public DiagnosticSeverity $severity,
private ?Throwable $originException = null,
private ?Route $route = null,
) {}

public function message(): string
{
return $this->message;
}

public function severity(): DiagnosticSeverity
{
return $this->severity;
}

public function toException(): Throwable
{
$exception = $this->originException ?? new Exception($this->message);

if ($this->route) {
$exception = $exception instanceof RouteAware ? $exception->setRoute($this->route) : $exception;
}

return $exception;
}

public function withRoute(?Route $route): self
{
return new self($this->message, $this->severity, $this->originException, $route);
}

public function withSeverity(DiagnosticSeverity $severity): self
{
return new self($this->message, $severity, $this->originException, $this->route);
}

public static function fromException(Throwable $exception): self
{
return new self(
$exception->getMessage(),
DiagnosticSeverity::Error,
$exception,
);
}
}
9 changes: 0 additions & 9 deletions src/Exceptions/InvalidSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Dedoc\Scramble\Support\Generator\Types\Type;
use Exception;
use Illuminate\Console\OutputStyle;
use Illuminate\Routing\Route;

class InvalidSchema extends Exception implements ConsoleRenderable, RouteAware
{
Expand Down Expand Up @@ -42,14 +41,6 @@ public static function createForSchema(string $message, string $path, Type $sche
return $exception;
}

public function getRouteAwareMessage(Route $route, string $msg): string
{
$method = $route->methods()[0];
$action = $route->getAction('uses');

return "'$method $route->uri' ($action): ".$msg;
}

public function renderInConsole(OutputStyle $outputStyle): void
{
$codeSample = null;
Expand Down
8 changes: 8 additions & 0 deletions src/Exceptions/RouteAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ public function getRoute(): ?Route
{
return $this->route;
}

public function getRouteAwareMessage(Route $route, string $msg): string
{
$method = $route->methods()[0];
$action = $route->getAction('uses');

return "'$method $route->uri' ($action): ".$msg;
}
}
4 changes: 3 additions & 1 deletion src/Exceptions/RulesEvaluationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
use Illuminate\Support\Arr;
use Throwable;

class RulesEvaluationException extends Exception
class RulesEvaluationException extends Exception implements RouteAware
{
use RouteAwareTrait;

/** @var array<string, Throwable> */
public array $exceptions = [];

Expand Down
Loading
Loading