Skip to content

Commit c1ab18d

Browse files
committed
chore: use custom exceptions
1 parent 7eff034 commit c1ab18d

3 files changed

Lines changed: 22 additions & 9 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"require": {
1010
"php": "^8.1",
1111
"graham-campbell/guzzle-factory": "^5.0",
12-
"parceltrap/parceltrap": "^1.2"
12+
"parceltrap/parceltrap": "^1.3"
1313
},
1414
"require-dev": {
1515
"laravel/pint": "^1.2",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ParcelTrap\USPS\Exceptions;
6+
7+
use ParcelTrap\Contracts\ParcelTrapException;
8+
use RuntimeException;
9+
10+
class USPSErrorResponseException extends RuntimeException implements ParcelTrapException
11+
{
12+
}

src/USPS.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use ParcelTrap\DTOs\TrackingDetails;
1313
use ParcelTrap\Enums\Status;
1414
use ParcelTrap\Exceptions\ApiAuthenticationFailedException;
15-
use RuntimeException;
15+
use ParcelTrap\USPS\Exceptions\USPSErrorResponseException;
1616
use SimpleXMLElement;
1717

1818
class USPS implements Driver
@@ -52,17 +52,18 @@ public function find(string $identifier, array $parameters = []): TrackingDetail
5252
simplexml_load_string($this->getCleanedBody($response->getBody()->getContents()))
5353
), true, 512, JSON_THROW_ON_ERROR);
5454

55-
if (
56-
isset($json['Description']) &&
57-
str_starts_with((string) $json['Description'], 'API Authorization failure')
58-
) {
59-
throw new ApiAuthenticationFailedException($this);
55+
if (isset($json['Description'])) {
56+
throw str_starts_with((string) $json['Description'], 'API Authorization failure') ?
57+
new ApiAuthenticationFailedException($this) :
58+
new USPSErrorResponseException((string) $json['Description']);
6059
}
6160

62-
assert(isset($json['TrackInfo']), 'The tracking information is missing from the response');
61+
if (! isset($json['TrackInfo'])) {
62+
throw new USPSErrorResponseException('The tracking information is missing from the response');
63+
}
6364

6465
if (isset($json['TrackInfo']['Error']['Description'])) {
65-
throw new RuntimeException((string) $json['TrackInfo']['Error']['Description']);
66+
throw new USPSErrorResponseException((string) $json['TrackInfo']['Error']['Description']);
6667
}
6768

6869
$json = $json['TrackInfo'];

0 commit comments

Comments
 (0)