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

Commit 554b223c by zhangshichun Committed by Sushant

fix(mysql): match with newlines in error message (#10320)

1 parent b875b7aa
...@@ -261,7 +261,7 @@ class Query extends AbstractQuery { ...@@ -261,7 +261,7 @@ class Query extends AbstractQuery {
switch (err.errno) { switch (err.errno) {
case ER_DUP_ENTRY: { case ER_DUP_ENTRY: {
const match = err.message.match( const match = err.message.match(
/Duplicate entry '(.*)' for key '?((.|\s)*?)'?\s.*$/); /Duplicate entry '([\s\S]*)' for key '?((.|\s)*?)'?[\s.*]?$/);
let fields = {}; let fields = {};
let message = 'Validation error'; let message = 'Validation error';
......
...@@ -211,8 +211,7 @@ class Query extends AbstractQuery { ...@@ -211,8 +211,7 @@ class Query extends AbstractQuery {
switch (errCode) { switch (errCode) {
case 1062: { case 1062: {
const match = err.message.match(/Duplicate entry '(.*)' for key '?((.|\s)*?)'?$/); const match = err.message.match(/Duplicate entry '([\s\S]*)' for key '?((.|\s)*?)'?$/);
let fields = {}; let fields = {};
let message = 'Validation error'; let message = 'Validation error';
const values = match ? match[1].split('-') : undefined; const values = match ? match[1].split('-') : undefined;
......
...@@ -40,5 +40,17 @@ if (dialect === 'mariadb') { ...@@ -40,5 +40,17 @@ if (dialect === 'mariadb') {
expect(parsedErr.value).to.be.undefined; expect(parsedErr.value).to.be.undefined;
expect(parsedErr.index).to.equal('brothers_ibfk_1'); expect(parsedErr.index).to.equal('brothers_ibfk_1');
}); });
it('newlines contained in err message are parsed correctly', () => {
const fakeErr = new Error("Duplicate entry 'test\r' for key 'num'");
fakeErr.errno = 1062;
const parsedErr = queryProto.formatError(fakeErr);
expect(parsedErr).to.be.instanceOf(Sequelize.UniqueConstraintError);
expect(parsedErr.parent).to.equal(fakeErr);
expect(parsedErr.fields.num).to.equal('test\r');
});
}); });
} }
...@@ -40,5 +40,18 @@ if (dialect === 'mysql') { ...@@ -40,5 +40,18 @@ if (dialect === 'mysql') {
expect(parsedErr.value).to.be.undefined; expect(parsedErr.value).to.be.undefined;
expect(parsedErr.index).to.equal('brothers_ibfk_1'); expect(parsedErr.index).to.equal('brothers_ibfk_1');
}); });
it('newlines contained in err message are parsed correctly', () => {
const fakeErr = new Error("Duplicate entry '13888888888\r' for key 'num'");
fakeErr.code = 1062;
const parsedErr = queryProto.formatError(fakeErr);
expect(parsedErr).to.be.instanceOf(Sequelize.UniqueConstraintError);
expect(parsedErr.parent).to.equal(fakeErr);
expect(parsedErr.fields.num).to.equal('13888888888\r');
});
}); });
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!