不要怂,就是干,撸起袖子干!

Commit a0aa66b3 by Dr. Evil

New ExclusionConstraintError and handling it in Postgres

1 parent 32474460
Showing with 66 additions and 5 deletions
...@@ -121,7 +121,7 @@ module.exports = (function() { ...@@ -121,7 +121,7 @@ module.exports = (function() {
attribute: field, attribute: field,
collate: attribute.match(/COLLATE "(.*?)"/) ? /COLLATE "(.*?)"/.exec(attribute)[1] : undefined, collate: attribute.match(/COLLATE "(.*?)"/) ? /COLLATE "(.*?)"/.exec(attribute)[1] : undefined,
order: attribute.indexOf('DESC') !== -1 ? 'DESC' : attribute.indexOf('ASC') !== -1 ? 'ASC': undefined, order: attribute.indexOf('DESC') !== -1 ? 'DESC' : attribute.indexOf('ASC') !== -1 ? 'ASC': undefined,
length: undefined, length: undefined
}; };
}); });
delete result.columns; delete result.columns;
...@@ -254,7 +254,10 @@ module.exports = (function() { ...@@ -254,7 +254,10 @@ module.exports = (function() {
Query.prototype.formatError = function (err) { Query.prototype.formatError = function (err) {
var match var match
, table , table
, index; , index
, fields
, errors
, message;
var code = err.code || err.sqlState var code = err.code || err.sqlState
, errMessage = err.message || err.messagePrimary , errMessage = err.message || err.messagePrimary
...@@ -277,9 +280,9 @@ module.exports = (function() { ...@@ -277,9 +280,9 @@ module.exports = (function() {
match = errDetail.match(/Key \((.*?)\)=\((.*?)\)/); match = errDetail.match(/Key \((.*?)\)=\((.*?)\)/);
if (match) { if (match) {
var fields = Utils._.zipObject(match[1].split(', '), match[2].split(', ')) fields = Utils._.zipObject(match[1].split(', '), match[2].split(', '));
, errors = [] errors = [];
, message = 'Validation error'; message = 'Validation error';
Utils._.forOwn(fields, function(value, field) { Utils._.forOwn(fields, function(value, field) {
errors.push(new sequelizeErrors.ValidationErrorItem( errors.push(new sequelizeErrors.ValidationErrorItem(
...@@ -309,6 +312,44 @@ module.exports = (function() { ...@@ -309,6 +312,44 @@ module.exports = (function() {
} }
break; break;
case '23P01':
// there are multiple different formats of error messages for this error code
// this regex should check at least two
match = errDetail.match(/Key \((.*?)\)=\((.*?)\)/);
if (match) {
fields = Utils._.zipObject(match[1].split(', '), match[2].split(', '));
errors = [];
message = 'Validation error';
Utils._.forOwn(fields, function(value, field) {
errors.push(new sequelizeErrors.ValidationErrorItem(
field + ' must be unique', 'unique violation', field, value));
});
if (this.callee && this.callee.__options && this.callee.__options.uniqueKeys) {
Utils._.forOwn(this.callee.__options.uniqueKeys, function(constraint) {
if (Utils._.isEqual(constraint.fields, Object.keys(fields)) && !!constraint.msg) {
message = constraint.msg;
return false;
}
});
}
return new sequelizeErrors.ExclusionConstraintError({
message: message,
errors: errors,
parent: err,
fields: fields
});
} else {
return new sequelizeErrors.ExclusionConstraintError({
message: errMessage,
parent: err
});
}
break;
default: default:
return new sequelizeErrors.DatabaseError(err); return new sequelizeErrors.DatabaseError(err);
} }
......
...@@ -145,6 +145,26 @@ error.ForeignKeyConstraintError = function (options) { ...@@ -145,6 +145,26 @@ error.ForeignKeyConstraintError = function (options) {
util.inherits(error.ForeignKeyConstraintError, error.DatabaseError); util.inherits(error.ForeignKeyConstraintError, error.DatabaseError);
/** /**
* Thrown when an exclusion constraint is violated in the database
* @extends DatabaseError
* @constructor
*/
error.ExclusionConstraintError = function (options) {
options = options || {};
options.parent = options.parent || { sql: '' };
error.DatabaseError.call(this, options.parent);
this.name = 'SequelizeExclusionConstraintError';
this.message = options.message;
this.fields = options.fields;
this.table = options.table;
this.value = options.value;
this.index = options.index;
};
util.inherits(error.ExclusionConstraintError, error.DatabaseError);
/**
* The message from the DB. * The message from the DB.
* @property message * @property message
* @name message * @name message
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!