Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a common exception interface #53

Merged
merged 1 commit into from
Jan 29, 2020

Conversation

Einenlum
Copy link
Contributor

@Einenlum Einenlum commented Jan 16, 2020

First of all, thanks for this great library :).

Right now, if I want to check if a country code is valid, I need to do this:

try {
    (new ISO3166())->alpha2($value);

    return true;
} catch (InvalidArgumentException $e) {
} catch (OutOfBoundsException $e) {
} catch (DomainException $e) {
}

return false;

or (for PHP ^7.1):

try {
    (new ISO3166())->alpha2($value);

    return true;
} catch (InvalidArgumentException | OutOfBoundsException | DomainException $e) {
    return false;
}

This pull request proposes to add a common exception interface for these three exceptions. Therefore I don't have to check first if my $value argument is a string that matches 2 chars. If there is an ISO3166Exception, and I don't need more details, I know right away that the value I gave leads to nowhere.

New possible behavior (with no BC break):

try {
    (new ISO3166())->alpha2($value);

    return true;
} catch (\League\ISO3166\Exception\ISO3166Exception $e) {
    return false;
}

@codecov
Copy link

codecov bot commented Jan 16, 2020

Codecov Report

Merging #53 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##             master    #53   +/-   ##
=======================================
  Coverage       100%   100%           
  Complexity       33     33           
=======================================
  Files             3      3           
  Lines            75     75           
=======================================
  Hits             75     75

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 86f9972...dc18f9a. Read the comment docs.

@alcohol
Copy link
Owner

alcohol commented Jan 27, 2020

While you could achieve the same by simply using catch (Exception $e), I can see the value for a namespaced interface. I would however like to propose the new interface extends from \Throwable. What are your thoughts on this?

@alcohol
Copy link
Owner

alcohol commented Jan 27, 2020

Also sorry for not replying sooner, it seems this repository was no longer on my watched list after I did a perhaps too rigorous cleanup of said list.

@Einenlum
Copy link
Contributor Author

I would however like to propose the new interface extends from \Throwable. What are your thoughts on this?

Oh, clever! 👍

Also sorry for not replying sooner, it seems this repository was no longer on my watched list after I did a perhaps too rigorous cleanup of said list.

No worries, it's definitely not an urgent "issue" ^^. And less than 2 weeks for an answer in FOSS is clearly a good metric :) Thanks for your answer.

@alcohol
Copy link
Owner

alcohol commented Jan 28, 2020

When you can find the time to add the extends from, I'll gladly merge this and tag a new release :-)

@Einenlum Einenlum force-pushed the refacto/iso-exception-interface branch from ca75073 to dc18f9a Compare January 28, 2020 15:15
@Einenlum
Copy link
Contributor Author

@alcohol Done :)

@alcohol alcohol merged commit ac5e94a into alcohol:master Jan 29, 2020
@alcohol
Copy link
Owner

alcohol commented Jan 29, 2020

Cheers https://github.com/thephpleague/iso3166/releases/tag/2.1.5

@Einenlum Einenlum deleted the refacto/iso-exception-interface branch January 30, 2020 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants