<p>This is what I generally do if I want to catch exceptions but still output an error to the browser. This example uses Craft's included Guzzle class to make the http request.</p>

<pre><code>try 
{
    // create client
    $client = new \Guzzle\Http\Client('https://twitter.com');

    // get response
    $response = $client-&gt;get()-&gt;send();
}
catch (\Guzzle\Http\Exception\CurlException $e) 
{
    // throw a craft exception which displays the error cleanly
    throw new HttpException(400, 'Internet connection not available');
}
</code></pre>

<p>You can of course do anything you want inside the catch block, such as rendering a template and providing it with information about the error:</p>

<pre><code>$html = craft()-&gt;templates-&gt;render('error', array(
    'message' =&gt; 'Internet connection not available'
));
</code></pre>

CC BY-SA 3.0
2014-06-24T20:28:53.007Z
@BenCroker Thanks for mentioning Guzzle in your answer. I've been completely missing out until now!
Matt Stein
CC BY-SA 3.0
2014-06-25T13:14:00.970Z
Guzzle is amazing!!
Ben Croker
CC BY-SA 3.0
2014-06-18T06:54:27.903Z
If the call was invoked from a template and I call craft()->templates->render, what's the result? Does the template rendered by this call replace the whole page?
ray
CC BY-SA 3.0
2014-06-18T07:00:32.760Z
I've updated my answer to reflect that it simply returns html code but you can then do whatever you want with this.
Ben Croker
CC BY-SA 3.0
2014-06-18T06:21:14.090Z
2014-06-18T07:49:10.577Z
2014-06-18T07:49:10.577Z
Ben Croker
<p>Yep, Craft makes pretty liberal use of try/catch blocks. Wrap your HTTP request in that, and return whatever makes sense in the context – <code>null</code>, <code>false</code>, or maybe an empty array.</p>

<p>Probably also worth logging the error, so the developer can investigate why it’s not working, using <code>Craft::log()</code>.</p>

<p>Remember when catching exceptions, that there’s a good chance the exception class won’t be in Craft’s namespace. So you’ll probably need to prepend the exception class(es) with <code>\</code>.</p>

<p>So to put it all together:</p>

<pre><code>try
{
    // Make the HTTP request
    // ...
}
catch (\Exception $e)
{
    Craft::log('The request to domain.com failed: '.$e-&gt;getMessage(),
               LogLevel::Warning, false, 'mypluginhandle');

    return false;
}
</code></pre>

CC BY-SA 3.0
2014-06-18T06:23:30.433Z
2014-06-18T06:23:30.433Z
Brandon Kelly
<p>What's the best way to prevent exceptions in my plugin from blowing up my entire page? try/catch and return null, and then check for null in the template?</p>

<p>For example: my plugin fetches data from a remote REST API. If that request fails for any reason I get an error page. I understand why, but I'm looking for the best way to handle it. I'd appreciate any input, even if it seems glaringly obvious to you.</p>

<templating><plugin-development>
What is the best way to handle errors in plugins and templates?
# What is the best way to handle errors in plugins and templates?

(Asked by: username_0 on 2014-06-18)

What's the best way to prevent exceptions in my plugin from blowing up my entire page? try/catch and return null, and then check for null in the template?

For example: my plugin fetches data from a remote REST API. If that request fails for any reason I get an error page. I understand why, but I'm looking for the best way to handle it. I'd appreciate any input, even if it seems glaringly obvious to you.

**Tags:** templating, plugin-development


---

## 2 answers

### Answer by username_3 on 2014-06-18. Score: 13 | Accepted answer

Yep, Craft makes pretty liberal use of try/catch blocks. Wrap your HTTP request in that, and return whatever makes sense in the context – null, false, or maybe an empty arusername_0.
Probably also worth logging the error, so the developer can investigate why it’s not working, using Craft::log().
Remember when catching exceptions, that there’s a good chance the exception class won’t be in Craft’s namespace. So you’ll probably need to prepend the exception class(es) with \.
So to put it all together:
try
{
    // Make the HTTP request
    // ...
}
catch (\Exception $e)
{
    Craft::log('The request to domain.com failed: '.$e->getMessage(),
               LogLevel::Warning, false, 'mypluginhandle');

    return false;
}


---

### Answer by username_1 on 2014-06-18. Score: 8

This is what I generally do if I want to catch exceptions but still output an error to the browser. This example uses Craft's included Guzzle class to make the http request.
try 
{
    // create client
    $client = new \Guzzle\Http\Client('https://twitter.com');

    // get response
    $response = $client->get()->send();
}
catch (\Guzzle\Http\Exception\CurlException $e) 
{
    // throw a craft exception which displays the error cleanly
    throw new HttpException(400, 'Internet connection not available');
}

You can of course do anything you want inside the catch block, such as rendering a template and providing it with information about the error:
$html = craft()->templates->render('error', arusername_0(
    'message' => 'Internet connection not available'
));

> **Comments:**
> **username_0**: If the call was invoked from a template and I call craft()->templates->render, what's the result? Does the template rendered by this call replace the whole page?  
> **username_1**: I've updated my answer to reflect that it simply returns html code but you can then do whatever you want with this.  
> **username_2**: @BenCroker Thanks for mentioning Guzzle in your answer. I've been completely missing out until now!  
> **username_1**: Guzzle is amazing!!  

---

