Mvolz has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/141258

Change subject: Added error handling to ZoteroRequest
......................................................................

Added error handling to ZoteroRequest

Zotero Request now takes response and error in addition
to body.

Added error handling to two common cases:

Zotero server not running, and
No Zotero translators available.

Change-Id: I2845e6a99203d00bd2e2d20166e037228655099c
---
M server.js
M zotero.js
2 files changed, 47 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/citoid 
refs/changes/58/141258/1

diff --git a/server.js b/server.js
index 9e6ec53..5385cad 100644
--- a/server.js
+++ b/server.js
@@ -11,7 +11,11 @@
 
 var port = '1970';
 
-var zoteroURL = 'http://localhost:1969/web'; //assumes zotero already started
+var zoteroURL = 'http://localhost:1969/web'; 
+
+//Value of WorldCat API key. 
+//If false, doesn't use any WorldCat functions
+var wskey = false; 
 
 /*testing variables*/
 var testSessionID = "abc123";
@@ -35,11 +39,29 @@
        //Retrieve query params from request
        var requestedURL = req.body.url;
 
+       res.type('application/json');
+
        //Request from Zotero and set response
-       zoteroRequest(requestedURL, testSessionID, function(body){
-               res.type('application/json');
-               res.json(body);
-               
+       zoteroRequest(requestedURL, testSessionID, function(error, response, 
body){
+
+               if (response) {
+                       if (!error && response.statusCode == 200) {
+                               res.json(body);
+                       }
+                       //501 response indicates Zotero doesn't have a 
translator available
+                       //in this case, send url to a generic scraper
+                       else if(response.statusCode == 501){
+                               console.log(body);
+                               res.json(body);
+                               //res.json(naiveScrape(testURL)); //not 
implemented yet
+                       }
+               }
+               else {
+                       //no response, probably means zotero service is not 
running
+                       var message = "Server at "+zoteroURL+" does not appear 
to be running.";
+                       res.json(message);
+                       console.log(message);
+               }
        });
 });
 
diff --git a/zotero.js b/zotero.js
index e971f9a..9fecb5a 100644
--- a/zotero.js
+++ b/zotero.js
@@ -6,6 +6,8 @@
  */
 
 var request = require('request');
+var Promise = require('bluebird');
+var async = require('async');
 
 var zoteroURL = 'http://localhost:1969/web'; //assumes zotero already started
 
@@ -21,8 +23,13 @@
 
        request(options, function (error, response, body) {
                if (!error && response.statusCode == 200) {
-                       callback(modifyBody(body));
+                       //modify body if response is okay
+                       callback(error, response, modifyBody(body));
                }
+               else {
+                       callback(error, response, body);
+               }
+               
        });
 };
 
@@ -47,18 +54,25 @@
        }
 
        delete body[0].creators; //remove creators field
-       //body[0] += newCreators; //append new creator fields to end of body
-       //console.log(body);
        return body;
 }
 
 /*Test server fcns*/
 var testServer = function(){
-    testURL = "http://www.tandfonline.com/doi/abs/10.1080/15424060903167229";
+    //testURL = "http://www.tandfonline.com/doi/abs/10.1080/15424060903167229"; 
//URL that works with Zotero
+       testURL = 
"http://books.google.co.uk/books?hl=en&lr=&id=7lueAgAAQBAJ&oi=fnd&pg=PR5&dq=mediawiki&ots=-Z0o2LCgao&sig=IGHnyWEiNiNvPyXeyCuOcdvi15s#v=onepage&q=mediawiki&f=false";
 //url that doesn't work with zotero
        testSessionID = "abc123"
 
-       zoteroRequest(testURL, testSessionID, function(body){
-               console.log(modifyBody(body));
+       zoteroRequest(testURL, testSessionID, function(error, response, body){
+               if (response) {
+                       if (!error && response.statusCode == 200) {
+                               console.log(body);
+                       }
+                       else if(response.statusCode == 501){
+                               console.log(body);
+                       }
+               }
+               else {console.log("Server at "+zoteroURL+" does not appear to 
be running.")}
        });
 }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/141258
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2845e6a99203d00bd2e2d20166e037228655099c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/citoid
Gerrit-Branch: master
Gerrit-Owner: Mvolz <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to