Skip to content
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);
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