Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
Adding Password Reset
Browse files Browse the repository at this point in the history
  • Loading branch information
amoshaviv committed Jul 31, 2014
1 parent 9fb6e41 commit f4b62ca
Show file tree
Hide file tree
Showing 33 changed files with 853 additions and 706 deletions.
33 changes: 5 additions & 28 deletions app/controllers/articles.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,10 @@
* Module dependencies.
*/
var mongoose = require('mongoose'),
errorHandler = require('./errors'),
Article = mongoose.model('Article'),
_ = require('lodash');

/**
* Get the error message from error object
*/
var getErrorMessage = function(err) {
var message = '';

if (err.code) {
switch (err.code) {
case 11000:
case 11001:
message = 'Article already exists';
break;
default:
message = 'Something went wrong';
}
} else {
for (var errName in err.errors) {
if (err.errors[errName].message) message = err.errors[errName].message;
}
}

return message;
};

/**
* Create a article
*/
Expand All @@ -41,7 +18,7 @@ exports.create = function(req, res) {
article.save(function(err) {
if (err) {
return res.send(400, {
message: getErrorMessage(err)
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(article);
Expand All @@ -67,7 +44,7 @@ exports.update = function(req, res) {
article.save(function(err) {
if (err) {
return res.send(400, {
message: getErrorMessage(err)
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(article);
Expand All @@ -84,7 +61,7 @@ exports.delete = function(req, res) {
article.remove(function(err) {
if (err) {
return res.send(400, {
message: getErrorMessage(err)
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(article);
Expand All @@ -99,7 +76,7 @@ exports.list = function(req, res) {
Article.find().sort('-created').populate('user', 'displayName').exec(function(err, articles) {
if (err) {
return res.send(400, {
message: getErrorMessage(err)
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(articles);
Expand Down
42 changes: 42 additions & 0 deletions app/controllers/errors.server.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

/**
* Get unique error field name
*/
var getUniqueErrorMessage = function(err) {
var output;

try {
var fieldName = err.err.substring(err.err.lastIndexOf('.$') + 2, err.err.lastIndexOf('_1'));
output = fieldName.charAt(0).toUpperCase() + fieldName.slice(1) + ' already exist';

} catch(ex) {
output = 'Unique field already exist';
}

return output;
};

/**
* Get the error message from error object
*/
exports.getErrorMessage = function(err) {
var message = '';

if (err.code) {
switch (err.code) {
case 11000:
case 11001:
message = getUniqueErrorMessage(err);
break;
default:
message = 'Something went wrong';
}
} else {
for (var errName in err.errors) {
if (err.errors[errName].message) message = err.errors[errName].message;
}
}

return message;
};
Loading

0 comments on commit f4b62ca

Please sign in to comment.