{"id":"4a6d6dbb27a99409","repo":"jackc/pgx","slug":"w-expected-d-bytes-got-d","errorCode":null,"errorMessage":"%w: expected %d bytes, got %d","messagePattern":"%w: expected (.+?) bytes, got (.+?)","errorType":"exception","errorClass":"ErrInvalidLength","httpStatus":null,"severity":"error","filePath":"internal/pgio/read.go","lineNumber":216,"sourceCode":"\tif r.err != nil {\n\t\treturn r.err\n\t}\n\tif r.rp != len(r.s) {\n\t\treturn r.errTrailing()\n\t}\n\treturn nil\n}\n\nfunc (r *Reader) errTrailing() error {\n\treturn fmt.Errorf(\"%d unexpected trailing bytes at offset %d\", len(r.s)-r.rp, r.rp)\n}\n\n// ErrInvalidLength is wrapped by the errors returned from the exact-length\n// read functions below.\nvar ErrInvalidLength = errors.New(\"invalid length\")\n\nfunc errLength(want, got int) error {\n\treturn fmt.Errorf(\"%w: expected %d bytes, got %d\", ErrInvalidLength, want, got)\n}\n\n// The Uint*Exact functions read a single fixed-size value that makes up an\n// entire message, which is what the scan plans for the fixed-size PostgreSQL\n// types receive. They are the counterpart to Reader for values that have no\n// internal structure: there is no position to track and no error to make\n// sticky, just an exact-length assertion the caller cannot skip. Keeping them\n// separate from Reader is deliberate — these are the hottest decode paths in\n// the driver and they are small enough for the compiler to inline, which a\n// Reader method carrying a bounds check and a read pointer is not.\n\n// Uint16Exact returns the big-endian uint16 in src, which must be exactly 2 bytes.\nfunc Uint16Exact(src []byte) (uint16, error) {\n\tif len(src) != 2 {\n\t\treturn 0, errLength(2, len(src))\n\t}\n\treturn binary.BigEndian.Uint16(src), nil\n}","sourceCodeStart":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/jackc/pgx/blob/ec1a0befd22592cffffdeeb0a50311b506372f4c/internal/pgio/read.go#L198-L234","documentation":"Error \"%w: expected %d bytes, got %d\" thrown in jackc/pgx.","triggerScenarios":"Thrown at internal/pgio/read.go:216 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"analyzedSha":"ec1a0befd22592cffffdeeb0a50311b506372f4c","analyzedAt":"2026-08-04T22:52:11.263Z","schemaVersion":2}