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

Unhandled Exceptions on Validation #54

Open
bluec opened this issue Oct 11, 2024 · 0 comments
Open

Unhandled Exceptions on Validation #54

bluec opened this issue Oct 11, 2024 · 0 comments

Comments

@bluec
Copy link

bluec commented Oct 11, 2024

Been using this in production for a while for postcode validation and we see all sorts of unhandled exceptions when validating postcodes.

Looking at the \JustSteveKing\LaravelPostcodes\Service\PostcodeService::validate method:

  public function validate(string $postcode, bool $preValidate = false): bool
  {
      if ($preValidate) {
          return !!preg_match(config('postcodes.regex.postcode'), $postcode);
      }
  
      return $this->getResponse("postcodes/$postcode/validate");
  }

Parsing the various status and error responses would be tiresome, so as a simple fix would it be better if the request were wrapped in a try/catch block and return false on any exception....

  public function validate(string $postcode, bool $preValidate = false): bool
  {
      if ($preValidate) {
          return !!preg_match(config('postcodes.regex.postcode'), $postcode);
      }

      try {
          return $this->getResponse("postcodes/$postcode/validate");
      } catch (GuzzleException $e) {
          return false;
      }
  }

I've tried this and it works for me. Happy to do a PR if this is an acceptable solution.

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

No branches or pull requests

1 participant