Skip to content

Commit 5c05aca

Browse files
tanthammarclaude
andcommitted
Enhance BusinessNameFromVatID service with improved error handling
- Add Invalid error case to BusinessNameLookupError enum for invalid VAT IDs - Add label() method to BusinessNameLookupError for human-readable messages - Pre-validate VAT format before external API calls to avoid unnecessary requests - Update README documentation to reflect new error handling capabilities 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 9a9e4b8 commit 5c05aca

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,11 @@ if ($result instanceof BusinessNameLookupError) {
185185
match ($result) {
186186
BusinessNameLookupError::Unknown => 'VAT ID not found',
187187
BusinessNameLookupError::ServiceUnavailable => 'VAT service temporarily unavailable',
188+
BusinessNameLookupError::Invalid => 'Invalid VAT ID format',
188189
};
190+
191+
// Or use the built-in label method
192+
$errorMessage = $result->label();
189193
} else {
190194
// Success - $result is the business name string
191195
$businessName = $result;

src/Enums/BusinessNameLookupError.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,14 @@ enum BusinessNameLookupError: string
66
{
77
case Unknown = 'unknown';
88
case ServiceUnavailable = 'service_unavailable';
9+
case Invalid = 'invalid'; //Vat number is invalid, unable to get legal name
10+
11+
public function label(): string
12+
{
13+
return match ($this) {
14+
self::Unknown => 'Unknown',
15+
self::ServiceUnavailable => 'Service Unavailable',
16+
self::Invalid => 'Invalid VAT ID',
17+
};
18+
}
919
}

src/Services/BusinessNameFromVatID.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Mpociot\VatCalculator\Exceptions\VATCheckUnavailableException;
66
use Mpociot\VatCalculator\VatCalculator;
77
use TantHammar\LaravelRules\Enums\BusinessNameLookupError;
8+
use TantHammar\LaravelRules\Rules\VatNumberFormat;
89

910
class BusinessNameFromVatID
1011
{
@@ -15,6 +16,12 @@ public static function lookup(string $vatID): object
1516
}
1617

1718
try {
19+
20+
//do simple validation before calling external api
21+
if (! (new VatNumberFormat)->passes(null, $vatID)) {
22+
return BusinessNameLookupError::Invalid;
23+
}
24+
1825
// Do not use Facade. Configure VatCalculator to throw an error when country != GB, else only bool false is returned
1926
$calculator = new VatCalculator(['forward_soap_faults' => true]);
2027

0 commit comments

Comments
 (0)