Skip to content

Commit

Permalink
throw an exception if there is not parser of $type registered, show h…
Browse files Browse the repository at this point in the history
…elpful error message in command.

Generate new build.
  • Loading branch information
HelgeSverre committed Sep 14, 2023
1 parent 9364198 commit d651b3a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
Binary file modified builds/sdkgenerator
Binary file not shown.
16 changes: 15 additions & 1 deletion src/Commands/GenerateSdk.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Crescat\SaloonSdkGenerator\CodeGenerator;
use Crescat\SaloonSdkGenerator\Data\Generator\Config;
use Crescat\SaloonSdkGenerator\Data\Generator\GeneratedCode;
use Crescat\SaloonSdkGenerator\Exceptions\ParserNotRegisteredException;
use Crescat\SaloonSdkGenerator\Factory;
use Crescat\SaloonSdkGenerator\Helpers\Utils;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -55,7 +56,20 @@ public function handle(): void
)
);

$specification = Factory::parse($type, $inputPath);
try {
$specification = Factory::parse($type, $inputPath);
} catch (ParserNotRegisteredException) {
// TODO: Prettier errors using termwind
$this->error("No parser registered for --type='$type'");

if (in_array($type, ['yml', 'yaml', 'json', 'xml'])) {
$this->warn('Note: the --type option is used to specify the API Specification type (ex: openapi, postman), not the file format.');
}

$this->line('Available types: '.implode(', ', Factory::getRegisteredParserTypes()));

return;
}

$result = $generator->run($specification);

Expand Down
9 changes: 9 additions & 0 deletions src/Exceptions/ParserNotRegisteredException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Crescat\SaloonSdkGenerator\Exceptions;

use Exception;

class ParserNotRegisteredException extends Exception
{
}
14 changes: 13 additions & 1 deletion src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Crescat\SaloonSdkGenerator\Contracts\Parser;
use Crescat\SaloonSdkGenerator\Data\Generator\ApiSpecification;
use Crescat\SaloonSdkGenerator\Exceptions\ParserNotRegisteredException;
use Crescat\SaloonSdkGenerator\Parsers\OpenApiParser;
use Crescat\SaloonSdkGenerator\Parsers\PostmanCollectionParser;

Expand All @@ -22,11 +23,19 @@ public static function getRegisteredParsers(): array
return self::$registeredParsers;
}

public static function getRegisteredParserTypes(): array
{
return array_keys(self::$registeredParsers);
}

public static function registerParser(string $name, string $className): void
{
self::$registeredParsers[$name] = $className;
}

/**
* @throws ParserNotRegisteredException
*/
public static function createParser(string $type, mixed $input): ?Parser
{
if (isset(self::$registeredParsers[$type])) {
Expand All @@ -40,9 +49,12 @@ public static function createParser(string $type, mixed $input): ?Parser
return new $className($input);
}

return null;
throw new ParserNotRegisteredException("No parser registered for '$type'");
}

/**
* @throws ParserNotRegisteredException
*/
public static function parse(string $type, mixed $input): ApiSpecification
{
return self::createParser($type, $input)->parse();
Expand Down

0 comments on commit d651b3a

Please sign in to comment.