Skip to content

Commit

Permalink
Improved exceptions management + request body
Browse files Browse the repository at this point in the history
  • Loading branch information
sebprt committed Oct 25, 2023
1 parent 4ad74dc commit 9e5e7bf
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Kiboko\Component\Satellite\Cloud\DTO\PipelineId;
use Kiboko\Component\Satellite\Cloud\DTO\Step;
use Kiboko\Component\Satellite\Cloud\DTO\StepCode;

final class AddBeforePipelineStepCommand implements Command
{
public function __construct(
Expand Down
2 changes: 1 addition & 1 deletion src/Cloud/Command/Workflow/RemoveWorkflowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
final class RemoveWorkflowCommand implements Command
{
public function __construct(
public WorkflowId $workflow,
public WorkflowId $id,
) {}
}
7 changes: 7 additions & 0 deletions src/Cloud/DeclareWorkflowFailedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\Satellite\Cloud;

class DeclareWorkflowFailedException extends \RuntimeException {}
15 changes: 15 additions & 0 deletions src/Cloud/Event/Workflow/WorkflowRemoved.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Kiboko\Component\Satellite\Cloud\Event\Workflow;

class WorkflowRemoved
{
public function __construct(
private readonly string $id,
) {}

public function getId(): string
{
return $this->id;
}
}
43 changes: 37 additions & 6 deletions src/Cloud/Handler/Workflow/DeclareWorkflowCommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public function __construct(

public function __invoke(Cloud\Command\Workflow\DeclareWorkflowCommand $command): Cloud\Event\Workflow\WorkflowDeclared
{
$result = null;
try {
/** @var Api\Model\WorkflowDeclareWorkflowCommandJsonldRead $result */
$result = $this->client->declareWorkflowWorkflowCollection(
Expand All @@ -42,14 +41,46 @@ public function __invoke(Cloud\Command\Workflow\DeclareWorkflowCommand $command)
->setType($repository->type)
->setUrl($repository->url)
))
),
)
->setJobs(
$command->jobs->map(
function (Cloud\DTO\Workflow\JobInterface $job) {
if ($job instanceof Cloud\DTO\Workflow\Pipeline) {
return (new Api\Model\Job())
->setCode($job->code->asString())
->setLabel($job->label)
->setPipeline(
(new Api\Model\Pipeline())
->setSteps(
$job->stepList->map(
fn (Cloud\DTO\Step $step) => (new Api\Model\Step())
->setCode($step->code->asString())
->setLabel($step->label)
->setConfiguration($step->config)
)
)
);
}

if ($job instanceof Cloud\DTO\Workflow\Action) {
return (new Api\Model\Job())
->setCode($job->code->asString())
->setLabel($job->label)
->setAction(
(new Api\Model\Action())
->setConfiguration($job->configuration)
);
}

throw new \RuntimeException('Unexpected instance of PipelineInterface.');
}
)
),
);
} catch (Api\Exception\DeclareWorkflowWorkflowCollectionBadRequestException $exception) {
throw new Cloud\DeclarePipelineFailedException('Something went wrong while declaring the pipeline. Maybe your client is not up to date, you may want to update your Gyroscops client.', previous: $exception);
throw new Cloud\DeclareWorkflowFailedException('Something went wrong while declaring the workflow. Maybe your client is not up to date, you may want to update your Gyroscops client.', previous: $exception);
} catch (Api\Exception\DeclareWorkflowWorkflowCollectionUnprocessableEntityException $exception) {
throw new Cloud\DeclarePipelineFailedException('Something went wrong while declaring the pipeline. It seems the data you sent was invalid, please check your input.', previous: $exception);
} catch (\Exception $exception) {
var_dump($exception);
throw new Cloud\DeclareWorkflowFailedException('Something went wrong while declaring the workflow. It seems the data you sent was invalid, please check your input.', previous: $exception);
}

return new Cloud\Event\Workflow\WorkflowDeclared($result->getId());
Expand Down
20 changes: 17 additions & 3 deletions src/Cloud/Handler/Workflow/RemoveWorkflowCommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,31 @@

namespace Kiboko\Component\Satellite\Cloud\Handler\Workflow;

use Gyroscops\Api;
use Kiboko\Component\Satellite\Cloud;

final class RemoveWorkflowCommandHandler
final readonly class RemoveWorkflowCommandHandler
{
public function __construct(
\Gyroscops\Api\Client $client,
private \Gyroscops\Api\Client $client,
) {
}

public function __invoke(Cloud\Command\Workflow\DeclareWorkflowCommand $command)
public function __invoke(Cloud\Command\Workflow\RemoveWorkflowCommand $command): Cloud\Event\Workflow\WorkflowRemoved
{
try {
/** @var Api\Model\WorkflowRemoveWorkflowCommandJsonldRead $result */
$result = $this->client->softDeleteWorkflowItem(
$command->id->asString()
);
} catch (Api\Exception\SoftDeleteWorkflowItemBadRequestException $exception) {
throw new Cloud\RemoveWorkflowFailedException('Something went wrong while removing the workflow. Maybe your client is not up to date, you may want to update your Gyroscops client.', previous: $exception);
} catch (Api\Exception\SoftDeleteWorkflowItemUnprocessableEntityException $exception) {
throw new Cloud\RemoveWorkflowFailedException('Something went wrong while removing the workflow. It seems the data you sent was invalid, please check your input.', previous: $exception);
} catch (Api\Exception\SoftDeleteWorkflowItemNotFoundException $exception) {
throw new Cloud\RemoveWorkflowFailedException('Something went wrong while removing the workflow. It seems the data you want to delete do not exists.', previous: $exception);
}

return new Cloud\Event\Workflow\WorkflowRemoved($result->getId());
}
}
7 changes: 7 additions & 0 deletions src/Cloud/RemoveWorkflowFailedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\Satellite\Cloud;

class RemoveWorkflowFailedException extends \RuntimeException {}

0 comments on commit 9e5e7bf

Please sign in to comment.