Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more detailed error message to database open failures. #317

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions c_src/sqlite3_nif.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,15 @@ exqlite_open(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])

rc = sqlite3_open_v2((char*)bin.data, &db, flags, NULL);
if (rc != SQLITE_OK) {
if (db != NULL) {
char error_msg[1024];
const char* msg = sqlite3_errmsg(db);
int code = sqlite3_system_errno(db);
snprintf(error_msg, sizeof(error_msg), "%s (errno: %d)", msg, code);
Comment on lines +324 to +326
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me think about this a little bit more. This changes the method signature from {:error, atom()} to {:error, atom() | String.t()}. I think maybe the better solution would be to do something like {:error, {:database_open_failed, code(), String.t()}}

sqlite3_close_v2(db);
return make_error_tuple(env, make_binary(env, error_msg, strlen(error_msg)));
}

return make_error_tuple(env, am_database_open_failed);
}

Expand Down
7 changes: 7 additions & 0 deletions test/exqlite/sqlite3_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ defmodule Exqlite.Sqlite3Test do
File.rm(path)
end

test "fails opening a database on disk" do
{:ok, path} = Temp.path()
:ok = File.mkdir(path)
{:error, "unable to open database file (errno: " <> _number} = Sqlite3.open(path)
File.rm_rf(path)
end

test "creates database path on disk when non-existent" do
{:ok, path} = Temp.mkdir()
{:ok, conn} = Sqlite3.open(path <> "/non_exist.db")
Expand Down
Loading