forked from denodrivers/postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.ts
106 lines (98 loc) · 2.4 KB
/
error.ts
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { Message } from "./connection.ts";
export interface ErrorFields {
severity: string;
code: string;
message: string;
detail?: string;
hint?: string;
position?: string;
internalPosition?: string;
internalQuery?: string;
where?: string;
schemaName?: string;
table?: string;
column?: string;
dataType?: string;
contraint?: string;
file?: string;
line?: string;
routine?: string;
}
export class PostgresError extends Error {
public fields: ErrorFields;
constructor(fields: ErrorFields) {
super(fields.message);
this.fields = fields;
this.name = "PostgresError";
}
}
export function parseError(msg: Message): PostgresError {
// https://www.postgresql.org/docs/current/protocol-error-fields.html
const errorFields: any = {};
let byte: number;
let char: string;
let errorMsg: string;
while ((byte = msg.reader.readByte())) {
char = String.fromCharCode(byte);
errorMsg = msg.reader.readCString();
switch (char) {
case "S":
errorFields.severity = errorMsg;
break;
case "C":
errorFields.code = errorMsg;
break;
case "M":
errorFields.message = errorMsg;
break;
case "D":
errorFields.detail = errorMsg;
break;
case "H":
errorFields.hint = errorMsg;
break;
case "P":
errorFields.position = errorMsg;
break;
case "p":
errorFields.internalPosition = errorMsg;
break;
case "q":
errorFields.internalQuery = errorMsg;
break;
case "W":
errorFields.where = errorMsg;
break;
case "s":
errorFields.schema = errorMsg;
break;
case "t":
errorFields.table = errorMsg;
break;
case "c":
errorFields.column = errorMsg;
break;
case "d":
errorFields.dataTypeName = errorMsg;
break;
case "n":
errorFields.constraint = errorMsg;
break;
case "F":
errorFields.file = errorMsg;
break;
case "L":
errorFields.line = errorMsg;
break;
case "R":
errorFields.routine = errorMsg;
break;
default:
// from Postgres docs
// > Since more field types might be added in future,
// > frontends should silently ignore fields of unrecognized type.
break;
}
}
return new PostgresError(errorFields);
}