- Timestamp:
- 03/23/14 17:26:12 (11 years ago)
- Location:
- Dev/trunk/src
- Files:
-
- 1 deleted
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
Dev/trunk/src/package.json
r525 r527 8 8 "passport": "~0.1.18", 9 9 "passport-local": "~0.1.6", 10 "request": "~2.21.0",11 10 "ya-csv": "~0.9.3", 12 11 "tv4": "~1.0.16" -
Dev/trunk/src/server/api/categories.js
r525 r527 15 15 return couch.get(url,{query:{reduce:true,group:true,group_level:1}}) 16 16 .handle({ 17 '-1': _.identity, 17 18 200: function(result) { 18 19 return _.map(result.rows, function(item) { … … 29 30 var sub = 'published' in req.query && 'published'; 30 31 getCategories(sub) 32 .handle({'-1': util.handleException}) 31 33 .handle(res.send.bind(res)); 32 34 }); … … 37 39 var sub = 'published' in req.query && 'published'; 38 40 topics.getTopicsWithCategory(category,sub) 41 .handle({'-1': util.handleException}) 39 42 .handle(res.send.bind(res)); 40 43 }); -
Dev/trunk/src/server/api/open.js
r525 r527 44 44 } 45 45 }) 46 .handle({'-1': util.handleException}) 46 47 .handle(res.send.bind(res)); 47 48 }); … … 74 75 } 75 76 }) 77 .handle({'-1': util.handleException}) 76 78 .handle(res.send.bind(res)); 77 79 }); … … 112 114 } 113 115 }) 116 .handle({'-1': util.handleException}) 114 117 .handle(res.send.bind(res)); 115 118 }); … … 141 144 } 142 145 }) 146 .handle({'-1': util.handleException}) 143 147 .handle(res.send.bind(res)); 144 148 }); -
Dev/trunk/src/server/api/questions.js
r525 r527 17 17 return couch.get(url,{query:query}) 18 18 .handle({ 19 '-1': _.identity, 19 20 200: util.handleRowDocs, 20 21 default: util.handleUnknownResponse … … 26 27 return couch.get(url,{query:query}) 27 28 .handle({ 29 '-1': _.identity, 28 30 200: util.handleRowDocs, 29 31 default: util.handleUnknownResponse … … 35 37 return couch.get(url,{query:query}) 36 38 .handle({ 39 '-1': _.identity, 37 40 200: util.handleRowDictOfValues, 38 41 default: util.handleUnknownResponse … … 44 47 return couch.get(url,{query:query}) 45 48 .handle({ 49 '-1': _.identity, 46 50 200: util.handleRowDocs, 47 51 default: util.handleUnknownResponse … … 56 60 return couch.get(url,{query:query}) 57 61 .handle({ 62 '-1': _.identity, 58 63 200: util.handleRowDocs, 59 64 default: util.handleUnknownResponse … … 67 72 return couch.get(url) 68 73 .handle({ 74 '-1': _.identity, 69 75 200: util.handleRowDictOfValues, 70 76 default: util.handleUnknownResponse … … 93 99 hr = getQuestions(sub); 94 100 } 95 hr.handle(res.send.bind(res)); 101 hr.handle({'-1': util.handleException}) 102 .handle(res.send.bind(res)); 96 103 }); 97 104 app.post('/', … … 122 129 }); 123 130 } 124 hr.handle(res.send.bind(res)); 131 hr.handle({'-1': util.handleException}) 132 .handle(res.send.bind(res)); 125 133 }); 126 134 app.get('/:id', … … 160 168 }); 161 169 } 162 hr.handle(res.send.bind(res)); 170 hr.handle({'-1': util.handleException}) 171 .handle(res.send.bind(res)); 163 172 }); 164 173 app.delete('/:id', -
Dev/trunk/src/server/api/responses.js
r525 r527 16 16 return couch.get(url) 17 17 .handle({ 18 '-1': _.identity, 18 19 200: util.handleRowValues, 19 20 default: util.handleUnknownResponse … … 30 31 hr = util.getDocumentsOfType('Response'); 31 32 } 32 hr.handle(res.send.bind(res)); 33 hr.handle({'-1': util.handleException}) 34 .handle(res.send.bind(res)); 33 35 }); 34 36 app.get('/:id', … … 44 46 return util.postDocument('Response',doc); 45 47 }) 48 .handle({'-1': util.handleException}) 46 49 .handle(res.send.bind(res)); 47 50 }); … … 64 67 hr = util.putDocument(id,rev,'Response',doc); 65 68 } 66 hr.handle(res.send.bind(res)); 69 hr.handle({'-1': util.handleException}) 70 .handle(res.send.bind(res)); 67 71 }); 68 72 app.delete('/:id', -
Dev/trunk/src/server/api/surveyRuns.js
r525 r527 30 30 }); 31 31 } 32 hr.handle(res.send.bind(res)); 32 hr.handle({'-1': util.handleException}) 33 .handle(res.send.bind(res)); 33 34 }); 34 35 app.get('/:id', … … 53 54 hr = util.putDocument(id,rev,'SurveyRun',doc); 54 55 } 55 hr.handle(res.send.bind(res)); 56 hr.handle({'-1': util.handleException}) 57 .handle(res.send.bind(res)); 56 58 }); 57 59 app.delete('/:id', … … 70 72 } 71 73 } 72 }).handle(res.send.bind(res)); 74 }) 75 .handle({'-1': util.handleException}) 76 .handle(res.send.bind(res)); 73 77 }); 74 78 app.get('/:id/responses', … … 77 81 var id = req.params.id; 78 82 responses.getResponsesBySurveyRunId(id) 83 .handle({'-1': util.handleException}) 79 84 .handle(res.send.bind(res)); 80 85 }); … … 85 90 responses.getResponsesBySurveyRunId(id) 86 91 .handle({ 92 '-1': function(result) { 93 util.handleException(result) 94 .handle(res.send.bind(res)); 95 }, 87 96 200: function(responses) { 88 97 var answers = _.map(responses,function(response){ -
Dev/trunk/src/server/api/surveys.js
r525 r527 23 23 couch.get(url) 24 24 .handle({ 25 '-1': _.identity, 25 26 200: function(result) { 26 27 return _.map(result.rows, function(item) { … … 30 31 default: util.handleUnknownResponse 31 32 }) 33 .handle({'-1': util.handleException}) 32 34 .handle(res.send.bind(res)); 33 35 }); … … 45 47 hr = util.postDocument('Survey',doc); 46 48 } 47 hr.handle(res.send.bind(res)); 49 hr.handle({'-1': util.handleException}) 50 .handle(res.send.bind(res)); 48 51 }); 49 52 app.get('/:id', … … 66 69 util.putDocument(id,rev,'Survey',doc) 67 70 .handle({ 71 '-1': function(result) { 72 util.handleException(result) 73 .handle(res.send.bind(res)); 74 }, 68 75 201: function(doc) { 69 76 res.set({ -
Dev/trunk/src/server/api/topics.js
r525 r527 14 14 return couch.get(url,{query:{reduce:true,group:true}}) 15 15 .handle({ 16 '-1': _.identity, 16 17 200: function(result) { 17 18 return _.map(result.rows, function(item) { return {name:item.key, count:item.value}; }); … … 24 25 return couch.get(url,{query:{reduce:true,group:true,group_level:2,startkey:[category],endkey:[category,{}]}}) 25 26 .handle({ 27 '-1': _.identity, 26 28 200: function(result) { 27 29 return _.map(result.rows, function(item) { return { name:item.key[1], count:item.value }; }); … … 41 43 hr = getTopics(sub); 42 44 } 43 hr.handle(res.send.bind(res)); 45 hr.handle({'-1': util.handleException}) 46 .handle(res.send.bind(res)); 44 47 }); 45 48 -
Dev/trunk/src/server/api/util.js
r525 r527 34 34 }; 35 35 36 var handleException = exports.handleException = function(error) { 37 return new HTTPResult(500,{ 38 reason: error.message, 39 filename: error.filename, 40 lineNumber: error.lineNumber 41 }); 42 }; 36 43 var handleUnknownResponse = exports.handleUnknownResponse = function(status,error) { 37 44 return new HTTPResult(500,{error: error.reason}); … … 77 84 return couch.get(url) 78 85 .handle({ 86 '-1': _.identity, 79 87 200: handleRowValues, 80 88 404: function() { return {error: "Cannot find collection of type "+type}; }, … … 89 97 return couch.get(id,opts) 90 98 .handle({ 99 '-1': _.identity, 91 100 200: function(doc){ 92 101 if ( doc.type !== type ) { … … 114 123 return couch.put(id,doc,opts) 115 124 .handle({ 125 '-1': _.identity, 116 126 201: function(res){ 117 127 doc._id = res.id; … … 133 143 return couch.delete(id,opts) 134 144 .handle({ 145 '-1': _.identity, 135 146 200: identity, 136 147 409: function(error) { … … 149 160 return couch.post(doc) 150 161 .handle({ 162 '-1': _.identity, 151 163 201: function(response) { 152 164 doc._id = response.id; … … 164 176 return function(req,res) { 165 177 getDocumentsOfType(type) 178 .handle({'-1': handleException}) 166 179 .handle(res.send.bind(res)); 167 180 }; … … 173 186 getDocument(id,rev,type) 174 187 .handle({ 188 '-1': function(result) { 189 handleException(result) 190 .handle(res.send.bind(res)); 191 }, 175 192 200: function(doc){ 176 193 res.set({ … … 189 206 putDocument(id,rev,type,doc) 190 207 .handle({ 208 '-1': function(result) { 209 handleException(result) 210 .handle(res.send.bind(res)); 211 }, 191 212 201: function(doc) { 192 213 res.set({ … … 204 225 var rev = etags.parse(req.header('If-Match'))[0] || (doc && doc._rev); 205 226 deleteDocument(id,rev) 227 .handle({'-1': handleException}) 206 228 .handle(res.send.bind(res)); 207 229 }; … … 212 234 postDocument(type,doc) 213 235 .handle({ 236 '-1': function(result) { 237 handleException(result) 238 .handle(res.send.bind(res)); 239 }, 214 240 201: function(doc) { 215 241 res.set({ -
Dev/trunk/src/server/util/couch.js
r525 r527 1 1 var request = require('./request') 2 2 , _ = require('underscore') 3 , HTTPResult = require('./http-result') 3 4 ; 4 5 … … 42 43 } 43 44 44 function couchRequest (method, url, body, opts) { 45 function couchRequest (method, url, body, opts, retries) { 46 body = body || {}; 47 retries = _.isNumber(retries) ? retries : 3; 45 48 var options = { 46 49 method: method, 47 headers: { 48 'Content-Type': 'application/json; charset=utf-8', 49 'Accept': 'application/json' 50 }, 51 body: JSON.stringify(stringifyFunctions(body || {})) 50 headers: {}, 51 body: stringifyFunctions(body), 52 json: true, 53 timeout: 5000 52 54 }; 53 55 if (opts) { … … 56 58 } 57 59 return request(url, options) 58 .handle({ 59 '-1': _.identity, 60 default: function(status,result) { return JSON.parse(result); } 61 }); 60 .handle(function(status,body){ 61 if ( _.isUndefined(body) ) { 62 if ( retries > 0) { 63 return couchRequest(method,url,body,opts,retries-1); 64 } else { 65 return new HTTPResult(-1,new Error("No response after several retries.")); 66 } 67 } else { 68 return body; 69 } 70 }); 62 71 } 63 72 -
Dev/trunk/src/server/util/http-result.js
r525 r527 39 39 // _fire :: Handler -> () 40 40 HTTPResult.prototype._fire = function(handler) { 41 var ret = handler.f(this.status,this.result); 42 if ( ret instanceof HTTPResult ) { 43 ret.handle(handler.next.set.bind(handler.next)); 44 } else { 45 handler.next.set(this.status,ret); 41 try { 42 var ret = handler.f(this.status,this.result); 43 if ( ret instanceof HTTPResult ) { 44 ret.handle(handler.next.set.bind(handler.next)); 45 } else { 46 handler.next.set(this.status,ret); 47 } 48 } catch(ex) { 49 console.log('HTTPResult','exception',ex.toString(),ex.stack); 50 handler.next.set(-1,ex); 46 51 } 47 52 }; -
Dev/trunk/src/server/util/request.js
r525 r527 1 1 var HTTPResult = require('./http-result') 2 , request = require('request') 3 , url = require('url') 2 , HTTP = require('http') 3 , HTTPS = require('https') 4 , URL = require('url') 4 5 , _ = require('underscore') 5 6 ; … … 9 10 var result = new HTTPResult(); 10 11 11 options = options ? _.clone(options) : {}; 12 options.uri = url || options.uri; 13 14 request(options,function(err,res,body){ 15 if ( err ) { 16 result.set(-1,err); 17 } else { 18 result.set(res.statusCode,body); 19 } 12 var body = options.body; 13 delete options.body; 14 15 var json = options.json; 16 delete options.json; 17 18 var reqOpts = _.isString(url) ? URL.parse(url) : url; 19 _.extend(reqOpts,options); 20 if ( json === true ) { 21 options.headers = options.headers || {}; 22 _.extend(options.headers,{ 23 'Content-Type': 'application/json', 24 'Accepts': 'application/json' 25 }); 26 } 27 28 var protocol = reqOpts.protocol; 29 var httpModule; 30 if ( protocol === 'http:' ) { 31 httpModule = HTTP; 32 } else if ( protocol === 'https:' ) { 33 httpModule = HTTPS; 34 } else { 35 throw new Error("Unknown protocol "+protocol); 36 } 37 38 var req = httpModule.request(reqOpts); 39 req.on('response', function(response) { 40 var data = ""; 41 response.setEncoding('utf8'); 42 response.on('data', function(chunk) { 43 data += chunk; 44 }); 45 response.on('end', function() { 46 var body; 47 try { 48 if ( json ) { 49 if ( data ) { body = JSON.parse(data); } 50 } else { 51 body = data; 52 } 53 result.set(response.statusCode,body); 54 } catch (ex) { 55 console.log('error',data); 56 result.set(-1,ex); 57 } 58 }); 59 response.on('error', function(error) { 60 result.set(-1,error); 61 }); 20 62 }); 63 req.on('error', function(error){ 64 result.set(-1,error); 65 }); 66 67 if ( body ) { 68 req.write(json ? JSON.stringify(body) : body); 69 } 70 req.end(); 21 71 22 72 return result;
Note: See TracChangeset
for help on using the changeset viewer.