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

Specific Exception on connection error #54

Merged
merged 2 commits into from
Oct 6, 2012
Merged

Specific Exception on connection error #54

merged 2 commits into from
Oct 6, 2012

Conversation

luads
Copy link
Contributor

@luads luads commented Sep 18, 2012

Just making a little easier to handle a "Unable to connect" exception.

nategood added a commit that referenced this pull request Oct 6, 2012
Specific Exception on connection error
@nategood nategood merged commit fcaa366 into nategood:master Oct 6, 2012
nategood pushed a commit that referenced this pull request Oct 6, 2012
 - REFACTOR [PR #49](#49) Broke headers out into their own class
 - REFACTOR [PR #54](#54) Added more specific Exceptions
 - FIX [PR #58](#58) Fixes throwing an error on an empty xml response
 - FEATURE [PR #57](#57) Adds support for digest authentication
@hyildirim
Copy link

Hello:

Can you please provide an example for catching this error in the code using the class? I shut down the API server that I am hitting and I do see "unable to connect" in the logs, but I cannot catch it within the code and tell user that something went wrong.

Here is the simple try/catch block I am using.

try
{
$response = Request::get($apiURL)
->expectsJson()
->sendIt();

echo "

";
if (!$response->hasErrors())
{
$body = $response->body;
foreach ($body as $k => $v)
{
print "$k=$v
\n";
}
}
else
{
echo "API returned non-200 " . $response->code . "
\n";
}
}
catch (Exception $e)
{
throw new Exception( 'Something really gone wrong', 0, $e);
}

@luads
Copy link
Contributor Author

luads commented Jun 11, 2013

@hyildirim you just need to catch the specific exception, like this:

try {
    $users = $api->get('users/list');
} catch (\Httpful\Exception\ConnectionErrorException $e) {
    // something went wrong, redirect the user to a "offline" page
}

@hyildirim
Copy link

I appreciate the quick response. That worked like a charm. Thx very much!

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.

3 participants