Skip to content

Instantly share code, notes, and snippets.

@dniccum
Last active February 29, 2016 23:03
Show Gist options
  • Save dniccum/aa4cc537ded4b5a2532c to your computer and use it in GitHub Desktop.
Save dniccum/aa4cc537ded4b5a2532c to your computer and use it in GitHub Desktop.
A custom error reporting workflow for SailsJS that uses AngularJS and the NodeJS package sails-hook-validation
app = angular.module('sample');
app.factory('ErrorAPI', [function() {
return {
ParseErrors: function(errors) {
if (Array.isArray(errors.data.err)) {
for (var item in errors.data.err) {
console.log(errors.data.err[item], 'Error');
}
} else if (errors.data.err) {
console.log(errors.data.err, 'Error');
} else {
console.log(errors.data, 'Error');
}
}
}
}]);
module.exports = {
ParseUserErrors: function(errors) {
var validationErrors = [];
for (var key in errors) {
if (errors.hasOwnProperty(key)) {
for (var item in errors[key]) {
if (errors[key][item].rule !== "string") {
validationErrors.push(errors[key][item].message);
}
}
}
}
return validationErrors;
}
};
app = angular.module('sample');
app.controller('userController', [function() {
$scope.formData = {};
$scope.formSubmit = function() {
$http({
method: 'POST',
url: '/api/user',
withCredentials: true,
data: {
firstName: $scope.formData.firstName,
lastName: $scope.formData.lastName,
email: $scope.formData.email,
password: $scope.formData.password
}
}).then(function successCallback(response) {
console.log('success',response)
}, function errorCallback(errorResponse) {
console.log('error',errorResponse);
ErrorAPI.ParseErrors(errorResponse);
});
};
}]);
module.exports = {
attributes: {
firstName: {
type: 'string',
required: true,
minLength: 2,
maxLength: 30
},
lastName: {
type: 'string',
required: true,
minLength: 2,
maxLength: 30
},
email: {
type: 'email',
required: true,
unique: true,
maxLength: 40
},
password: {
type: 'string',
required: true
}
},
validationMessages: {
firstName: {
required: "'First Name' should be a name and is required.",
minLength: 'The name that was provided was not long enough.',
maxLength: 'The name that was provided was too long.'
},
lastName: {
required: "'Last Name' should be a name and is required.",
minLength: 'The name that was provided was not long enough.',
maxLength: 'The name that was provided was too long.'
},
email: {
required: "'Email' is required.",
type: 'This is not a valid email address.',
maxLength: 'The email address that was provided is too long.'
}
}
};
module.exports = {
create: function (req, res) {
User.create({
firstName: req.param('firstName'),
lastName: req.param('lastName'),
email: req.param('email'),
password: req.param('password')
}, function userCreated(err, newUser) {
if (err) {
// err.Errors is the custom array that sails-hook-validation adds
if (err && err.Errors) {
var parsedErrors = ErrorService.ParseUserErrors(err.Errors);
return res.badRequest({err : parsedErrors});
}
return res.negotiate(err);
}
return res.ok(newUser);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment