Skip to content

Commit

Permalink
workflow exception wraps error, provides access
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Jan 22, 2025
1 parent b8faec1 commit 1956f92
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
53 changes: 53 additions & 0 deletions src/Exceptions/WorkflowException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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;

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

/**
* Exception thrown when validating Workflow runtime.
*/
final class WorkflowException extends Exception
{
/**
* 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,
string $message,
Throwable $throwable,
) {
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();
}
}
10 changes: 6 additions & 4 deletions src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Amp\Parallel\Worker\Execution;
use Chevere\Parameter\Interfaces\CastInterface;
use Chevere\Workflow\Exceptions\WorkflowException;
use Chevere\Workflow\Interfaces\JobInterface;
use Chevere\Workflow\Interfaces\ResponseReferenceInterface;
use Chevere\Workflow\Interfaces\RunInterface;
Expand Down Expand Up @@ -91,14 +92,15 @@ public function withRunJob(string $name): RunnerInterface
try {
$response = cast($action(...$arguments));
} catch (Throwable $e) {
throw new $e(
previous: $e->getPrevious(),
code: $e->getCode(),
throw new WorkflowException(
name: $name,
job: $job,
throwable: $e,
message: (string) message(
'%message% at job `%name%` declared in %fileLine%',
name: $name,
message: $e->getMessage(),
fileLine: $job->caller(),
fileLine: strval($job->caller()),
)
);
}
Expand Down

0 comments on commit 1956f92

Please sign in to comment.