-
Notifications
You must be signed in to change notification settings - Fork 2
/
error.js
53 lines (46 loc) · 1.69 KB
/
error.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const {EOL} = require('os');
const {addInspection, messageGap} = require('./utils');
const parsingErrorCode = {
unclosedMLC: 0, // Unclosed multi-line comment.
unclosedText: 1, // Unclosed text block.
unclosedQI: 2, // Unclosed quoted identifier.
multiLineQI: 3 // Multi-line quoted identifiers are not supported.
};
Object.freeze(parsingErrorCode);
const errorMessages = [
{name: 'unclosedMLC', message: 'Unclosed multi-line comment.'},
{name: 'unclosedText', message: 'Unclosed text block.'},
{name: 'unclosedQI', message: 'Unclosed quoted identifier.'},
{name: 'multiLineQI', message: 'Multi-line quoted identifiers are not supported.'}
];
class SQLParsingError extends Error {
constructor(code, position) {
const err = errorMessages[code].message;
const message = `Error parsing SQL at {line:${position.line},col:${position.column}}: ${err}`;
super(message);
this.name = this.constructor.name;
this.error = err;
this.code = code;
this.position = position;
Error.captureStackTrace(this, this.constructor);
}
}
SQLParsingError.prototype.toString = function (level) {
level = level > 0 ? parseInt(level) : 0;
const gap = messageGap(level + 1);
const lines = [
'SQLParsingError {',
`${gap}code: parsingErrorCode.${errorMessages[this.code].name}`,
`${gap}error: "${this.error}"`,
`${gap}position: {line: ${this.position.line}, col: ${this.position.column}}`,
`${messageGap(level)}}`
];
return lines.join(EOL);
};
addInspection(SQLParsingError.prototype, function () {
return this.toString();
});
module.exports = {
SQLParsingError,
parsingErrorCode
};