Skip to content

Commit a574f23

Browse files
committed
Add global errors to error constants
closes #416 closes #518
1 parent 96215d2 commit a574f23

File tree

3 files changed

+140
-14
lines changed

3 files changed

+140
-14
lines changed

Changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ you spot any mistakes.
66

77
## HEAD
88

9+
* Add global errors to error constants
910
* Update known error codes
1011

1112
## v2.3.0 (2014-05-16)

lib/protocol/constants/errors.js

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,41 @@
1-
// Generated by generate-error-constants.js, do not modify by hand
1+
/**
2+
* MySQL error constants
3+
*
4+
* !! Generated by generate-error-constants.js, do not modify by hand !!
5+
*/
6+
7+
exports.EE_CANTCREATEFILE = 1;
8+
exports.EE_READ = 2;
9+
exports.EE_WRITE = 3;
10+
exports.EE_BADCLOSE = 4;
11+
exports.EE_OUTOFMEMORY = 5;
12+
exports.EE_DELETE = 6;
13+
exports.EE_LINK = 7;
14+
exports.EE_EOFERR = 9;
15+
exports.EE_CANTLOCK = 10;
16+
exports.EE_CANTUNLOCK = 11;
17+
exports.EE_DIR = 12;
18+
exports.EE_STAT = 13;
19+
exports.EE_CANT_CHSIZE = 14;
20+
exports.EE_CANT_OPEN_STREAM = 15;
21+
exports.EE_GETWD = 16;
22+
exports.EE_SETWD = 17;
23+
exports.EE_LINK_WARNING = 18;
24+
exports.EE_OPEN_WARNING = 19;
25+
exports.EE_DISK_FULL = 20;
26+
exports.EE_CANT_MKDIR = 21;
27+
exports.EE_UNKNOWN_CHARSET = 22;
28+
exports.EE_OUT_OF_FILERESOURCES = 23;
29+
exports.EE_CANT_READLINK = 24;
30+
exports.EE_CANT_SYMLINK = 25;
31+
exports.EE_REALPATH = 26;
32+
exports.EE_SYNC = 27;
33+
exports.EE_UNKNOWN_COLLATION = 28;
34+
exports.EE_FILENOTFOUND = 29;
35+
exports.EE_FILE_NOT_CLOSED = 30;
36+
exports.EE_CHANGE_OWNERSHIP = 31;
37+
exports.EE_CHANGE_PERMISSIONS = 32;
38+
exports.EE_CANT_SEEK = 33;
239
exports.ER_HASHCHK = 1000;
340
exports.ER_NISAMCHK = 1001;
441
exports.ER_NO = 1002;
@@ -884,6 +921,38 @@ exports.ER_INNODB_FORCED_RECOVERY = 18
884921
exports.ER_AES_INVALID_IV = 1882;
885922

886923
// Lookup-by-number table
924+
exports[1] = 'EE_CANTCREATEFILE';
925+
exports[2] = 'EE_READ';
926+
exports[3] = 'EE_WRITE';
927+
exports[4] = 'EE_BADCLOSE';
928+
exports[5] = 'EE_OUTOFMEMORY';
929+
exports[6] = 'EE_DELETE';
930+
exports[7] = 'EE_LINK';
931+
exports[9] = 'EE_EOFERR';
932+
exports[10] = 'EE_CANTLOCK';
933+
exports[11] = 'EE_CANTUNLOCK';
934+
exports[12] = 'EE_DIR';
935+
exports[13] = 'EE_STAT';
936+
exports[14] = 'EE_CANT_CHSIZE';
937+
exports[15] = 'EE_CANT_OPEN_STREAM';
938+
exports[16] = 'EE_GETWD';
939+
exports[17] = 'EE_SETWD';
940+
exports[18] = 'EE_LINK_WARNING';
941+
exports[19] = 'EE_OPEN_WARNING';
942+
exports[20] = 'EE_DISK_FULL';
943+
exports[21] = 'EE_CANT_MKDIR';
944+
exports[22] = 'EE_UNKNOWN_CHARSET';
945+
exports[23] = 'EE_OUT_OF_FILERESOURCES';
946+
exports[24] = 'EE_CANT_READLINK';
947+
exports[25] = 'EE_CANT_SYMLINK';
948+
exports[26] = 'EE_REALPATH';
949+
exports[27] = 'EE_SYNC';
950+
exports[28] = 'EE_UNKNOWN_COLLATION';
951+
exports[29] = 'EE_FILENOTFOUND';
952+
exports[30] = 'EE_FILE_NOT_CLOSED';
953+
exports[31] = 'EE_CHANGE_OWNERSHIP';
954+
exports[32] = 'EE_CHANGE_PERMISSIONS';
955+
exports[33] = 'EE_CANT_SEEK';
887956
exports[1000] = 'ER_HASHCHK';
888957
exports[1001] = 'ER_NISAMCHK';
889958
exports[1002] = 'ER_NO';

tool/generate-error-constants.js

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,92 @@
11
#!/usr/bin/env node
2+
var fs = require('fs');
23
var path = require('path');
34
var script = path.basename(__filename);
45

5-
var errorFile = process.argv[2];
6-
if (!errorFile) {
7-
console.error('Usage: ./' + script + ' path/to/errmsg-utf8.txt');
6+
var srcDir = process.argv[2];
7+
if (!srcDir) {
8+
var args = [];
9+
args[0] = process.argv[0].indexOf(' ') !== -1
10+
? '"' + process.argv[0] + '"'
11+
: process.argv[0];
12+
args[1] = process.argv[1].indexOf(' ') !== -1
13+
? '"' + process.argv[1] + '"'
14+
: process.argv[1];
15+
args[2] = path.join('path', 'to', 'mysql', 'src');
16+
console.error('Usage: ' + args.join(' '));
817
process.exit(1);
918
}
1019

11-
var fs = require('fs');
12-
var errors = fs.readFileSync(errorFile, 'utf-8');
13-
14-
var targetFile = path.join(__dirname, '../lib/protocol/constants/errors.js');
15-
var offset = Number(errors.match(/start-error-number (\d+)/)[1]);
16-
var codes = errors.match(/^([A-Z_]+)/mg).map(fixupCode);
20+
var codes = [];
21+
var targetFile = path.join(__dirname, '..', 'lib', 'protocol', 'constants', 'errors.js');
1722
var stream = fs.createWriteStream(targetFile);
18-
var alignment = codes.reduce(maxLength, 0);
1923

20-
stream.write('// Generated by ' + script + ', do not modify by hand\n');
24+
appendGlobalErrorCodes(srcDir, codes);
25+
appendSqlErrorCodes(srcDir, codes);
26+
27+
stream.write('/**\n * MySQL error constants\n *\n * !! Generated by ' + script + ', do not modify by hand !!\n */\n\n');
2128

29+
var alignment = codes.reduce(maxLength, 0);
2230
for (var i = 0; i < codes.length; i++) {
23-
stream.write('exports.' + codes[i] + (new Array(alignment - codes[i].length + 1)).join(' ') + ' = ' + (offset + i) + ';\n');
31+
if (i in codes) {
32+
stream.write('exports.' + codes[i] + (new Array(alignment - codes[i].length + 1)).join(' ') + ' = ' + i + ';\n');
33+
}
2434
}
2535

2636
stream.write('\n// Lookup-by-number table\n');
2737

38+
var alignment = String(codes.length).length;
2839
for (var i = 0; i < codes.length; i++) {
29-
stream.write('exports[' + (offset + i) + '] = \'' + codes[i] + '\';\n');
40+
if (i in codes) {
41+
stream.write('exports[' + i + ']' + (new Array(alignment - String(i).length + 1)).join(' ') + ' = \'' + codes[i] + '\';\n');
42+
}
3043
}
3144

3245
console.log('Wrote constants to ' + targetFile);
3346

47+
function appendGlobalErrorCodes(srcDir, codes) {
48+
var headerFile = path.join(srcDir, 'include', 'mysys_err.h');
49+
var code = '';
50+
var contents = fs.readFileSync(headerFile, 'ascii');
51+
var block = false;
52+
var match = null;
53+
var num = 0;
54+
var regexp = /#define +(EE_[A-Z_]+)\s+([0-9]+)/mg;
55+
56+
while ((match = regexp.exec(contents))) {
57+
code = match[1];
58+
num = Number(match[2]);
59+
60+
if (!block) {
61+
block = code === 'EE_ERROR_FIRST';
62+
continue;
63+
}
64+
65+
if (code === 'EE_ERROR_LAST') {
66+
break;
67+
}
68+
69+
codes[num] = code;
70+
}
71+
72+
return codes;
73+
}
74+
75+
function appendSqlErrorCodes(srcDir, codes) {
76+
var errorFile = path.join(srcDir, 'sql', 'share', 'errmsg-utf8.txt');
77+
var contents = fs.readFileSync(errorFile, 'utf-8');
78+
var offset = Number(contents.match(/start-error-number (\d+)/)[1]);
79+
var names = contents.match(/^([A-Z_]+)/mg).map(fixupCode);
80+
var num = 0;
81+
82+
for (var i = 0; i < names.length; i++) {
83+
num = offset + i;
84+
codes[num] = names[i];
85+
}
86+
87+
return codes;
88+
}
89+
3490
function fixupCode(code) {
3591
return code
3692
// remove obsolete markers

0 commit comments

Comments
 (0)