Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Jan 26, 2025
1 parent 04fedd5 commit 5fcedb9
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 50 deletions.
36 changes: 2 additions & 34 deletions src/Exceptions/JobsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,9 @@

namespace Chevere\Workflow\Exceptions;

use Chevere\Workflow\Interfaces\JobInterface;
use Exception;
use Throwable;

/**
* Exception thrown by the Jobs runtime.
* Exception thrown by the Jobs runtime (static).
*/
final class JobsException extends Exception
final class JobsException extends WorkflowException
{
/**
* The job name that thrown the exception.
*/
public readonly string $name;

/**
* The job that thrown the exception.
*/
public readonly JobInterface $job;

/**
* The exception thrown by the job.
*/
public readonly Throwable $throwable;

public function __construct(
string $name,
JobInterface $job,
Throwable $throwable,
) {
$message = "[{$name}]: " . $throwable->getMessage();
parent::__construct(message: $message, previous: $throwable);
$this->name = $name;
$this->job = $job;
$this->throwable = $throwable;
$this->file = $job->caller()->file();
$this->line = $job->caller()->line();
}
}
21 changes: 21 additions & 0 deletions src/Exceptions/RunnerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <rodolfo@chevere.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Chevere\Workflow\Exceptions;

/**
* Exception thrown by the Workflow runner (dynamic).
*/
final class RunnerException extends WorkflowException
{
}
17 changes: 14 additions & 3 deletions src/Exceptions/WorkflowException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
use Chevere\Workflow\Interfaces\JobInterface;
use Exception;
use Throwable;
use function Chevere\Message\message;

/**
* Exception thrown by the Workflow runner.
* Exception thrown by a Workflow participant.
*/
final class WorkflowException extends Exception
abstract class WorkflowException extends Exception
{
/**
* The job name that thrown the exception.
Expand All @@ -40,14 +41,24 @@ final class WorkflowException extends Exception
public function __construct(
string $name,
JobInterface $job,
string $message,
Throwable $throwable,
) {
$message = (string) message(
$this->template(),
name: $name,
message: $throwable->getMessage(),
caller: $job->caller()
);
parent::__construct(message: $message, previous: $throwable);
$this->name = $name;
$this->job = $job;
$this->throwable = $throwable;
$this->file = $job->caller()->file();
$this->line = $job->caller()->line();
}

protected function template(): string
{
return '[%name%]: %message%';
}
}
11 changes: 2 additions & 9 deletions src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use Amp\Parallel\Worker\Execution;
use Chevere\Parameter\Interfaces\CastInterface;
use Chevere\Workflow\Exceptions\WorkflowException;
use Chevere\Workflow\Exceptions\RunnerException;
use Chevere\Workflow\Interfaces\JobInterface;
use Chevere\Workflow\Interfaces\ResponseReferenceInterface;
use Chevere\Workflow\Interfaces\RunInterface;
Expand All @@ -25,7 +25,6 @@
use Throwable;
use function Amp\Future\await;
use function Amp\Parallel\Worker\submit;
use function Chevere\Message\message;
use function Chevere\Parameter\cast;

final class Runner implements RunnerInterface
Expand Down Expand Up @@ -92,16 +91,10 @@ public function withRunJob(string $name): RunnerInterface
try {
$response = cast($action(...$arguments));
} catch (Throwable $e) {
throw new WorkflowException(
throw new RunnerException(
name: $name,
job: $job,
throwable: $e,
message: (string) message(
'%message% at job `%name%` declared in %fileLine%',
name: $name,
message: $e->getMessage(),
fileLine: strval($job->caller()),
)
);
}
$new->addJobResponse($name, $response);
Expand Down
8 changes: 6 additions & 2 deletions src/Traits/ExpectWorkflowExceptionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Chevere\Workflow\Traits;

use Chevere\Workflow\Exceptions\WorkflowException;
use Chevere\Workflow\Exceptions\RunnerException;
use Closure;

/**
Expand Down Expand Up @@ -42,7 +42,11 @@ private function expectWorkflowException(
): void {
try {
$closure();
} catch (WorkflowException $e) {
} catch (RunnerException $e) {
$this->assertSame(
$e->getMessage(),
"[{$job}]: {$message}"
);
$this->assertInstanceOf($instance, $e->throwable);
if ($job !== null) {
$this->assertSame($job, $e->name);
Expand Down
5 changes: 3 additions & 2 deletions tests/RunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,11 @@ public function testActionThrows(): void
{
$closure = fn () => run(
workflow(
job1: sync(new TestActionThrows()),
job1: sync(
new TestActionThrows()
),
)
);

$this->expectWorkflowException(
closure: $closure,
instance: Exception::class,
Expand Down

0 comments on commit 5fcedb9

Please sign in to comment.