Skip to content
This repository was archived by the owner on Jun 14, 2026. It is now read-only.

Commit b7f0aa2

Browse files
cliedemankoskimas
authored andcommitted
Adding mssql
1 parent 0af4d98 commit b7f0aa2

21 files changed

Lines changed: 576 additions & 101 deletions

File tree

.travis.yml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ addons:
1313
- postgresql-client-10
1414

1515
node_js:
16-
- '6'
17-
- '7'
18-
- '8'
19-
- '9'
20-
- '10'
21-
- '11'
16+
- "6"
17+
- "7"
18+
- "8"
19+
- "9"
20+
- "10"
21+
- "11"
2222

2323
env:
2424
- POSTGRESQL_VERSION=9.4 MYSQL_VERSION=5.5
@@ -33,6 +33,7 @@ before_install:
3333
- sudo service mysql stop || sudo service mysql-5.6 stop || sudo stop mysql-5.6 || sudo stop mysql || echo "failed to stop mysql"
3434
- sudo docker pull mysql:$MYSQL_VERSION
3535
- sudo docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:$MYSQL_VERSION
36+
- sudo docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=eioC9vvCZzQSy4S9g37i' -p 1433:1433 -d microsoft/mssql-server-linux:2017-CU8
3637
- sleep 20
3738

3839
before_script:

docker-compose.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
version: "3"
2+
services:
3+
mssql:
4+
image: "microsoft/mssql-server-linux:2017-CU12"
5+
environment:
6+
- ACCEPT_EULA=Y
7+
- SA_PASSWORD=eioC9vvCZzQSy4S9g37i
8+
ports:
9+
- "1433:1433"
10+
mysql:
11+
image: "mysql:8.0.15"
12+
environment:
13+
- MYSQL_DATABASE=db_errors_test
14+
- MYSQL_USER=db_errors
15+
- MYSQL_PASSWORD=password
16+
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
17+
ports:
18+
- "3306:3306"
19+
postgres:
20+
image: "postgres:11.2"
21+
environment:
22+
- POSTGRES_DB=db_errors_test
23+
- POSTGRES_USER=db_errors
24+
- POSTGRES_PASSWORD=password
25+
ports:
26+
- "3306:3306"

lib/errorCodes/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
const postgres = require('./postgres');
44
const mysql = require('./mysql');
5+
const mssql = require('./mssql');
56

67
module.exports = {
78
postgres,
8-
mysql
9+
mysql,
10+
mssql,
911
};

lib/errorCodes/mssql.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
// select * from sysmessages where [error] in (2601, 2627, 515, 547, 241, 242, 8152, 245) and msglangid = 1033;
4+
5+
const codes = {
6+
16: new Set([
7+
241,
8+
242,
9+
245,
10+
515,
11+
547,
12+
8152
13+
]),
14+
14: new Set([2601, 2627])
15+
};
16+
17+
function has(code) {
18+
const set = codes[code.severity];
19+
20+
if (set) {
21+
return set.has(code.errorCode);
22+
}
23+
24+
return false;
25+
}
26+
27+
// Top level key is the severity
28+
module.exports = {
29+
has,
30+
}

lib/parsers/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const postgres = require('./postgres/DBError/parser');
22
const sqlite = require('./sqlite/DBError/parser');
33
const mysql = require('./mysql/DBError/parser');
4+
const mssql = require('./mssql/DBError/parser');
45

56
module.exports = {
67
postgres,
78
sqlite,
8-
mysql
9+
mysql,
10+
mssql,
911
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const CheckViolationError = require('../../../../errors/CheckViolationError');
2+
const { isCode } = require('../util');
3+
4+
const REGEX = /The (INSERT|UPDATE) statement conflicted with the CHECK constraint "(.+)". The conflict occurred in database "(.+)", table "(.+)", column '(.+)'./
5+
6+
module.exports = {
7+
error: CheckViolationError,
8+
9+
parse: (err) => {
10+
if (isCode(err, 16, 547)) {
11+
const match = REGEX.exec(err.originalError.message);
12+
13+
if (!match) {
14+
return null;
15+
}
16+
17+
return {
18+
table: match[3],
19+
constraint: match[1]
20+
};
21+
} else {
22+
return null;
23+
}
24+
},
25+
26+
subclassParsers: []
27+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const DataError = require('../../../../errors/DataError');
2+
const { isCode } = require('../util');
3+
4+
module.exports = {
5+
error: DataError,
6+
7+
parse: (err) => {
8+
// 241 - Conversion failed when converting date and/or time from character string.
9+
// 242 - The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
10+
// 245 - Conversion failed when converting the nvarchar value 'lol' to data type int.
11+
// 8152 - String or binary data would be truncated.
12+
if (isCode(err, 16, 241) || isCode(err, 16, 242) || isCode(err, 16, 245) || isCode(err, 16, 8152)) {
13+
return {};
14+
} else {
15+
return null;
16+
}
17+
},
18+
19+
subclassParsers: []
20+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const ForeignKeyViolationError = require('../../../../errors/ForeignKeyViolationError');
2+
const { isCode } = require('../util');
3+
4+
const INSERT_UPDATE_REGEX = /The (?:INSERT|UPDATE) statement conflicted with the FOREIGN KEY constraint "(.+)". The conflict occurred in database "(.+)", table "(.+)", column '(.+)'./;
5+
const DELETE_REGEX = /The DELETE statement conflicted with the REFERENCE constraint "(.+)". The conflict occurred in database "(.+)", table "(.+)", column '(.+)'./;
6+
7+
module.exports = {
8+
error: ForeignKeyViolationError,
9+
10+
parse: (err) => {
11+
if (isCode(err, 16, 547)) {
12+
const insertUpdateMatch = INSERT_UPDATE_REGEX.exec(err.originalError.message);
13+
14+
if (insertUpdateMatch) {
15+
return {
16+
table: insertUpdateMatch[3],
17+
constraint: insertUpdateMatch[1],
18+
};
19+
}
20+
21+
const deleteMatch = DELETE_REGEX.exec(err.originalError.message);
22+
23+
if (deleteMatch) {
24+
return {
25+
table: deleteMatch[3],
26+
constraint: deleteMatch[1],
27+
};
28+
}
29+
}
30+
31+
return null;
32+
},
33+
34+
subclassParsers: []
35+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const NotNullViolationError = require('../../../../errors/NotNullViolationError');
2+
const { isCode } = require('../util');
3+
4+
const REGEX = /Cannot insert the value NULL into column '(.+)', table '(.+)'; column does not allow nulls. (?:INSERT|UPDATE) fails./;
5+
6+
module.exports = {
7+
error: NotNullViolationError,
8+
9+
parse: (err) => {
10+
if (isCode(err, 16, 515)) {
11+
const match = REGEX.exec(err.originalError.message);
12+
13+
if (!match) {
14+
return null;
15+
}
16+
17+
return {
18+
table: match[2],
19+
column: match[1],
20+
};
21+
} else {
22+
return null;
23+
}
24+
},
25+
26+
subclassParsers: []
27+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const UniqueViolationError = require('../../../../errors/UniqueViolationError');
2+
const { isCode } = require('../util');
3+
4+
const UNIQUE_INDEX_REGEX = /Cannot insert duplicate key row in object '(.+)' with unique index '(.+)'. The duplicate key value is (.+)./;
5+
const UNIQUE_CONSTRAINT_REGEX = /Violation of UNIQUE KEY constraint '(.+)'. Cannot insert duplicate key in object '(.+)'. The duplicate key value is \((.+)\)/;
6+
7+
// 2601 - Violation in unique index
8+
// 2627 - Violation in unique constraint (although it is implemented using unique index)
9+
10+
module.exports = {
11+
error: UniqueViolationError,
12+
13+
parse: (err) => {
14+
if (isCode(err, 14, 2627)) {
15+
const constraintMatch = UNIQUE_CONSTRAINT_REGEX.exec(err.originalError.message);
16+
17+
if (!constraintMatch) {
18+
return null;
19+
}
20+
21+
return {
22+
table: constraintMatch[2],
23+
constraint: constraintMatch[1]
24+
};
25+
}
26+
27+
// TODO: this case is missing a test
28+
if (isCode(err, 14, 2601)) {
29+
const indexMatch = UNIQUE_INDEX_REGEX.exec(err.originalError.message);
30+
31+
if (!indexMatch) {
32+
return null;
33+
}
34+
35+
return {
36+
table: indexMatch[1],
37+
constraint: indexMatch[2]
38+
};
39+
}
40+
41+
return null;
42+
},
43+
44+
subclassParsers: []
45+
};

0 commit comments

Comments
 (0)