Browse Source

Report problem instead of going down in flames when solr isn't running

Jordi Boggiano 12 years ago
parent
commit
e8fd660359
1 changed files with 26 additions and 8 deletions
  1. 26 8
      src/Packagist/WebBundle/Controller/WebController.php

+ 26 - 8
src/Packagist/WebBundle/Controller/WebController.php

@@ -152,10 +152,18 @@ class WebController extends Controller
                 $paginator->setCurrentPage($req->query->get('page', 1), false, true);
 
                 if ($req->getRequestFormat() === 'json') {
-                    $result = array(
-                        'results' => array(),
-                        'total' => $paginator->getNbResults(),
-                    );
+                    try {
+                        $result = array(
+                            'results' => array(),
+                            'total' => $paginator->getNbResults(),
+                        );
+                    } catch (\Solarium_Client_HttpException $e) {
+                        return new JsonResponse(array(
+                            'status' => 'error',
+                            'message' => 'Could not connect to the search server',
+                        ), 500);
+                    }
+
                     foreach ($paginator as $package) {
                         $url = $this->generateUrl('view_package', array('name' => $package->name), true);
 
@@ -177,10 +185,20 @@ class WebController extends Controller
                 }
 
                 if ($req->isXmlHttpRequest()) {
-                    return $this->render('PackagistWebBundle:Web:list.html.twig', array(
-                        'packages' => $paginator,
-                        'noLayout' => true,
-                    ));
+                    try {
+                        return $this->render('PackagistWebBundle:Web:list.html.twig', array(
+                            'packages' => $paginator,
+                            'noLayout' => true,
+                        ));
+                    } catch (\Twig_Error_Runtime $e) {
+                        if (!$e->getPrevious() instanceof \Solarium_Client_HttpException) {
+                            throw $e;
+                        }
+                        return new JsonResponse(array(
+                            'status' => 'error',
+                            'message' => 'Could not connect to the search server',
+                        ), 500);
+                    }
                 }
 
                 return $this->render('PackagistWebBundle:Web:search.html.twig', array('packages' => $paginator, 'searchForm' => $form->createView()));