Skip to content

Commit

Permalink
cleanup exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Nielsvanpach committed Jan 31, 2024
1 parent c7b2e09 commit e288978
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/Countries/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Carbon\CarbonImmutable;
use Spatie\Holidays\Concerns\Translatable;
use Spatie\Holidays\Exceptions\InvalidYear;
use Spatie\Holidays\Exceptions\UnsupportedCountry;
use Spatie\Holidays\Exceptions\InvalidCountry;

abstract class Country
{
Expand Down Expand Up @@ -93,7 +93,7 @@ public static function findOrFail(string $countryCode): Country
$country = self::find($countryCode);

if (! $country) {
throw UnsupportedCountry::make($countryCode);
throw InvalidCountry::notFound($countryCode);
}

return $country;
Expand Down
4 changes: 2 additions & 2 deletions src/Countries/SriLanka.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Spatie\Holidays\Countries;

use Spatie\Holidays\Exceptions\UnsupportedCountry;
use Spatie\Holidays\Exceptions\InvalidCountry;

class SriLanka extends Country
{
Expand All @@ -16,6 +16,6 @@ protected function allHolidays(int $year): array
// Sri lanka has a committee that decides the holidays for the year
// instead of following a full moon calendar.

throw UnsupportedCountry::make($this->countryCode());
throw InvalidCountry::notFound($this->countryCode());
}
}
2 changes: 1 addition & 1 deletion src/Countries/Switzerland.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Switzerland extends Country
public function __construct(protected ?string $region = null)
{
if ($region !== null && ! in_array($region, self::REGIONS)) {
throw InvalidRegion::unsupportedRegion($region);
throw InvalidRegion::notFound($region);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use RuntimeException;

class UnsupportedCountry extends RuntimeException
class InvalidCountry extends RuntimeException
{
public static function make(string $countryCode): self
public static function notFound(string $countryCode): self
{
return new self("Country code `{$countryCode}` is not supported.");
}
Expand Down
7 changes: 1 addition & 6 deletions src/Exceptions/InvalidRegion.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@

class InvalidRegion extends RuntimeException
{
public static function unsupportedRegion(string $region): self
public static function notFound(string $region): self
{
return new self("Region '$region' is not supported.");
}

public static function unsupportedLocale(string $locale): self
{
return new self("Locale '$locale' is not supported.");
}
}
4 changes: 2 additions & 2 deletions tests/Countries/SriLankaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Spatie\Holidays\Tests\Countries;

use Spatie\Holidays\Exceptions\UnsupportedCountry;
use Spatie\Holidays\Exceptions\InvalidCountry;
use Spatie\Holidays\Holidays;

it('cannot calculate sri lanka holidays', function () {
Holidays::for(country: 'lk')->get();
})->throws(UnsupportedCountry::class);
})->throws(InvalidCountry::class);
4 changes: 2 additions & 2 deletions tests/HolidaysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Spatie\Holidays\Countries\Netherlands;
use Spatie\Holidays\Exceptions\InvalidLocale;
use Spatie\Holidays\Exceptions\InvalidYear;
use Spatie\Holidays\Exceptions\UnsupportedCountry;
use Spatie\Holidays\Exceptions\InvalidCountry;
use Spatie\Holidays\Holidays;

it('can get all holidays of the current year', function () {
Expand Down Expand Up @@ -43,7 +43,7 @@

it('cannot get all holidays of an unknown country code', function () {
Holidays::for(country: 'unknown');
})->throws(UnsupportedCountry::class);
})->throws(InvalidCountry::class);

it('cannot get holidays for years before 1970', function () {
Holidays::for(country: 'be', year: 1969)->get();
Expand Down

0 comments on commit e288978

Please sign in to comment.