Skip to content

Commit

Permalink
fix(endpoint): refactor handleError for promise use
Browse files Browse the repository at this point in the history
  • Loading branch information
kingcody committed Jun 23, 2015
1 parent 1bcffd6 commit 0af7c3e
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions endpoint/templates/name.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
var _ = require('lodash');
var <%= classedName %> = require('./<%= name %>.model');

function handleError(res, err) {
return res.status(500).send(err);
function handleError(res, statusCode) {
statusCode = statusCode || 500;
return function(err) {
res.status(statusCode).send(err);
};
}

function responseWithResult(res, statusCode) {
Expand All @@ -19,7 +22,7 @@ function responseWithResult(res, statusCode) {
function handleEntityNotFound(res) {
return function(entity) {
if (!entity) {
res.send(404);
res.status(404).end();
return null;
}
return entity;
Expand All @@ -41,7 +44,7 @@ function removeEntity(res) {
if (entity) {
return entity.removeAsync()
.then(function() {
return res.status(204);
res.status(204).end();
});
}
};
Expand All @@ -50,10 +53,9 @@ function removeEntity(res) {
// Get list of <%= name %>s
exports.index = function(req, res) {<% if (!filters.mongoose) { %>
res.json([]);<% } %><% if (filters.mongoose) { %>
<%= classedName %>.find(function(err, <%= name %>s) {
if (err) { return handleError(res, err); }
return res.status(200).json(<%= name %>s);
});<% } %>
<%= classedName %>.findAsync()
.then(responseWithResult(res))
.catch(handleError(res));<% } %>
};<% if (filters.mongoose) { %>

// Gets a single <%= name %> from the DB.
Expand Down

0 comments on commit 0af7c3e

Please sign in to comment.