}, function (error) { return when.reject(new Error('There is no user by that email address. Check again.')); });
}, function (error) { return when.reject(new errors.InternalServerError(error)); });
saveFlow: function(fn, data) { if (is_malicious(fn)) { return when.reject(new Error('forbidden flow name')); } return storageModule.saveFlow(fn, data); },
beforeEach(() => { res = when.reject({ status: 500 }) return fetchThings(fooId, barId) })
teamAdd: (name) => { if (typeof name !== 'string') { return when.reject(new Error('invalid arguments')); } return Spark.request('post', 'teams', { name }); },
}, function (err) { return when.reject("Error importing data: " + err.message || err, err.stack); });
}, function (error) { return when.reject({errorCode: 500, message: error.message || error}); });
var uninstallModule = sinon.stub(redNodes,'uninstallModule', function() { return when.reject(new Error("test error")); });
}).then(function (user) { // Check if user exists if (user) { return when.reject(new Error('A user is already registered. Only one user for now!')); } }).then(function () {
.otherwise(function (error) { return when.reject(error); });
var installModule = sinon.stub(redNodes,'installModule', function() { var err = new Error("test error"); err.code = 404; return when.reject(err); });
}, function (error) { return when.reject(error); });
function findAncestor( client, db, table, type, modelId ) { return when.reject( new Error( "Not supported" ) ); }
monad.createChannel = function() { return when.reject( ":( no can do" ); };
Spark.prototype.request = function(method, resource, id, query, maxResults) { if(!this.token) { return when.reject(new Error('token not defined')); } // parse args var args = Array.prototype.slice.call(arguments); method = args.shift(); resource = args.shift(); var url = this.apiUrl + resource; // add id to url if specified if(args.length > 0 && typeof args[0] === 'string') { url = url + '/' + args.shift(); } // get query if specified if(args.length > 0 && typeof args[0] === 'object') { query = args.shift(); } // get limit for items retrieved and convert string values to number if(args.length > 0 && typeof args[0] !== 'undefined' && typeof parseInt(args[0],10) === 'number') { maxResults = parseInt(args.shift(),10); } // headers var headers = { 'Authorization': 'Bearer ' + this.token, 'Content-Type': 'application/json' }; // default request options var requestOptions = { url: url, headers: headers, method: method, timeout: this.requestTimeout, gzip: true, time: true, json: true }; // update options for contents resource if(resource === 'contents') { requestOptions.encoding = 'binary'; requestOptions.json = false; } // update options for query if(query) { if(method === 'post' || method === 'put') { // do body query requestOptions.body = query; } else { // do query string requestOptions.qs = query; } } // perform HTTP request and structure response function request(options) { return when.promise((resolve, reject) => { req(options, (err, res, body) => { if(err) { reject(err); } else { var response = { headers: res.headers || {}, statusCode: res.statusCode || 0, code: res.statusCode || 0, body: body || res.body || {}, ok: (res.statusCode >= 200 && res.statusCode < 300) }; resolve(response); } }); }); } var errorCount = 0; var requestQueue = options => { /** * Spark request event. * * @event request * @type {object} * @property {options} request - API Request * @property {string} id - Spark UUID */ this.emit('request', options, this.id); return when(this.queue.schedule(request, options)) .then(response => { /** * Spark response event. * * @event reponse * @type {object} * @property {options} response - Response * @property {string} id - Spark UUID */ this.emit('response', response, this.id); if(_.includes(this.requeueCodes, response.statusCode)) { return when(true).delay(this.requeueMinTime).then(() => requestRequeue(options)); } else { return when(response); } }); }; var requestRequeue = options => { errorCount++; /** * Spark retry event. * * @event retry * @type {object} * @property {options} request - API Request * @property {string} id - Spark UUID */ this.emit('retry', options, this.id); debug('Retry attempt #%s for (%s) %s', errorCount, options.method, options.url); return when(this.requeue.schedule(request, options)) .then(response => { this.emit('response', response, this.id); if(errorCount >= this.requeueMaxRetry) { return when(response); } if(_.includes(this.requeueCodes, response.statusCode)) { return when(true).delay(this.requeueMinTime).then(() => requestRequeue(options)); } // return response else { return when(response); } }); }; // recursive pagination var requestPaginate = (items, link) => { // limit max results if(typeof maxResults === 'number' && items.length > maxResults) { return when(items); } var pageRequestOptions = _.clone(requestOptions); pageRequestOptions.url = link.match(/(http.*)>/)[1]; // request next page return requestQueue(pageRequestOptions) .then(response => { // response 2XX if(response.ok) { // if more items returned... if(response.body && response.body.items) { // concat next page items with current items items = _.concat(items, response.body.items); // if pagination link found in next page response, get next page if(response.headers['link']) { return requestPaginate(items, response.headers['link']); } // no more pages, return items else { return when(items); } } } // response not ok, return items retrieved else { return when(items); } }); }; return requestQueue(requestOptions) .then(response => { // response 2XX if(response.ok) { // if pagination link found in response if(response.body && response.body.items && response.headers['link']) { return requestPaginate(response.body.items, response.headers['link']) .then(allItems => { if(typeof maxResults === 'number' && allItems.length > 0) { allItems = allItems.slice(0, maxResults); } response.body.items = allItems; return when(response); }); } // return response else { return when(response); } } // everything else else { var errorMessage = util.format('received response code %s for (%s) %s body:%j qs:%j', response.statusCode, requestOptions.method, requestOptions.url, requestOptions.body || {}, requestOptions.qs || {}); debug(errorMessage); return when.reject(new Error(errorMessage)); } }); };
return when(self.setWarning(user)).then(function (remaining) { s = (remaining > 1) ? 's' : ''; return when.reject(new Error('Your password is incorrect.<br>' + remaining + ' attempt' + s + ' remaining!')); });
Spark.prototype.messageStreamRoom = function(roomId, message) { if(typeof roomId !== 'string') { return when.reject(new Error('roomId invalid')); } var requestOptions = { method: 'post', url: this.apiUrl + 'messages', headers: { 'Authorization': 'Bearer ' + this.token }, formData: {'roomId': roomId } }; // if message is object... if(typeof message === 'object') { // if message.text present if(typeof message.text === 'string') requestOptions.formData.text = message.text; // if message.stream found and message.filename present if(typeof message.stream !== 'undefined' && typeof message.filename === 'string') { // regex for matching file extention var re = /\.([0-9a-z]{1,5})$/i; // if file extension found... if(re.test(message.filename) && message.filename.match(re).length > 1) { var ext = message.filename.match()[1]; requestOptions.formData.files = { value: message.stream, options: { filename: message.filename, contentType: mime.lookup(ext) } }; } // else, file extension missing else { return when.reject(new Error('message.filename missing a file extension')); } } // if message.stream found and message.filename NOT present else if(typeof message.stream !== 'undefined' && typeof message.filename === 'undefined') { requestOptions.formData.files = message.stream; } // else, message object invalid else { return when.reject(new Error('missing message object properties')); } } else { return when.reject(new Error('message is not object')); } return when.promise((resolve, reject) => { req(requestOptions, (err, response, body) => { if(err) { reject(err); } else { if(response.statusCode == 200) { resolve(response); } else { reject(util.format('received response code %s for (%s) %s body:%j qs:%j', response.statusCode, requestOptions.method, requestOptions.url, requestOptions.body || {}, requestOptions.qs || {})); } } }); }).then(res => this.toObject(res)); };
}).then(function (matched) { if (!matched) { return when.reject(new Error('Your password is incorrect')); } return nodefn.call(bcrypt.genSalt); }).then(function (salt) {
}, function (reason) { return when.reject(reason); });
rejectError: function (err) { return when.reject(err); },
}).otherwise(function (error) { return when.reject(new errors.UnauthorizedError(error.message)); });
function onError( err ) { console.error( util.format( 'Requesting a version list for "%s/%s" from GitHub failed with %s', owner, repo, err ) ); return when.reject( new Error( 'Invalid repository' ) ); }
teamGet: (teamId) => { if (typeof teamId !== 'string') { return when.reject(new Error('invalid arguments')); } return Spark.request('get', 'teams', teamId); },
function checkUserData(userData) { if (_.isEmpty(userData) || _.isEmpty(userData.users) || _.isEmpty(userData.users[0])) { return when.reject({code: 400, message: 'No root key (\'users\') provided.'}); } return when.resolve(userData); }
permissableStub = sandbox.stub(Models.Post, 'permissable', function () { return when.reject(); });
}, function () { return when.reject({type: 'NotFound', message: 'You do not have permission to browse users.'}); });
}).catch(function (error) { return when.reject(new errors.ValidationError(error.message)); });
}, function () { return when.reject({type: 'NoPermission', message: 'You do not have permission to edit this users.'}); });
saveLibraryEntry: function(type, path, meta, body) { if (is_malicious(path)) { return when.reject(new Error('forbidden flow name')); } return storageModule.saveLibraryEntry(type, path, meta, body); }
}).then(function (matched) { if (!matched) { return when.reject(new Error('Your password is incorrect')); } return nodefn.call(bcrypt.hash, newPassword, null, null); }).then(function (hash) {