libsql-client-go icon indicating copy to clipboard operation
libsql-client-go copied to clipboard

[Bug] Handling connection errors when initializing database connection

Open ksamirdev opened this issue 1 year ago • 0 comments

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
}

ksamirdev avatar Nov 16 '23 14:11 ksamirdev