Skip to content

Commit 526216d

Browse files
committed
sqlite: refactor authz error handling
1 parent b51043c commit 526216d

File tree

2 files changed

+11
-46
lines changed

2 files changed

+11
-46
lines changed

src/node_sqlite.cc

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -233,28 +233,6 @@ inline void THROW_ERR_SQLITE_ERROR(Isolate* isolate, DatabaseSync* db) {
233233
}
234234
}
235235

236-
bool DatabaseSync::HasPendingAuthorizerError() const {
237-
Local<Value> error =
238-
object()->GetInternalField(kPendingAuthorizerError).As<Value>();
239-
return !error.IsEmpty() && !error->IsUndefined();
240-
}
241-
242-
void DatabaseSync::StoreAuthorizerError(Local<Value> error) {
243-
if (!HasPendingAuthorizerError()) {
244-
object()->SetInternalField(kPendingAuthorizerError, error);
245-
}
246-
}
247-
248-
void DatabaseSync::RethrowPendingAuthorizerError() {
249-
if (HasPendingAuthorizerError()) {
250-
Local<Value> error =
251-
object()->GetInternalField(kPendingAuthorizerError).As<Value>();
252-
object()->SetInternalField(kPendingAuthorizerError,
253-
Undefined(env()->isolate()));
254-
env()->isolate()->ThrowException(error);
255-
}
256-
}
257-
258236
inline void THROW_ERR_SQLITE_ERROR(Isolate* isolate, const char* message) {
259237
Local<Object> e;
260238
if (CreateSQLiteError(isolate, message).ToLocal(&e)) {
@@ -1149,14 +1127,6 @@ void DatabaseSync::Prepare(const FunctionCallbackInfo<Value>& args) {
11491127
sqlite3_stmt* s = nullptr;
11501128
int r = sqlite3_prepare_v2(db->connection_, *sql, -1, &s, 0);
11511129

1152-
if (db->HasPendingAuthorizerError()) {
1153-
db->RethrowPendingAuthorizerError();
1154-
if (s != nullptr) {
1155-
sqlite3_finalize(s);
1156-
}
1157-
return;
1158-
}
1159-
11601130
CHECK_ERROR_OR_THROW(env->isolate(), db, r, SQLITE_OK, void());
11611131
BaseObjectPtr<StatementSync> stmt =
11621132
StatementSync::Create(env, BaseObjectPtr<DatabaseSync>(db), s);
@@ -1178,10 +1148,6 @@ void DatabaseSync::Exec(const FunctionCallbackInfo<Value>& args) {
11781148

11791149
Utf8Value sql(env->isolate(), args[0].As<String>());
11801150
int r = sqlite3_exec(db->connection_, *sql, nullptr, nullptr, nullptr);
1181-
if (db->HasPendingAuthorizerError()) {
1182-
db->RethrowPendingAuthorizerError();
1183-
return;
1184-
}
11851151
CHECK_ERROR_OR_THROW(env->isolate(), db, r, SQLITE_OK, void());
11861152
}
11871153

@@ -1957,19 +1923,19 @@ int DatabaseSync::AuthorizerCallback(void* user_data,
19571923
js_argv.emplace_back(
19581924
NullableSQLiteStringToValue(isolate, param4).ToLocalChecked());
19591925

1960-
TryCatch try_catch(isolate);
19611926
MaybeLocal<Value> retval = callback->Call(
19621927
context, Undefined(isolate), js_argv.size(), js_argv.data());
19631928

1964-
if (try_catch.HasCaught()) {
1965-
db->StoreAuthorizerError(try_catch.Exception());
1929+
Local<Value> result;
1930+
1931+
if (!retval.ToLocal(&result)) {
1932+
db->SetIgnoreNextSQLiteError(true);
19661933
return SQLITE_DENY;
19671934
}
19681935

19691936
Local<String> error_message;
1970-
Local<Value> result;
1971-
if (!retval.ToLocal(&result) || result->IsUndefined() || result->IsNull() ||
1972-
!result->IsInt32()) {
1937+
1938+
if (!result->IsInt32()) {
19731939
if (!String::NewFromUtf8(
19741940
isolate,
19751941
"Authorizer callback must return an integer authorization code")
@@ -1978,7 +1944,8 @@ int DatabaseSync::AuthorizerCallback(void* user_data,
19781944
}
19791945

19801946
Local<Value> err = Exception::TypeError(error_message);
1981-
db->StoreAuthorizerError(err);
1947+
isolate->ThrowException(err);
1948+
db->SetIgnoreNextSQLiteError(true);
19821949
return SQLITE_DENY;
19831950
}
19841951

@@ -1993,7 +1960,8 @@ int DatabaseSync::AuthorizerCallback(void* user_data,
19931960
}
19941961

19951962
Local<Value> err = Exception::RangeError(error_message);
1996-
db->StoreAuthorizerError(err);
1963+
isolate->ThrowException(err);
1964+
db->SetIgnoreNextSQLiteError(true);
19971965
return SQLITE_DENY;
19981966
}
19991967

src/node_sqlite.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ class DatabaseSync : public BaseObject {
113113
public:
114114
enum InternalFields {
115115
kAuthorizerCallback = BaseObject::kInternalFieldCount,
116-
kPendingAuthorizerError,
117116
kInternalFieldCount
118117
};
119118

@@ -172,9 +171,7 @@ class DatabaseSync : public BaseObject {
172171
void SetIgnoreNextSQLiteError(bool ignore);
173172
bool ShouldIgnoreSQLiteError();
174173

175-
bool HasPendingAuthorizerError() const;
176-
void StoreAuthorizerError(v8::Local<v8::Value> error);
177-
void RethrowPendingAuthorizerError();
174+
178175

179176
SET_MEMORY_INFO_NAME(DatabaseSync)
180177
SET_SELF_SIZE(DatabaseSync)

0 commit comments

Comments
 (0)