golistic / pxmysql

Go MySQL driver using X Protocol
MIT License
14 stars 0 forks source link

Wrapped client errors not correctly formatted #4

Closed geertjanvdk closed 1 year ago

geertjanvdk commented 1 year ago

Some MySQL Client errors allow Go errors to be wrapped. For example, the mysqlerrors.ClientBadTCPSocket, on timeout, it will include the underlaying Go error "i/o timeout". This, however, fails, because we use fmt.Sprintf and not fmt.Errorf when creating the message.

For example, the output when connecting to an unknown host:

unknown MySQL server host '127.0.0.40:33060' (%!w(*errors.errorString=&{i/o timeout})) [2005:HY000]

That should be:

unknown MySQL server host '127.0.0.40:33060' (i/o timeout) [2005:HY000]

Cod to reproduce:

import (
    "context"
    "errors"
    "fmt"
    "log"
    "time"

    "github.com/golistic/pxmysql/xmysql"
)

func bug4() {
    config := &xmysql.ConnectConfig{
        Address:    "127.0.0.40",
    }

    cnx, err := xmysql.NewConnection(config)
    if err != nil {
        log.Fatal(err)
    }

    ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
    defer cancel()
    _, err = cnx.NewSession(ctx)
    if err != nil {
        fmt.Println("Unwrapped error:", errors.Unwrap(err).Error())
        log.Fatal(err)
    }
}