First step of error management when Wikipedia request fails. Set up in whole list and document list.
authorcavaliet
Wed, 30 Nov 2011 17:58:18 +0100
changeset 50 e967654e90cb
parent 49 e854d8cb376c
child 51 67c323b2f4d1
First step of error management when Wikipedia request fails. Set up in whole list and document list.
Controller/WikiTagController.php
Entity/TagRepository.php
Utils/WikiTagUtils.php
--- a/Controller/WikiTagController.php	Mon Nov 28 15:58:34 2011 +0100
+++ b/Controller/WikiTagController.php	Wed Nov 30 17:58:18 2011 +0100
@@ -193,7 +193,12 @@
                 return new Response(json_encode(array('error' => 'duplicate_tag', 'message' => sprintf("Le tag %s existe déjà pour cette fiche.", $tag_label))),400);
             }
             // We create the new tag or get the already existing tag. $tag, $revision_id, $created
-            $ar = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->getOrCreateTag($tag_label);
+            try {
+                $ar = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->getOrCreateTag($tag_label);
+            }
+            catch (\Exception $e){
+                return new Response(json_encode(array('error' => 'wikipedia_request_failed', 'message' => $e->getMessage())),400);
+            }
             $tag = $ar[0];
             $revision_id = $ar[1];
             $created = $ar[2];
@@ -275,7 +280,13 @@
             return new Response(json_encode(array('error' => 'duplicate_tag', 'message' => sprintf("Le tag %s existe déjà pour cette fiche.", $tag_label))),400);
         }
         // returns array($tag, $revision_id, $created)
-        $ar = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->getOrCreateTag($tag_label);// tag, revision_id, created = get_or_create_tag(tag_label)
+        try {
+            $ar = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->getOrCreateTag($tag_label);
+        }
+        catch (\Exception $e){
+            return new Response(json_encode(array('error' => 'wikipedia_request_failed', 'message' => $e->getMessage())),400);
+        }
+        
         $tag = $ar[0];
         $revision_id = $ar[1];
         $created = $ar[2];
@@ -524,8 +535,12 @@
         $id_moved_tag = $this->getRequest()->request->get('id');
         $moved_tag = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findOneBy(array('id' => $id_moved_tag));
         // We update the tag label and its wikipedia info with the new label.
-        $this->updateTagWithNewLabel($moved_tag, $tag_label);
-        
+        try {
+            $this->updateTagWithNewLabel($moved_tag, $tag_label);
+        }
+        catch (\Exception $e){
+            return new Response(json_encode(array('error' => 'wikipedia_request_failed', 'message' => $e->getMessage())),400);
+        }
         // We render the tag list.
         $num_page = $this->getRequest()->request->get('num_page');
         $nb_by_page = $this->getRequest()->request->get('nb_by_page');
@@ -544,7 +559,12 @@
         $id_moved_tag = $this->getRequest()->request->get('tag_id');
         $moved_tag = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findOneBy(array('id' => $id_moved_tag));
         // We update the tag label and its wikipedia info with the original label.
-        return $this->updateTagWithNewLabel($moved_tag, $moved_tag->getOriginalLabel());
+        try {
+            $this->updateTagWithNewLabel($moved_tag, $moved_tag->getOriginalLabel());
+        }
+        catch (\Exception $e){
+            return new Response(json_encode(array('error' => 'wikipedia_request_failed', 'message' => $e->getMessage())),400);
+        }
         
         // We render the tag list.
         $num_page = $this->getRequest()->request->get('num_page');
@@ -564,7 +584,12 @@
             if($label!=$tag->getLabel()){
                 // We get the Wikipedia informations for the sent label
                 $tag_label_normalized = WikiTagUtils::normalizeTag($label);
-                $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized);
+                try {
+                    $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized);
+                }
+                catch (\Exception $e){
+                    throw new \Exception($e->getMessage());
+                }
                 $tag->setWikipediaInfo($wp_response);
                 // Save datas.
                 $em = $this->getDoctrine()->getEntityManager();
--- a/Entity/TagRepository.php	Mon Nov 28 15:58:34 2011 +0100
+++ b/Entity/TagRepository.php	Wed Nov 30 17:58:18 2011 +0100
@@ -86,7 +86,12 @@
                 }
             }
             if($match_exists==false) {
-                $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized);
+                try {
+                    $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized);
+                }
+                catch (\Exception $e){
+                    throw new \Exception($e->getMessage());
+                }
                 $status = $wp_response['status'];
                 if($status==Tag::$TAG_URL_STATUS_DICT['match']) {
                     $tag = new Tag();
@@ -103,7 +108,12 @@
         if($created==true) {
             
             if($wp_request_done==false) {
-                $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized);
+                try {
+                    $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized);
+                }
+                catch (\Exception $e){
+                    throw new \Exception($e->getMessage());
+                }
             }
             
             $tag->setWikipediaInfo($wp_response);
@@ -117,8 +127,12 @@
     
         }
         else if($tag!=null && $tag->getWikipediaPageId()!=null) {
-            
-            $wp_response = WikiTagUtils::getWikipediaInfo(null, $tag->getWikipediaPageId());
+            try {
+                $wp_response = WikiTagUtils::getWikipediaInfo(null, $tag->getWikipediaPageId());
+            }
+            catch (\Exception $e){
+                throw new \Exception($e->getMessage());
+            }
             $wikipedia_revision_id = $wp_response['revision_id'];
         }
         else {
--- a/Utils/WikiTagUtils.php	Mon Nov 28 15:58:34 2011 +0100
+++ b/Utils/WikiTagUtils.php	Wed Nov 30 17:58:18 2011 +0100
@@ -60,7 +60,12 @@
             return WikiTagUtils::returnNullResult(null);
         }
         
-        $ar = WikiTagUtils::requestWikipedia($params);
+        try {
+            $ar = WikiTagUtils::requestWikipedia($params);
+        }
+        catch (\Exception $e){
+            throw new \Exception($e->getMessage());
+        }
         $res = $ar[0];
         $original_response = $res;
         $pages = $ar[1];
@@ -172,9 +177,16 @@
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_USERAGENT, 'http://www.iri.centrepompidou.fr');
+        curl_setopt($ch, CURLOPT_TIMEOUT_MS, 5000);
         $res = curl_exec($ch);
+        $curl_errno = curl_errno($ch);
+        $curl_error = curl_error($ch);
         curl_close($ch);
         
+        if ($curl_errno > 0) {
+            throw new \Exception("Wikipedia request failed. cURLError #$curl_errno: $curl_error\n");
+        }
+        
         $val = json_decode($res, true);
         $pages = $val["query"]["pages"];
         return array($res, $pages);