Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly handle return when in a route or middleware #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ module.exports = Backbone.Model.extend({

prepareResponse: function(modelOrCollection, req, res, next) {
if (!modelOrCollection) {
return next();
next();
return;
}

if (modelOrCollection instanceof Model) {
Expand All @@ -316,7 +317,7 @@ module.exports = Backbone.Model.extend({
res.data = modelOrCollection;
}

return next();
next();
},

/**
Expand Down Expand Up @@ -347,7 +348,7 @@ module.exports = Backbone.Model.extend({
res.data = envelope;
}

return next();
next();
},

/**
Expand Down Expand Up @@ -383,7 +384,7 @@ module.exports = Backbone.Model.extend({
res.code = err.code;
res.data = envelope;

return next();
next();
},

/**
Expand Down
29 changes: 13 additions & 16 deletions crud_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@ module.exports = Controller.extend({
options = options || {};
_.merge(options, this.parseQueryString(req));

return collection.count(options).then(function(total) {
collection.count(options).then(function(total) {
res.data = {
total: total
};
return next();
next();
}).catch(next);
},

Expand All @@ -173,7 +173,7 @@ module.exports = Controller.extend({
options.fields = fields;
}

return collection.fetch(options).bind(this).tap(function() {
collection.fetch(options).bind(this).tap(function() {
res.paging = {
total: collection.total,
count: collection.count,
Expand All @@ -188,8 +188,7 @@ module.exports = Controller.extend({

// Optionally restrict fields for response
res.data = this._restrictFields(req.query.fields, res.data);

return next();
next();
}).catch(next);
},

Expand All @@ -206,25 +205,23 @@ module.exports = Controller.extend({
options.fields = fields;
}

return model.fetch(options).bind(this).then(function(model) {
model.fetch(options).bind(this).then(function(model) {
res.data = model.render();

// Optionally restrict fields for response
res.data = this._restrictFields(req.query.fields, res.data);

return next();
next();
}).catch(next);
},

create: function(req, res, next) {
var model = this.setupModel(req);

return model.setFromRequest(req.body).then(function() {
model.setFromRequest(req.body).then(function() {
return model.save();
}).then(function(model) {
res.data = model.render();

return next();
next();
}).catch(next);
},

Expand All @@ -236,14 +233,13 @@ module.exports = Controller.extend({
require: true
});

return model.fetch(options).then(function() {
model.fetch(options).then(function() {
return model.setFromRequest(req.body);
}).then(function() {
return model.save(null, options);
}).then(function(model) {
res.data = model.render();

return next();
next();
}).catch(next);
},

Expand All @@ -252,11 +248,12 @@ module.exports = Controller.extend({

return model.destroy().then(function(resp) {
if (resp === 0) {
return next(new MuniError('Document not found.', 404));
next(new MuniError('Document not found.', 404));
return;
}

res.code = 204;
return next();
next();
}).catch(next);
}
});
5 changes: 3 additions & 2 deletions limiter.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ function middleware(req, res, next) {
});

if (!bypass && options.reject && client.used >= client.limit) {
return rejected(req, res, next);
rejected(req, res, next);
return;
}

client.used++;
return next();
next();
}

function rejected(req, res, next) {
Expand Down
5 changes: 3 additions & 2 deletions router.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,13 @@ module.exports = function(options) {
// respond with an error before routing
var missingParams = router._buildMissingParams(req, requiredParams);
if (missingParams.length) {
return next(router._buildMissingParamsError(missingParams));
next(router._buildMissingParamsError(missingParams));
return;
}
}

// Execute the route for the request
return routeOptions.action.call(controller, req, res, next);
routeOptions.action.call(controller, req, res, next);
};
},

Expand Down