Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKaustav Banerjee
5,786 PointsExceptions Code not running properly!
Hi,
I am trying the run the code as shown in the tutorial video for PHP Exceptions and is encountering the following error:
Fatal error: Uncaught Error: Class 'Buzz\Browser' not found in /home/treehouse/workspace/index.php:15 Stack trace: #0 /home/treehouse/workspace/index.php(39): fetchHttpBody('http ://example...') #1 {main} thrown in /home/treehouse/workspace/index.php on line 15
I rechecked the code with the original code, but still I am facing difficulty to identify the error. Experts assistance in this case would be very helpful indeed. Please help.
Thanks & Regards, Kaustav
2 Answers
Kaustav Banerjee
5,786 PointsHi Tim,
Sure. No problem. Here is the code which I was working on. Hope this helps.
<?php
ini_set('display_errors', 1);
include "./vendor/autoload.php";
//Make some custom exceptions
class HttpRedirectException extends Exception {}
class HttpClientException extends Exception {}
class HttpServerException extends Exception {}
function fetchHttpBody($url)
{
$browser = new Buzz\Browser();
$response = $browser->get($url);
$content = $response->getContent();
$statusCode = $response->getStatusCode();
$statusGroup = substr((string) $statusCode, 0, 1);
switch ($statusGroup) {
case '2':
return $content;
case '3':
throw new HttpRedirectException('HTTP request was redirected', $statusCode);
case '4':
throw new HttpClientException('You made a bad request', $statusCode);
case '5':
throw new HttpServerException('The server you tried calling is not ok', $statusCode);
default:
throw new Exception('Got an unexpected status code of '.$statusGroup);
}
}
//Now try this request
try {
echo fetchHttpBody('http ://example.org/');
} catch (HttpRedirectException $e) {
printf('Redirect Error: %s (code %d)', $e->getMessage(), $e->getCode());
} catch (HttpClientException $e) {
printf('Client Error: %s (code %d)', $e->getMessage(), $e->getCode());
} catch (HttpServerException $e) {
printf('Server Error: %s (code %d)', $e->getMessage(), $e->getCode());
} catch (Exception $e) {
echo "General Error: ".$e->getMessage();
}
Tim Knight
28,888 PointsHi Kaustav,
Sorry I'm just getting back to you now. The video in question doesn't actually tell you everything you need to know to run this code because the focus of the video really is about understanding how to catch exceptions within the code's execution. As I've had time to review everything the reason you're getting this error is because you don't actually have Buzz install through composer. As I mentioned again, actually running this code isn't essentially to the exercise. If you did however really want to run this you'll want to install the Buzz package into the project with composer. So from the terminal, while in the directory of the project you'd run composer require kriswallsmith/buzz
this will add the package to your composer.json file and get it installed into your vendor folder. The line include "./vendor/autoload.php";
currently in your code will get the package loaded into your code base allowing you to access the Class with new Buzz\Browser();
.
Tim Knight
28,888 PointsTim Knight
28,888 PointsHi Kaustav,
I know you mentioned it was the exact to the course code but maybe you could share a link to the workspace or post the code here so we can take a look at it? Sometimes it just takes a second pair of eyes.