Improve error messages for query argument encoding

pull/1185/head
Jack Christensen 2022-04-01 17:54:56 -05:00
parent 3a6d9490e5
commit 500c0721d7
3 changed files with 14 additions and 3 deletions

View File

@ -488,6 +488,7 @@ func (c *Conn) execParamsAndPreparedPrefix(sd *pgconn.StatementDescription, args
for i := range args {
err := c.eqb.AppendParam(c.typeMap, sd.ParamOIDs[i], args[i])
if err != nil {
err = fmt.Errorf("failed to encode args[%d]: %v", i, err)
return err
}
}
@ -739,6 +740,7 @@ optionLoop:
for i := range args {
err = c.eqb.AppendParam(c.typeMap, sd.ParamOIDs[i], args[i])
if err != nil {
err = fmt.Errorf("failed to encode args[%d]: %v", i, err)
rows.fatal(err)
return rows, rows.err
}
@ -895,6 +897,7 @@ func (c *Conn) SendBatch(ctx context.Context, b *Batch) BatchResults {
for i := range bi.arguments {
err := c.eqb.AppendParam(c.typeMap, sd.ParamOIDs[i], bi.arguments[i])
if err != nil {
err = fmt.Errorf("failed to encode args[%d]: %v", i, err)
return &batchResults{ctx: ctx, conn: c, err: err}
}
}
@ -962,6 +965,7 @@ func (c *Conn) SendBatch(ctx context.Context, b *Batch) BatchResults {
for i := range bi.arguments {
err := c.eqb.AppendParam(c.typeMap, sd.ParamOIDs[i], bi.arguments[i])
if err != nil {
err = fmt.Errorf("failed to encode args[%d]: %v", i, err)
return &batchResults{ctx: ctx, conn: c, err: err}
}
}

View File

@ -1737,7 +1737,14 @@ func (m *Map) Encode(oid uint32, formatCode int16, value interface{}, buf []byte
return m.Encode(oid, formatCode, v, buf)
}
return nil, fmt.Errorf("unable to encode %#v into OID %d", value, oid)
return nil, fmt.Errorf("unable to encode %#v into format code %d for OID %d", value, formatCode, oid)
}
return plan.Encode(value, buf)
newBuf, err = plan.Encode(value, buf)
if err != nil {
err = fmt.Errorf("unable to encode %#v into format code %d for OID %d: %v", value, formatCode, oid, err)
return nil, err
}
return newBuf, nil
}

View File

@ -983,7 +983,7 @@ func TestQueryRowErrors(t *testing.T) {
{"select $1::badtype", []interface{}{"Jack"}, []interface{}{&actual.i16}, `type "badtype" does not exist`},
{"SYNTAX ERROR", []interface{}{}, []interface{}{&actual.i16}, "SQLSTATE 42601"},
{"select $1::text", []interface{}{"Jack"}, []interface{}{&actual.i16}, "cannot scan OID 25 in text format into *int16"},
{"select $1::point", []interface{}{int(705)}, []interface{}{&actual.s}, "unable to encode 705 into OID 600"},
{"select $1::point", []interface{}{int(705)}, []interface{}{&actual.s}, "unable to encode 705 into format code 1 for OID 600"},
}
for i, tt := range tests {