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 offset property to error message. #1779

Open
wants to merge 2 commits into
base: master
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
Empty file added somefile
Empty file.
11 changes: 7 additions & 4 deletions src/database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ void Database::Work_Open(napi_env e, void* data) {

if (baton->status != SQLITE_OK) {
baton->message = std::string(sqlite3_errmsg(db->_handle));
baton->offset = sqlite3_error_offset(db->_handle);
sqlite3_close(db->_handle);
db->_handle = NULL;
}
Expand All @@ -186,7 +187,7 @@ void Database::Work_AfterOpen(napi_env e, napi_status status, void* data) {

Napi::Value argv[1];
if (baton->status != SQLITE_OK) {
EXCEPTION(Napi::String::New(env, baton->message.c_str()), baton->status, exception);
EXCEPTION_WITH_OFFSET(Napi::String::New(env, baton->message.c_str()), baton->status, baton->offset, exception);
argv[0] = exception;
}
else {
Expand Down Expand Up @@ -250,6 +251,7 @@ void Database::Work_Close(napi_env e, void* data) {

if (baton->status != SQLITE_OK) {
baton->message = std::string(sqlite3_errmsg(db->_handle));
baton->offset = sqlite3_error_offset(db->_handle);
}
else {
db->_handle = NULL;
Expand All @@ -269,7 +271,7 @@ void Database::Work_AfterClose(napi_env e, napi_status status, void* data) {

Napi::Value argv[1];
if (baton->status != SQLITE_OK) {
EXCEPTION(Napi::String::New(env, baton->message.c_str()), baton->status, exception);
EXCEPTION_WITH_OFFSET(Napi::String::New(env, baton->message.c_str()), baton->status, baton->offset, exception);
argv[0] = exception;
}
else {
Expand Down Expand Up @@ -592,6 +594,7 @@ void Database::Work_Exec(napi_env e, void* data) {

if (baton->status != SQLITE_OK && message != NULL) {
baton->message = std::string(message);
baton->offset = sqlite3_error_offset(baton->db->_handle);
sqlite3_free(message);
}
}
Expand All @@ -608,7 +611,7 @@ void Database::Work_AfterExec(napi_env e, napi_status status, void* data) {
Napi::Function cb = baton->callback.Value();

if (baton->status != SQLITE_OK) {
EXCEPTION(Napi::String::New(env, baton->message.c_str()), baton->status, exception);
EXCEPTION_WITH_OFFSET(Napi::String::New(env, baton->message.c_str()), baton->status, baton->offset, exception);

if (IS_FUNCTION(cb)) {
Napi::Value argv[] = { exception };
Expand Down Expand Up @@ -716,7 +719,7 @@ void Database::Work_AfterLoadExtension(napi_env e, napi_status status, void* dat
Napi::Function cb = baton->callback.Value();

if (baton->status != SQLITE_OK) {
EXCEPTION(Napi::String::New(env, baton->message.c_str()), baton->status, exception);
EXCEPTION_WITH_OFFSET(Napi::String::New(env, baton->message.c_str()), baton->status, baton->offset, exception);

if (IS_FUNCTION(cb)) {
Napi::Value argv[] = { exception };
Expand Down
1 change: 1 addition & 0 deletions src/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Database : public Napi::ObjectWrap<Database> {
Database* db;
Napi::FunctionReference callback;
int status;
int offset;
std::string message;

Baton(Database* db_, Napi::Function cb_) :
Expand Down
16 changes: 16 additions & 0 deletions src/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ inline bool OtherIsInt(Napi::Number source) {
(name ##_obj).Set( Napi::String::New(env, "code"), \
Napi::String::New(env, sqlite_code_string(errno)));

#define EXCEPTION_WITH_OFFSET(msg, errno, offset, name) \
Napi::Value name = Napi::Error::New(env, \
StringConcat( \
StringConcat( \
Napi::String::New(env, sqlite_code_string(errno)), \
Napi::String::New(env, ": ") \
), \
(msg) \
).Utf8Value() \
).Value(); \
Napi::Object name ##_obj = name.As<Napi::Object>(); \
(name ##_obj).Set( Napi::String::New(env, "errno"), Napi::Number::New(env, errno)); \
(name ##_obj).Set( Napi::String::New(env, "offset"), Napi::Number::New(env, offset)); \
(name ##_obj).Set( Napi::String::New(env, "code"), \
Napi::String::New(env, sqlite_code_string(errno)));


#define EMIT_EVENT(obj, argc, argv) \
TRY_CATCH_CALL((obj), \
Expand Down
7 changes: 6 additions & 1 deletion src/statement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ template <class T> void Statement::Error(T* baton) {

// Fail hard on logic errors.
assert(stmt->status != 0);
EXCEPTION(Napi::String::New(env, stmt->message.c_str()), stmt->status, exception);
EXCEPTION_WITH_OFFSET(Napi::String::New(env, stmt->message.c_str()), stmt->status, stmt->offset, exception);

Napi::Function cb = baton->callback.Value();

Expand Down Expand Up @@ -146,6 +146,7 @@ void Statement::Work_Prepare(napi_env e, void* data) {

if (stmt->status != SQLITE_OK) {
stmt->message = std::string(sqlite3_errmsg(baton->db->_handle));
stmt->offset = sqlite3_error_offset(baton->db->_handle);
stmt->_handle = NULL;
}

Expand Down Expand Up @@ -412,6 +413,7 @@ void Statement::Work_Get(napi_env e, void* data) {

if (!(stmt->status == SQLITE_ROW || stmt->status == SQLITE_DONE)) {
stmt->message = std::string(sqlite3_errmsg(stmt->db->_handle));
stmt->offset = sqlite3_error_offset(stmt->db->_handle);
}
}

Expand Down Expand Up @@ -488,6 +490,7 @@ void Statement::Work_Run(napi_env e, void* data) {

if (!(stmt->status == SQLITE_ROW || stmt->status == SQLITE_DONE)) {
stmt->message = std::string(sqlite3_errmsg(stmt->db->_handle));
stmt->offset = sqlite3_error_offset(stmt->db->_handle);
}
else {
baton->inserted_id = sqlite3_last_insert_rowid(stmt->db->_handle);
Expand Down Expand Up @@ -562,6 +565,7 @@ void Statement::Work_All(napi_env e, void* data) {

if (stmt->status != SQLITE_DONE) {
stmt->message = std::string(sqlite3_errmsg(stmt->db->_handle));
stmt->offset = sqlite3_error_offset(stmt->db->_handle);
}
}

Expand Down Expand Up @@ -671,6 +675,7 @@ void Statement::Work_Each(napi_env e, void* data) {
else {
if (stmt->status != SQLITE_DONE) {
stmt->message = std::string(sqlite3_errmsg(stmt->db->_handle));
stmt->offset = sqlite3_error_offset(stmt->db->_handle);
}
sqlite3_mutex_leave(mtx);
break;
Expand Down
1 change: 1 addition & 0 deletions src/statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ class Statement : public Napi::ObjectWrap<Statement> {

std::queue<Call*> queue;
std::string message;
int offset = -1;
};

}
Expand Down
11 changes: 11 additions & 0 deletions test/database_fail.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ describe('error handling', function() {
});
});

it('should provide an offset in the error object', function(done) {
db.get('SELECT id, txt ROM foo', function(err, row) {
if (err) {
assert.equal(err.offset, 'SELECT id, txt ROM '.length);
done();
} else {
done(new Error('Completed query without error, but expected error'));
}
});
});

it('Database#all prepare fail', function(done) {
db.all('SELECT id, txt FROM foo', function(err, row) {
if (err) {
Expand Down