Skip to content

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

@ksamirdev

Description

@ksamirdev

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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions