Skip to content

Commit

Permalink
fix(errors): translate server errors
Browse files Browse the repository at this point in the history
This commit implements meaningful, translated server error messages.
This should help a user know what went wrong with a little bit more
accuracy.

Closes #70.  Closes #97.
  • Loading branch information
Jonathan Niles authored and jniles committed Jan 9, 2017
1 parent 56b0d49 commit 305a470
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 10 deletions.
12 changes: 11 additions & 1 deletion client/src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,17 @@
"NOT_FOUND" : "This record could not be found",
"UNAUTHORIZED" : "You have submitted a bad username and password combination. Please login in with valid credentials to continue.",
"UNKNOWN" : "An error occurred.",
"OVERPAID_INVOICE" : "You have overpaid the invoices."
"OVERPAID_INVOICE" : "You have overpaid the invoices.",
"ER_DUP_KEY" : "A key collided in a unique database field. Please retry your action. If the problem persists, contact the developers.",
"ER_DUP_ENTRY" : "You have duplicated a value in a unique field! Please make sure that the necessary values are unique.",
"ER_BAD_FIELD_ERROR" : "Column does not exist in database.",
"ER_ROW_IS_REFERENCED" : "Cannot delete entity because entity is used in another table.",
"ER_ROW_IS_REFERENCED_2" : "Cannot delete entity because entity is used in another table.",
"ER_BAD_NULL_ERROR" : "A column was left NULL that cannot be NULL.",
"ER_PARSE_ERROR" : "Your request could not be translated into valid SQL. Please modify your request and try again.",
"ER_EMPTY_QUERY" : "Your request seems empty. Are you sure you filled everything in?",
"ER_NO_DEFAULT_FOR_FIELD" : "You did not include enough information in your submission! Please check your errors and try again.",
"ER_DATA_TOO_LONG" : "One of your form values is too long! Please shorten it and resubmit."
},
"EXCHANGE": {
"ADDING_RATE" : "Adding an exchange Rate",
Expand Down
12 changes: 11 additions & 1 deletion client/src/i18n/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,17 @@
"NOT_FOUND" : "Cet enregistrement n'a pas pu être trouvé",
"UNAUTHORIZED" : "Vous avez soumis une mauvais nom et mot de passe. Entrez un nom et mot de passe valid pour continuer.",
"UNKNOWN" : "Une erreur est survenue.",
"OVERPAID_INVOICE" : "Vous avez payé trop aux factures."
"OVERPAID_INVOICE" : "Vous avez payé trop aux factures.",
"ER_DUP_KEY" : "Il ya déjà un enregistrement avec cette clé. Veuillez réessayer votre action. Si le problème persiste, contactez les développeurs.",
"ER_DUP_ENTRY" : "Vous avez dupliqué une valeur dans un champ unique! Assurez-vous que les valeurs nécessaires sont uniques.",
"ER_BAD_FIELD_ERROR" : "Vous avez entré un champ qui ne peut pas être stocké dans la base de données.",
"ER_ROW_IS_REFERENCED" : "Vous ne pouvez pas supprimer cet enregistrement référencé par un autre enregistrement dans la base de données.",
"ER_ROW_IS_REFERENCED_2" :"Vous ne pouvez pas supprimer cet enregistrement référencé par un autre enregistrement dans la base de données.",
"ER_BAD_NULL_ERROR" : "Un champ a été laissé vide qui ne peut pas être vide.",
"ER_PARSE_ERROR" : "Votre demande n'a pas été comprise. Veuillez modifier votre demande et réessayer.",
"ER_EMPTY_QUERY" : "Votre demande semble vide. Êtes-vous sûr de remplir tout?",
"ER_NO_DEFAULT_FOR_FIELD" : "Vous n'avez pas inclus suffisamment d'informations dans votre soumission! Veuillez vérifier vos erreurs et réessayer.",
"ER_DATA_TOO_LONG" : "Une de vos valeurs de formulaire est trop longue! Veuillez le raccourcir et le renvoyer."
},
"EXCHANGE": {
"ADDING_RATE" : "Ajout d'un taux",
Expand Down
9 changes: 5 additions & 4 deletions server/config/interceptors.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

/**
* @overview interceptors
* This modules defines a unified error handler for the server.
Expand All @@ -10,7 +12,6 @@
* @requires winston
* @requires BadRequest
*/
'use strict';

const winston = require('winston');
const BadRequest = require('../lib/errors/BadRequest');
Expand All @@ -21,11 +22,11 @@ const map = {
`A key collided in a unique database field. Please retry your action. If
the problem persists, contact the developers.`,
ER_BAD_FIELD_ERROR : 'Column does not exist in database.',
ER_ROW_IS_REFERENCED :
'Cannot delete entity because entity is used in another table.',
ER_ROW_IS_REFERENCED_2 :
'Cannot delete entity because entity is used in another table.',
ER_BAD_NULL_ERROR : 'A column was left NULL that cannot be NULL.',
ER_DUP_ENTRY :
'You cannot insert a duplicate record. This record already exists.',
ER_PARSE_ERROR :
`Your request could not be translated into valid SQL. Please modify your
request and try again.`,
Expand Down Expand Up @@ -66,7 +67,7 @@ exports.handler = function handler(error, req, res, next) {
key = SQL_STATES[error.sqlState];
description = error.toString();
} else {
key = 'ERRORS.BAD_REQUEST';
key = `ERRORS.${error.code}`;
description = map[error.code];
}

Expand Down
2 changes: 1 addition & 1 deletion test/integration/depots.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('(/depots) The depots API ', function () {
.send(badDepot)
.then(function (res) {
helpers.api.errored(res, 400);
expect(res.body.code).to.be.equal('ERRORS.BAD_REQUEST');
expect(res.body.code).to.be.equal('ERRORS.ER_NO_DEFAULT_FOR_FIELD');
})
.catch(helpers.handler);
});
Expand Down
2 changes: 1 addition & 1 deletion test/integration/priceList.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ describe('(/prices ) Price List', function () {
.then(function (res) {
helpers.api.errored(res, 400);

expect(res.body.code).to.equal('ERRORS.BAD_REQUEST');
expect(res.body.code).to.equal('ERRORS.ER_BAD_NULL_ERROR');

// make sure we didn't gain a price list!
return agent.get('/prices');
Expand Down
4 changes: 2 additions & 2 deletions test/integration/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('(/users) Users and Permissions', function () {
return agent.post('/users')
.send(badUser)
.then(function (res) {
helpers.api.errored(res, 400, 'ERRORS.BAD_REQUEST');
helpers.api.errored(res, 400, 'ERRORS.ER_BAD_NULL_ERROR');
})
.catch(helpers.handler);
});
Expand All @@ -76,7 +76,7 @@ describe('(/users) Users and Permissions', function () {
return agent.get('/users/' + newUser.id)
.then(function (res) {
expect(res).to.have.status(200);
expect(res).to.be.json;
expect(res).to.be.json;
expect(res.body.email).to.equal(newUser.email);
expect(res.body.display_name).to.equal(newUser.display_name);
})
Expand Down

0 comments on commit 305a470

Please sign in to comment.