Skip to content

Commit 65608ef

Browse files
authored
Merge pull request #21 from utopia-php/feat-additiona-error-details
Feat: return additional error object with exception
2 parents d227a32 + 931955c commit 65608ef

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/Pay/Adapter/Stripe.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ protected function handleError(int $code, mixed $response)
359359
$type = $error['decline_code'] ?? $type;
360360
}
361361
$message = $error['message'] ?? 'Unknown error';
362-
throw new Exception($type, $message, $code);
362+
throw new Exception($type, $message, $code, $error);
363363
}
364364

365365
throw new Exception($response, $code);

src/Pay/Exception.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,18 @@ class Exception extends \Exception
1616

1717
protected string $type = '';
1818

19-
public function __construct(string $type = Exception::GENERAL_UNKNOWN, string $message = null, int $code = null, \Throwable $previous = null)
19+
/**
20+
* Metadata object with additional error data
21+
*
22+
* @var array
23+
*/
24+
protected array $metadata = [];
25+
26+
public function __construct(string $type = Exception::GENERAL_UNKNOWN, string $message = null, int $code = null, array $metadata = [], \Throwable $previous = null)
2027
{
2128
$this->type = $type;
2229
$this->code = $code ?? 500;
30+
$this->metadata = $metadata;
2331

2432
$this->message = $message ?? 'Unknown error';
2533

@@ -46,4 +54,25 @@ public function setType(string $type): void
4654
{
4755
$this->type = $type;
4856
}
57+
58+
/**
59+
* Get metadata object.
60+
*
61+
* @return string
62+
*/
63+
public function getMetadata(): array
64+
{
65+
return $this->metadata;
66+
}
67+
68+
/**
69+
* Set metadata object.
70+
*
71+
* @param array $metadata
72+
* @return void
73+
*/
74+
public function setMetadata(array $metadata): void
75+
{
76+
$this->metadata = $metadata;
77+
}
4978
}

tests/Pay/Adapter/StripeTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,8 @@ public function testErrorHandling(): void
408408
} catch (Exception $e) {
409409
$this->assertEquals(402, $e->getCode());
410410
$this->assertEquals(Exception::AUTHENTICATION_REQUIRED, $e->getType());
411+
$this->assertNotEmpty($e->getMetadata());
412+
$this->assertEquals(Exception::AUTHENTICATION_REQUIRED, $e->getMetadata()['decline_code']);
411413
$this->assertInstanceOf(Exception::class, $e);
412414
}
413415

0 commit comments

Comments
 (0)