Skip to content

Commit 77eb97a

Browse files
authored
Merge pull request #666: Expose Application Failure Category
2 parents 7414627 + 7e886df commit 77eb97a

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/**
4+
* This file is part of Temporal package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Temporal\Exception\Failure;
13+
14+
/**
15+
* Used to categorize application failures, for example, to distinguish benign errors from others.
16+
*
17+
* @see \Temporal\Api\Enums\V1\ApplicationErrorCategory
18+
*/
19+
enum ApplicationErrorCategory: int
20+
{
21+
case Unspecified = 0;
22+
23+
/**
24+
* Expected application error with little/no severity.
25+
*/
26+
case Benign = 1;
27+
}

src/Exception/Failure/ApplicationFailure.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class ApplicationFailure extends TemporalFailure
4949
/**
5050
* @param ValuesInterface|null $details Optional details about the failure.
5151
* @param \DateInterval|null $nextRetryDelay Delay before the next retry attempt.
52+
* @param ApplicationErrorCategory $category The category of the application failure.
5253
*/
5354
public function __construct(
5455
string $message,
@@ -57,6 +58,7 @@ public function __construct(
5758
?ValuesInterface $details = null,
5859
?\Throwable $previous = null,
5960
private ?\DateInterval $nextRetryDelay = null,
61+
private readonly ApplicationErrorCategory $category = ApplicationErrorCategory::Unspecified,
6062
) {
6163
parent::__construct(
6264
self::buildMessage(\compact('message', 'type', 'nonRetryable')),
@@ -103,4 +105,9 @@ public function setNextRetryDelay(?\DateInterval $nextRetryDelay): void
103105
{
104106
$this->nextRetryDelay = $nextRetryDelay;
105107
}
108+
109+
public function getApplicationErrorCategory(): ApplicationErrorCategory
110+
{
111+
return $this->category;
112+
}
106113
}

src/Exception/Failure/FailureConverter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public static function mapExceptionToFailure(\Throwable $e, DataConverterInterfa
7070
$info = new ApplicationFailureInfo();
7171
$info->setType($e->getType());
7272
$info->setNonRetryable($e->isNonRetryable());
73+
$info->setCategory($e->getApplicationErrorCategory()->value);
7374

7475
// Set Next Retry Delay
7576
$nextRetry = DateInterval::toDuration($e->getNextRetryDelay());
@@ -181,6 +182,7 @@ private static function createFailureException(Failure $failure, DataConverterIn
181182
$details,
182183
$previous,
183184
DateInterval::parseOrNull($info->getNextRetryDelay()),
185+
ApplicationErrorCategory::tryFrom($info->getCategory()) ?? ApplicationErrorCategory::Unspecified,
184186
);
185187

186188
case $failure->hasTimeoutFailureInfo():

tests/Unit/Exception/FailureConverterTestCase.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Temporal\Api\Failure\V1\Failure;
1111
use Temporal\DataConverter\DataConverter;
1212
use Temporal\DataConverter\EncodedValues;
13+
use Temporal\Exception\Failure\ApplicationErrorCategory;
1314
use Temporal\Exception\Failure\ApplicationFailure;
1415
use Temporal\Exception\Failure\FailureConverter;
1516
use Temporal\Tests\Unit\AbstractUnit;
@@ -178,5 +179,23 @@ public function testMapExceptionToFailure(): void
178179
$this->assertTrue($failure->getApplicationFailureInfo()->getNonRetryable());
179180
$this->assertEmpty($failure->getApplicationFailureInfo()->getDetails());
180181
$this->assertNull($failure->getApplicationFailureInfo()->getNextRetryDelay());
182+
$this->assertSame(0, $failure->getApplicationFailureInfo()->getCategory());
183+
}
184+
185+
public function testMapAppFailureWithCategory(): void
186+
{
187+
$converter = DataConverter::createDefault();
188+
$exception = new ApplicationFailure(
189+
'message',
190+
'type',
191+
true,
192+
category: ApplicationErrorCategory::Benign,
193+
);
194+
195+
$failure = FailureConverter::mapExceptionToFailure($exception, $converter);
196+
$newException = FailureConverter::mapFailureToException($failure, $converter);
197+
198+
self::assertInstanceOf(ApplicationFailure::class, $newException);
199+
$this->assertSame(ApplicationErrorCategory::Benign, $newException->getApplicationErrorCategory());
181200
}
182201
}

0 commit comments

Comments
 (0)