Permalink
Browse files

improve error handling

  • Loading branch information...
1 parent bfacfa4 commit c6ed84edea0261eb95a4254268a17eec202bded1 @Trott committed Mar 22, 2013
Showing with 9 additions and 5 deletions.
  1. +5 −1 server.js
  2. +1 −1 static/UCSF.Person.js
  3. +2 −2 static/UCSF.Person.src.js
  4. +1 −1 static/ie7_polyfill.js
View
@@ -18,10 +18,14 @@ app.use(function (req, res, next) {
"use strict";
var apikeyMatches = false;
+
if(req.headers.origin && req.query.apikey) {
- //TODO: look up host in couchdb and only send the A-OK if it matches the origin header
db.get(req.query.apikey, function (err, doc) {
+ if (err) return next(err);
+ console.log(doc.host);
+ console.log(req.headers.origin);
if (doc.host === req.headers.origin) {
+ console.log('allowed');
res.header('Access-Control-Allow-Origin', req.headers.origin);
apikeyMatches = true;
@@ -1 +1 @@
-var UCSF=function(){"use strict";var e=[],t={serialize:function(e,t){var n=[];for(var s in e){var r=t?t+"["+s+"]":s,o=e[s];n.push("object"==typeof o?this.serialize(o,r):encodeURIComponent(r)+"="+encodeURIComponent(o))}return n.join("&")},createCORSRequest:function(e,t,n,s){var r=new XMLHttpRequest;return"withCredentials"in r?r.open(e,t,!0):"undefined"!=typeof XDomainRequest?(r=new XDomainRequest,r.open(e,t)):"undefined"!=typeof flensed&&"flXHR"in flensed?(r=new flensed.flXHR({xmlResponseText:!1,onreadystatechange:function(e){4===e.readyState&&(200===e.status&&n?n(JSON.parse(e.responseText)):s(e.statusText))},ontimeout:s,onerror:s}),r.open(e,t,!0)):r=null,r},createRequestString:function(e,t){var n=-1===e.indexOf("?")?"?":"&";return e+n+this.serialize(t)},Person:{search:function(t,n,s){s=s||function(e){window.alert(e)};var r=UCSF.createRequestString("http://apis.ucsf.edu/person/search",t),o=UCSF.createCORSRequest("GET",r,n,s);o?(o.onload=function(){n(JSON.parse(o.responseText))},o.onerror=s,o.send()):e.push({options:t,success:n,failure:s})}}};if(!t.createCORSRequest("GET","http://www.example.com/")){window.flensed={base_path:"http://apis.ucsf.edu/static/flensed/"};var n=document.createElement("script");n.src="http://apis.ucsf.edu/static/ie7_polyfill.js",n.onreadystatechange=function(){if("complete"===n.readyState||"loaded"===n.readyState)for(var t=e.length,s=0;t>s;s++)UCSF.Person.search(e[s].options,e[s].success,e[s].failure)},document.body.appendChild(n)}return t}();
+var UCSF=function(){"use strict";var e=[],t={serialize:function(e,t){var n=[];for(var r in e){var s=t?t+"["+r+"]":r,a=e[r];n.push("object"==typeof a?this.serialize(a,s):encodeURIComponent(s)+"="+encodeURIComponent(a))}return n.join("&")},createCORSRequest:function(e,t,n,r){var s=new XMLHttpRequest;return"withCredentials"in s?s.open(e,t,!0):"undefined"!=typeof XDomainRequest?(s=new XDomainRequest,s.open(e,t)):"undefined"!=typeof flensed&&"flXHR"in flensed?(s=new flensed.flXHR({xmlResponseText:!1,onreadystatechange:function(e){4===e.readyState&&(200===e.status&&n?n(JSON.parse(e.responseText)):r(e))},ontimeout:r,onerror:r}),s.open(e,t,!0)):s=null,s},createRequestString:function(e,t){var n=-1===e.indexOf("?")?"?":"&";return e+n+this.serialize(t)},Person:{search:function(t,n,r){r=r||function(e){window.alert(e.statusText||"An error occurred. Please try again.")};var s=UCSF.createRequestString("http://apis.ucsf.edu/person/search",t),a=UCSF.createCORSRequest("GET",s,n,r);a?(a.onload=function(){n(JSON.parse(a.responseText))},a.onerror=r,a.send()):e.push({options:t,success:n,failure:r})}}};if(!t.createCORSRequest("GET","http://www.example.com/")){window.flensed={base_path:"http://apis.ucsf.edu/static/flensed/"};var n=document.createElement("script");n.src="http://apis.ucsf.edu/static/ie7_polyfill.js",n.onreadystatechange=function(){if("complete"===n.readyState||"loaded"===n.readyState)for(var t=e.length,r=0;t>r;r++)UCSF.Person.search(e[r].options,e[r].success,e[r].failure)},document.body.appendChild(n)}return t}();
@@ -36,7 +36,7 @@ var UCSF = (function () {
if (XHRobj.status === 200 && success) {
success(JSON.parse(XHRobj.responseText));
} else {
- failure(XHRobj.statusText);
+ failure(XHRobj);
}
}
},
@@ -57,7 +57,7 @@ var UCSF = (function () {
Person: {
search: function (options, success, failure ) {
- failure = failure || function (msg) {window.alert(msg);};
+ failure = failure || function (obj) {window.alert(obj.statusText||'An error occurred. Please try again.');};
var reqString = UCSF.createRequestString('http://apis.ucsf.edu/person/search', options);
var xhr = UCSF.createCORSRequest('GET', reqString, success, failure);
if (! xhr) {

0 comments on commit c6ed84e

Please sign in to comment.