MCPcopy Create free account
hub / github.com/PostHog/duckgres / errorResponseField

Function errorResponseField

server/conn_test.go:1677–1705  ·  view source on GitHub ↗
(buf []byte, field byte)

Source from the content-addressed store, hash-verified

1675// extractErrorResponseField scans wire-protocol frames for an ErrorResponse
1676// ('E') message and returns the requested field value (e.g., 'C' for
1677// SQLSTATE, 'M' for primary message). Fails the test if no ErrorResponse
1678// is found or the field is absent.
1679func extractErrorResponseField(t *testing.T, buf []byte, field byte) string {
1680 t.Helper()
1681 if value, ok := errorResponseField(buf, field); ok {
1682 return value
1683 }
1684 t.Fatalf("no ErrorResponse frame with field %q in output (%d bytes)", field, len(buf))
1685 return ""
1686}
1687
1688func errorResponseField(buf []byte, field byte) (string, bool) {
1689 for i := 0; i+5 <= len(buf); {
1690 msgType := buf[i]
1691 length := int(binary.BigEndian.Uint32(buf[i+1 : i+5]))
1692 if length < 4 || i+1+length > len(buf) {
1693 return "", false
1694 }
1695 body := buf[i+5 : i+1+length]
1696 if msgType == 'E' {
1697 for j := 0; j < len(body); {
1698 code := body[j]
1699 if code == 0 {
1700 break
1701 }
1702 end := bytes.IndexByte(body[j+1:], 0)
1703 if end < 0 {
1704 return "", false
1705 }
1706 if code == field {
1707 return string(body[j+1 : j+1+end]), true
1708 }

Calls

no outgoing calls

Tested by

no test coverage detected