Repository navigation
Expand file tree
/
Copy patherror.go
More file actions
137 lines (113 loc) · 2.33 KB
/
Copy patherror.go
File metadata and controls
137 lines (113 loc) · 2.33 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package pgproto
import (
"bytes"
"fmt"
"io"
)
type Error struct {
Severity []byte
Text []byte
Code []byte
Message []byte
Position []byte
File []byte
Line []byte
Routine []byte
}
func (e *Error) server() {}
func ParseError(r io.Reader) (*Error, error) {
b := newReadBuffer(r)
// 'E'|'N' [int32 - length] ([char - key] [string - value] \0)+ \0
tag, err := b.ReadByte()
if err != nil {
return nil, err
}
if tag != 'E' && tag != 'N' {
return nil, fmt.Errorf("expected tag 'E' or 'N'")
}
b, err = b.ReadLength()
if err != nil {
return nil, err
}
e := &Error{}
for {
value, err := b.ReadString(false)
if err != nil {
return nil, err
}
// This message ends with a single null terminator
if bytes.Equal(value, []byte{'\x00'}) {
break
}
// Strip null terminator from the end
value = bytes.TrimRight(value, "\x00")
code := value[0]
value = value[1:]
switch code {
case 'S':
e.Severity = value
case 'V':
e.Text = value
case 'C':
e.Code = value
case 'M':
e.Message = value
case 'P':
e.Position = value
case 'F':
e.File = value
case 'L':
e.Line = value
case 'R':
e.Routine = value
}
}
return e, nil
}
func (e *Error) Encode() []byte {
return encodeError(e, 'E')
}
func (e *Error) AsMap() map[string]interface{} { return errorMap(e, "Error") }
func (e *Error) String() string { return messageToString(e) }
func encodeError(e *Error, tag byte) []byte {
b := newWriteBuffer()
// Severity
b.WriteByte('S')
b.WriteString(e.Severity, true)
// Text
b.WriteByte('V')
b.WriteString(e.Text, true)
// Code
b.WriteByte('C')
b.WriteString(e.Code, true)
// Message
b.WriteByte('M')
b.WriteString(e.Message, true)
// Position
b.WriteByte('P')
b.WriteString(e.Position, true)
// Line
b.WriteByte('L')
b.WriteString(e.Line, true)
// Routine
b.WriteByte('R')
b.WriteString(e.Routine, true)
// Finalize
b.WriteByte('\x00')
b.Wrap(tag)
return b.Bytes()
}
func errorMap(e *Error, name string) map[string]interface{} {
return map[string]interface{}{
"Type": name,
"Payload": map[string]string{
"Severity": string(e.Severity),
"Text": string(e.Text),
"Code": string(e.Code),
"Message": string(e.Message),
"Position": string(e.Position),
"Line": string(e.Line),
"Routine": string(e.Routine),
},
}
}