From 89d699c2e85882f59a29077f0f0147d00d1d445d Mon Sep 17 00:00:00 2001 From: Simon Paredes Date: Mon, 27 Nov 2023 18:53:55 -0300 Subject: [PATCH] wrap errors instead of just formatting them --- conn.go | 6 +++--- extended_query_builder.go | 2 +- pgconn/pgconn_test.go | 2 +- pgtype/array.go | 20 ++++++++++---------- pgtype/builtin_wrappers.go | 4 ++-- pgtype/date.go | 6 +++--- pgtype/multirange.go | 6 +++--- pgtype/range.go | 14 +++++++------- pgtype/range_codec.go | 16 ++++++++-------- stdlib/sql.go | 2 +- 10 files changed, 39 insertions(+), 39 deletions(-) diff --git a/conn.go b/conn.go index 843313eb..67b726ce 100644 --- a/conn.go +++ b/conn.go @@ -475,7 +475,7 @@ optionLoop: if queryRewriter != nil { sql, arguments, err = queryRewriter.RewriteQuery(ctx, c, sql, arguments) if err != nil { - return pgconn.CommandTag{}, fmt.Errorf("rewrite query failed: %v", err) + return pgconn.CommandTag{}, fmt.Errorf("rewrite query failed: %w", err) } } @@ -742,7 +742,7 @@ optionLoop: sql, args, err = queryRewriter.RewriteQuery(ctx, c, sql, args) if err != nil { rows := c.getRows(ctx, originalSQL, originalArgs) - err = fmt.Errorf("rewrite query failed: %v", err) + err = fmt.Errorf("rewrite query failed: %w", err) rows.fatal(err) return rows, err } @@ -924,7 +924,7 @@ func (c *Conn) SendBatch(ctx context.Context, b *Batch) (br BatchResults) { var err error sql, arguments, err = queryRewriter.RewriteQuery(ctx, c, sql, arguments) if err != nil { - return &batchResults{ctx: ctx, conn: c, err: fmt.Errorf("rewrite query failed: %v", err)} + return &batchResults{ctx: ctx, conn: c, err: fmt.Errorf("rewrite query failed: %w", err)} } } diff --git a/extended_query_builder.go b/extended_query_builder.go index 0bbdfbb5..9c9de5b2 100644 --- a/extended_query_builder.go +++ b/extended_query_builder.go @@ -36,7 +36,7 @@ func (eqb *ExtendedQueryBuilder) Build(m *pgtype.Map, sd *pgconn.StatementDescri for i := range args { err := eqb.appendParam(m, sd.ParamOIDs[i], -1, args[i]) if err != nil { - err = fmt.Errorf("failed to encode args[%d]: %v", i, err) + err = fmt.Errorf("failed to encode args[%d]: %w", i, err) return err } } diff --git a/pgconn/pgconn_test.go b/pgconn/pgconn_test.go index bf42b5d8..80621930 100644 --- a/pgconn/pgconn_test.go +++ b/pgconn/pgconn_test.go @@ -3324,7 +3324,7 @@ func TestSNISupport(t *testing.T) { defer srv.Close() if err := srv.Handshake(); err != nil { - serverErrChan <- fmt.Errorf("handshake: %v", err) + serverErrChan <- fmt.Errorf("handshake: %w", err) return } diff --git a/pgtype/array.go b/pgtype/array.go index 73761956..5ef613cb 100644 --- a/pgtype/array.go +++ b/pgtype/array.go @@ -110,7 +110,7 @@ func parseUntypedTextArray(src string) (*untypedTextArray, error) { r, _, err := buf.ReadRune() if err != nil { - return nil, fmt.Errorf("invalid array: %v", err) + return nil, fmt.Errorf("invalid array: %w", err) } var explicitDimensions []ArrayDimension @@ -122,7 +122,7 @@ func parseUntypedTextArray(src string) (*untypedTextArray, error) { for { r, _, err = buf.ReadRune() if err != nil { - return nil, fmt.Errorf("invalid array: %v", err) + return nil, fmt.Errorf("invalid array: %w", err) } if r == '=' { @@ -133,12 +133,12 @@ func parseUntypedTextArray(src string) (*untypedTextArray, error) { lower, err := arrayParseInteger(buf) if err != nil { - return nil, fmt.Errorf("invalid array: %v", err) + return nil, fmt.Errorf("invalid array: %w", err) } r, _, err = buf.ReadRune() if err != nil { - return nil, fmt.Errorf("invalid array: %v", err) + return nil, fmt.Errorf("invalid array: %w", err) } if r != ':' { @@ -147,12 +147,12 @@ func parseUntypedTextArray(src string) (*untypedTextArray, error) { upper, err := arrayParseInteger(buf) if err != nil { - return nil, fmt.Errorf("invalid array: %v", err) + return nil, fmt.Errorf("invalid array: %w", err) } r, _, err = buf.ReadRune() if err != nil { - return nil, fmt.Errorf("invalid array: %v", err) + return nil, fmt.Errorf("invalid array: %w", err) } if r != ']' { @@ -164,7 +164,7 @@ func parseUntypedTextArray(src string) (*untypedTextArray, error) { r, _, err = buf.ReadRune() if err != nil { - return nil, fmt.Errorf("invalid array: %v", err) + return nil, fmt.Errorf("invalid array: %w", err) } } @@ -178,7 +178,7 @@ func parseUntypedTextArray(src string) (*untypedTextArray, error) { for { r, _, err = buf.ReadRune() if err != nil { - return nil, fmt.Errorf("invalid array: %v", err) + return nil, fmt.Errorf("invalid array: %w", err) } if r == '{' { @@ -195,7 +195,7 @@ func parseUntypedTextArray(src string) (*untypedTextArray, error) { for { r, _, err = buf.ReadRune() if err != nil { - return nil, fmt.Errorf("invalid array: %v", err) + return nil, fmt.Errorf("invalid array: %w", err) } switch r { @@ -214,7 +214,7 @@ func parseUntypedTextArray(src string) (*untypedTextArray, error) { buf.UnreadRune() value, quoted, err := arrayParseValue(buf) if err != nil { - return nil, fmt.Errorf("invalid array value: %v", err) + return nil, fmt.Errorf("invalid array value: %w", err) } if currentDim == counterDim { implicitDimensions[currentDim].Length++ diff --git a/pgtype/builtin_wrappers.go b/pgtype/builtin_wrappers.go index 8bf367c1..b39d3fa1 100644 --- a/pgtype/builtin_wrappers.go +++ b/pgtype/builtin_wrappers.go @@ -231,7 +231,7 @@ func (w *uint64Wrapper) ScanNumeric(v Numeric) error { bi, err := v.toBigInt() if err != nil { - return fmt.Errorf("cannot scan into *uint64: %v", err) + return fmt.Errorf("cannot scan into *uint64: %w", err) } if !bi.IsUint64() { @@ -284,7 +284,7 @@ func (w *uintWrapper) ScanNumeric(v Numeric) error { bi, err := v.toBigInt() if err != nil { - return fmt.Errorf("cannot scan into *uint: %v", err) + return fmt.Errorf("cannot scan into *uint: %w", err) } if !bi.IsUint64() { diff --git a/pgtype/date.go b/pgtype/date.go index 009fc0db..784b16de 100644 --- a/pgtype/date.go +++ b/pgtype/date.go @@ -282,17 +282,17 @@ func (scanPlanTextAnyToDateScanner) Scan(src []byte, dst any) error { if match != nil { year, err := strconv.ParseInt(match[1], 10, 32) if err != nil { - return fmt.Errorf("BUG: cannot parse date that regexp matched (year): %v", err) + return fmt.Errorf("BUG: cannot parse date that regexp matched (year): %w", err) } month, err := strconv.ParseInt(match[2], 10, 32) if err != nil { - return fmt.Errorf("BUG: cannot parse date that regexp matched (month): %v", err) + return fmt.Errorf("BUG: cannot parse date that regexp matched (month): %w", err) } day, err := strconv.ParseInt(match[3], 10, 32) if err != nil { - return fmt.Errorf("BUG: cannot parse date that regexp matched (month): %v", err) + return fmt.Errorf("BUG: cannot parse date that regexp matched (month): %w", err) } // BC matched diff --git a/pgtype/multirange.go b/pgtype/multirange.go index 34950b34..ff2aa4ff 100644 --- a/pgtype/multirange.go +++ b/pgtype/multirange.go @@ -339,7 +339,7 @@ func parseUntypedTextMultirange(src []byte) ([]string, error) { r, _, err := buf.ReadRune() if err != nil { - return nil, fmt.Errorf("invalid array: %v", err) + return nil, fmt.Errorf("invalid array: %w", err) } if r != '{' { @@ -350,7 +350,7 @@ parseValueLoop: for { r, _, err = buf.ReadRune() if err != nil { - return nil, fmt.Errorf("invalid multirange: %v", err) + return nil, fmt.Errorf("invalid multirange: %w", err) } switch r { @@ -361,7 +361,7 @@ parseValueLoop: buf.UnreadRune() value, err := parseRange(buf) if err != nil { - return nil, fmt.Errorf("invalid multirange value: %v", err) + return nil, fmt.Errorf("invalid multirange value: %w", err) } elements = append(elements, value) } diff --git a/pgtype/range.go b/pgtype/range.go index 8f408f9f..16427ccc 100644 --- a/pgtype/range.go +++ b/pgtype/range.go @@ -40,7 +40,7 @@ func parseUntypedTextRange(src string) (*untypedTextRange, error) { r, _, err := buf.ReadRune() if err != nil { - return nil, fmt.Errorf("invalid lower bound: %v", err) + return nil, fmt.Errorf("invalid lower bound: %w", err) } switch r { case '(': @@ -53,7 +53,7 @@ func parseUntypedTextRange(src string) (*untypedTextRange, error) { r, _, err = buf.ReadRune() if err != nil { - return nil, fmt.Errorf("invalid lower value: %v", err) + return nil, fmt.Errorf("invalid lower value: %w", err) } buf.UnreadRune() @@ -62,13 +62,13 @@ func parseUntypedTextRange(src string) (*untypedTextRange, error) { } else { utr.Lower, err = rangeParseValue(buf) if err != nil { - return nil, fmt.Errorf("invalid lower value: %v", err) + return nil, fmt.Errorf("invalid lower value: %w", err) } } r, _, err = buf.ReadRune() if err != nil { - return nil, fmt.Errorf("missing range separator: %v", err) + return nil, fmt.Errorf("missing range separator: %w", err) } if r != ',' { return nil, fmt.Errorf("missing range separator: %v", r) @@ -76,7 +76,7 @@ func parseUntypedTextRange(src string) (*untypedTextRange, error) { r, _, err = buf.ReadRune() if err != nil { - return nil, fmt.Errorf("invalid upper value: %v", err) + return nil, fmt.Errorf("invalid upper value: %w", err) } if r == ')' || r == ']' { @@ -85,12 +85,12 @@ func parseUntypedTextRange(src string) (*untypedTextRange, error) { buf.UnreadRune() utr.Upper, err = rangeParseValue(buf) if err != nil { - return nil, fmt.Errorf("invalid upper value: %v", err) + return nil, fmt.Errorf("invalid upper value: %w", err) } r, _, err = buf.ReadRune() if err != nil { - return nil, fmt.Errorf("missing upper bound: %v", err) + return nil, fmt.Errorf("missing upper bound: %w", err) } switch r { case ')': diff --git a/pgtype/range_codec.go b/pgtype/range_codec.go index 8cfb3a63..684f1bf7 100644 --- a/pgtype/range_codec.go +++ b/pgtype/range_codec.go @@ -120,7 +120,7 @@ func (plan *encodePlanRangeCodecRangeValuerToBinary) Encode(value any, buf []byt buf, err = lowerPlan.Encode(lower, buf) if err != nil { - return nil, fmt.Errorf("failed to encode %v as element of range: %v", lower, err) + return nil, fmt.Errorf("failed to encode %v as element of range: %w", lower, err) } if buf == nil { return nil, fmt.Errorf("Lower cannot be NULL unless LowerType is Unbounded") @@ -144,7 +144,7 @@ func (plan *encodePlanRangeCodecRangeValuerToBinary) Encode(value any, buf []byt buf, err = upperPlan.Encode(upper, buf) if err != nil { - return nil, fmt.Errorf("failed to encode %v as element of range: %v", upper, err) + return nil, fmt.Errorf("failed to encode %v as element of range: %w", upper, err) } if buf == nil { return nil, fmt.Errorf("Upper cannot be NULL unless UpperType is Unbounded") @@ -194,7 +194,7 @@ func (plan *encodePlanRangeCodecRangeValuerToText) Encode(value any, buf []byte) buf, err = lowerPlan.Encode(lower, buf) if err != nil { - return nil, fmt.Errorf("failed to encode %v as element of range: %v", lower, err) + return nil, fmt.Errorf("failed to encode %v as element of range: %w", lower, err) } if buf == nil { return nil, fmt.Errorf("Lower cannot be NULL unless LowerType is Unbounded") @@ -215,7 +215,7 @@ func (plan *encodePlanRangeCodecRangeValuerToText) Encode(value any, buf []byte) buf, err = upperPlan.Encode(upper, buf) if err != nil { - return nil, fmt.Errorf("failed to encode %v as element of range: %v", upper, err) + return nil, fmt.Errorf("failed to encode %v as element of range: %w", upper, err) } if buf == nil { return nil, fmt.Errorf("Upper cannot be NULL unless UpperType is Unbounded") @@ -282,7 +282,7 @@ func (plan *scanPlanBinaryRangeToRangeScanner) Scan(src []byte, target any) erro err = lowerPlan.Scan(ubr.Lower, lowerTarget) if err != nil { - return fmt.Errorf("cannot scan into %v from range element: %v", lowerTarget, err) + return fmt.Errorf("cannot scan into %v from range element: %w", lowerTarget, err) } } @@ -294,7 +294,7 @@ func (plan *scanPlanBinaryRangeToRangeScanner) Scan(src []byte, target any) erro err = upperPlan.Scan(ubr.Upper, upperTarget) if err != nil { - return fmt.Errorf("cannot scan into %v from range element: %v", upperTarget, err) + return fmt.Errorf("cannot scan into %v from range element: %w", upperTarget, err) } } @@ -332,7 +332,7 @@ func (plan *scanPlanTextRangeToRangeScanner) Scan(src []byte, target any) error err = lowerPlan.Scan([]byte(utr.Lower), lowerTarget) if err != nil { - return fmt.Errorf("cannot scan into %v from range element: %v", lowerTarget, err) + return fmt.Errorf("cannot scan into %v from range element: %w", lowerTarget, err) } } @@ -344,7 +344,7 @@ func (plan *scanPlanTextRangeToRangeScanner) Scan(src []byte, target any) error err = upperPlan.Scan([]byte(utr.Upper), upperTarget) if err != nil { - return fmt.Errorf("cannot scan into %v from range element: %v", upperTarget, err) + return fmt.Errorf("cannot scan into %v from range element: %w", upperTarget, err) } } diff --git a/stdlib/sql.go b/stdlib/sql.go index c6c30ab2..86a4c932 100644 --- a/stdlib/sql.go +++ b/stdlib/sql.go @@ -840,7 +840,7 @@ func (r *Rows) Next(dest []driver.Value) error { var err error dest[i], err = r.valueFuncs[i](rv) if err != nil { - return fmt.Errorf("convert field %d failed: %v", i, err) + return fmt.Errorf("convert field %d failed: %w", i, err) } } else { dest[i] = nil