error_response.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. package pgproto3
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "strconv"
  6. )
  7. type ErrorResponse struct {
  8. Severity string
  9. SeverityUnlocalized string // only in 9.6 and greater
  10. Code string
  11. Message string
  12. Detail string
  13. Hint string
  14. Position int32
  15. InternalPosition int32
  16. InternalQuery string
  17. Where string
  18. SchemaName string
  19. TableName string
  20. ColumnName string
  21. DataTypeName string
  22. ConstraintName string
  23. File string
  24. Line int32
  25. Routine string
  26. UnknownFields map[byte]string
  27. }
  28. // Backend identifies this message as sendable by the PostgreSQL backend.
  29. func (*ErrorResponse) Backend() {}
  30. // Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message
  31. // type identifier and 4 byte message length.
  32. func (dst *ErrorResponse) Decode(src []byte) error {
  33. *dst = ErrorResponse{}
  34. buf := bytes.NewBuffer(src)
  35. for {
  36. k, err := buf.ReadByte()
  37. if err != nil {
  38. return err
  39. }
  40. if k == 0 {
  41. break
  42. }
  43. vb, err := buf.ReadBytes(0)
  44. if err != nil {
  45. return err
  46. }
  47. v := string(vb[:len(vb)-1])
  48. switch k {
  49. case 'S':
  50. dst.Severity = v
  51. case 'V':
  52. dst.SeverityUnlocalized = v
  53. case 'C':
  54. dst.Code = v
  55. case 'M':
  56. dst.Message = v
  57. case 'D':
  58. dst.Detail = v
  59. case 'H':
  60. dst.Hint = v
  61. case 'P':
  62. s := v
  63. n, _ := strconv.ParseInt(s, 10, 32)
  64. dst.Position = int32(n)
  65. case 'p':
  66. s := v
  67. n, _ := strconv.ParseInt(s, 10, 32)
  68. dst.InternalPosition = int32(n)
  69. case 'q':
  70. dst.InternalQuery = v
  71. case 'W':
  72. dst.Where = v
  73. case 's':
  74. dst.SchemaName = v
  75. case 't':
  76. dst.TableName = v
  77. case 'c':
  78. dst.ColumnName = v
  79. case 'd':
  80. dst.DataTypeName = v
  81. case 'n':
  82. dst.ConstraintName = v
  83. case 'F':
  84. dst.File = v
  85. case 'L':
  86. s := v
  87. n, _ := strconv.ParseInt(s, 10, 32)
  88. dst.Line = int32(n)
  89. case 'R':
  90. dst.Routine = v
  91. default:
  92. if dst.UnknownFields == nil {
  93. dst.UnknownFields = make(map[byte]string)
  94. }
  95. dst.UnknownFields[k] = v
  96. }
  97. }
  98. return nil
  99. }
  100. // Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length.
  101. func (src *ErrorResponse) Encode(dst []byte) ([]byte, error) {
  102. dst, sp := beginMessage(dst, 'E')
  103. dst = src.appendFields(dst)
  104. return finishMessage(dst, sp)
  105. }
  106. func (src *ErrorResponse) appendFields(dst []byte) []byte {
  107. if src.Severity != "" {
  108. dst = append(dst, 'S')
  109. dst = append(dst, src.Severity...)
  110. dst = append(dst, 0)
  111. }
  112. if src.SeverityUnlocalized != "" {
  113. dst = append(dst, 'V')
  114. dst = append(dst, src.SeverityUnlocalized...)
  115. dst = append(dst, 0)
  116. }
  117. if src.Code != "" {
  118. dst = append(dst, 'C')
  119. dst = append(dst, src.Code...)
  120. dst = append(dst, 0)
  121. }
  122. if src.Message != "" {
  123. dst = append(dst, 'M')
  124. dst = append(dst, src.Message...)
  125. dst = append(dst, 0)
  126. }
  127. if src.Detail != "" {
  128. dst = append(dst, 'D')
  129. dst = append(dst, src.Detail...)
  130. dst = append(dst, 0)
  131. }
  132. if src.Hint != "" {
  133. dst = append(dst, 'H')
  134. dst = append(dst, src.Hint...)
  135. dst = append(dst, 0)
  136. }
  137. if src.Position != 0 {
  138. dst = append(dst, 'P')
  139. dst = append(dst, strconv.Itoa(int(src.Position))...)
  140. dst = append(dst, 0)
  141. }
  142. if src.InternalPosition != 0 {
  143. dst = append(dst, 'p')
  144. dst = append(dst, strconv.Itoa(int(src.InternalPosition))...)
  145. dst = append(dst, 0)
  146. }
  147. if src.InternalQuery != "" {
  148. dst = append(dst, 'q')
  149. dst = append(dst, src.InternalQuery...)
  150. dst = append(dst, 0)
  151. }
  152. if src.Where != "" {
  153. dst = append(dst, 'W')
  154. dst = append(dst, src.Where...)
  155. dst = append(dst, 0)
  156. }
  157. if src.SchemaName != "" {
  158. dst = append(dst, 's')
  159. dst = append(dst, src.SchemaName...)
  160. dst = append(dst, 0)
  161. }
  162. if src.TableName != "" {
  163. dst = append(dst, 't')
  164. dst = append(dst, src.TableName...)
  165. dst = append(dst, 0)
  166. }
  167. if src.ColumnName != "" {
  168. dst = append(dst, 'c')
  169. dst = append(dst, src.ColumnName...)
  170. dst = append(dst, 0)
  171. }
  172. if src.DataTypeName != "" {
  173. dst = append(dst, 'd')
  174. dst = append(dst, src.DataTypeName...)
  175. dst = append(dst, 0)
  176. }
  177. if src.ConstraintName != "" {
  178. dst = append(dst, 'n')
  179. dst = append(dst, src.ConstraintName...)
  180. dst = append(dst, 0)
  181. }
  182. if src.File != "" {
  183. dst = append(dst, 'F')
  184. dst = append(dst, src.File...)
  185. dst = append(dst, 0)
  186. }
  187. if src.Line != 0 {
  188. dst = append(dst, 'L')
  189. dst = append(dst, strconv.Itoa(int(src.Line))...)
  190. dst = append(dst, 0)
  191. }
  192. if src.Routine != "" {
  193. dst = append(dst, 'R')
  194. dst = append(dst, src.Routine...)
  195. dst = append(dst, 0)
  196. }
  197. for k, v := range src.UnknownFields {
  198. dst = append(dst, k)
  199. dst = append(dst, v...)
  200. dst = append(dst, 0)
  201. }
  202. dst = append(dst, 0)
  203. return dst
  204. }
  205. // MarshalJSON implements encoding/json.Marshaler.
  206. func (src ErrorResponse) MarshalJSON() ([]byte, error) {
  207. return json.Marshal(struct {
  208. Type string
  209. Severity string
  210. SeverityUnlocalized string // only in 9.6 and greater
  211. Code string
  212. Message string
  213. Detail string
  214. Hint string
  215. Position int32
  216. InternalPosition int32
  217. InternalQuery string
  218. Where string
  219. SchemaName string
  220. TableName string
  221. ColumnName string
  222. DataTypeName string
  223. ConstraintName string
  224. File string
  225. Line int32
  226. Routine string
  227. UnknownFields map[byte]string
  228. }{
  229. Type: "ErrorResponse",
  230. Severity: src.Severity,
  231. SeverityUnlocalized: src.SeverityUnlocalized,
  232. Code: src.Code,
  233. Message: src.Message,
  234. Detail: src.Detail,
  235. Hint: src.Hint,
  236. Position: src.Position,
  237. InternalPosition: src.InternalPosition,
  238. InternalQuery: src.InternalQuery,
  239. Where: src.Where,
  240. SchemaName: src.SchemaName,
  241. TableName: src.TableName,
  242. ColumnName: src.ColumnName,
  243. DataTypeName: src.DataTypeName,
  244. ConstraintName: src.ConstraintName,
  245. File: src.File,
  246. Line: src.Line,
  247. Routine: src.Routine,
  248. UnknownFields: src.UnknownFields,
  249. })
  250. }
  251. // UnmarshalJSON implements encoding/json.Unmarshaler.
  252. func (dst *ErrorResponse) UnmarshalJSON(data []byte) error {
  253. // Ignore null, like in the main JSON package.
  254. if string(data) == "null" {
  255. return nil
  256. }
  257. var msg struct {
  258. Type string
  259. Severity string
  260. SeverityUnlocalized string // only in 9.6 and greater
  261. Code string
  262. Message string
  263. Detail string
  264. Hint string
  265. Position int32
  266. InternalPosition int32
  267. InternalQuery string
  268. Where string
  269. SchemaName string
  270. TableName string
  271. ColumnName string
  272. DataTypeName string
  273. ConstraintName string
  274. File string
  275. Line int32
  276. Routine string
  277. UnknownFields map[byte]string
  278. }
  279. if err := json.Unmarshal(data, &msg); err != nil {
  280. return err
  281. }
  282. dst.Severity = msg.Severity
  283. dst.SeverityUnlocalized = msg.SeverityUnlocalized
  284. dst.Code = msg.Code
  285. dst.Message = msg.Message
  286. dst.Detail = msg.Detail
  287. dst.Hint = msg.Hint
  288. dst.Position = msg.Position
  289. dst.InternalPosition = msg.InternalPosition
  290. dst.InternalQuery = msg.InternalQuery
  291. dst.Where = msg.Where
  292. dst.SchemaName = msg.SchemaName
  293. dst.TableName = msg.TableName
  294. dst.ColumnName = msg.ColumnName
  295. dst.DataTypeName = msg.DataTypeName
  296. dst.ConstraintName = msg.ConstraintName
  297. dst.File = msg.File
  298. dst.Line = msg.Line
  299. dst.Routine = msg.Routine
  300. dst.UnknownFields = msg.UnknownFields
  301. return nil
  302. }