tursodatabase / libsql-client-go

Go client API for libSQL
MIT License
159 stars 23 forks source link

[Bug] Handling connection errors when initializing database connection #88

Open samocodes opened 7 months ago

samocodes commented 7 months ago

The code doesn't report an error when the libsql server is unavailable. When I attempt to perform database queries, I encounter the following error message:

2023/11/16 20:22:23 <nil> failed to execute SQL: SELECT * FROM user
Post "http://127.0.0.1:8080/v2/pipeline": dial tcp 127.0.0.1:8080: connect: connection refused

I would expect the code to return an error message indicating that the libsql server is unavailable instead of silently failing.

func InitializeDB() (*sql.DB, error) {
    db, err := sql.Open("libsql", DB_URL)
    if err != nil {
        return nil, fmt.Errorf("failed to open db %s: %w", DB_URL, err)
    }

    err = db.Ping()
    if err != nil {
        return nil, fmt.Errorf("failed to ping database: %w", err)
    }

    return db, nil
}