Ana Liza Pandac
Ana Liza Pandac

Reputation: 4871

How to catch an error thrown by a third party library in PHP

I'm using the phpWhois package in my Laravel application for performing whois lookup.

It's working fine except that for some websites that I enter, this error always occurs:

'ErrorException in whois.gtld.godaddy.php line 50: Undefined index: owner'

I googled it and found that this problem already has an open issue on Github and also an existing pull request.

I don't want to copy and paste the suggested fix in the source code because it means I have to do it in every time I do composer install.

So I decided to catch the error instead but I don't know how.

I found a somewhat similar question here and tried the accepted solution but it is still throwing the exception.

Here's the existing code that I tried:

$whois = new \Whois();

try {
    $result = $whois->lookup($data['name']);
} catch (Exception $e) {
    return response()->json(['error' => $e]);
}

I would appreciate any comment/help.

Thanks for your time.

Upvotes: 2

Views: 988

Answers (1)

joson george
joson george

Reputation: 56

If you want to catch an exception,You follow below mentioned programming style.

try {
    $whois = new \Whois();
    $result = $whois->lookup($data['name']);
} catch (\Exception $e) {
    \var_dump($e->getMessage());
}

This above mentioned method work in almost all PHP frameworks.

Upvotes: 3

Related Questions