From bdc266269cec8832bc39b098830ff0065a76a532 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Mon, 4 Nov 2024 13:51:19 +0100 Subject: [PATCH] sqlite: improve error handling using MaybeLocal As per James' suggestion, consistently use MaybeLocal and avoid Check() and ToLocalChecked() entirely. Refs: https://github.com/nodejs/node/pull/54687 PR-URL: https://github.com/nodejs/node/pull/55571 Reviewed-By: Colin Ihrig --- src/node_sqlite.cc | 58 +++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/src/node_sqlite.cc b/src/node_sqlite.cc index 742c15233ccd13..3e08ed796e9ccb 100644 --- a/src/node_sqlite.cc +++ b/src/node_sqlite.cc @@ -56,40 +56,56 @@ using v8::Value; } \ } while (0) -inline Local CreateSQLiteError(Isolate* isolate, const char* message) { - Local js_msg = String::NewFromUtf8(isolate, message).ToLocalChecked(); - Local e = Exception::Error(js_msg) - ->ToObject(isolate->GetCurrentContext()) - .ToLocalChecked(); - e->Set(isolate->GetCurrentContext(), - OneByteString(isolate, "code"), - OneByteString(isolate, "ERR_SQLITE_ERROR")) - .Check(); +inline MaybeLocal CreateSQLiteError(Isolate* isolate, + const char* message) { + Local js_msg; + Local e; + if (!String::NewFromUtf8(isolate, message).ToLocal(&js_msg) || + !Exception::Error(js_msg) + ->ToObject(isolate->GetCurrentContext()) + .ToLocal(&e) || + e->Set(isolate->GetCurrentContext(), + OneByteString(isolate, "code"), + OneByteString(isolate, "ERR_SQLITE_ERROR")) + .IsNothing()) { + return MaybeLocal(); + } return e; } -inline Local CreateSQLiteError(Isolate* isolate, sqlite3* db) { +inline MaybeLocal CreateSQLiteError(Isolate* isolate, sqlite3* db) { int errcode = sqlite3_extended_errcode(db); const char* errstr = sqlite3_errstr(errcode); const char* errmsg = sqlite3_errmsg(db); - Local e = CreateSQLiteError(isolate, errmsg); - e->Set(isolate->GetCurrentContext(), - OneByteString(isolate, "errcode"), - Integer::New(isolate, errcode)) - .Check(); - e->Set(isolate->GetCurrentContext(), - OneByteString(isolate, "errstr"), - String::NewFromUtf8(isolate, errstr).ToLocalChecked()) - .Check(); + Local js_errmsg; + Local e; + if (!String::NewFromUtf8(isolate, errstr).ToLocal(&js_errmsg) || + !CreateSQLiteError(isolate, errmsg).ToLocal(&e) || + e->Set(isolate->GetCurrentContext(), + OneByteString(isolate, "errcode"), + Integer::New(isolate, errcode)) + .IsNothing() || + e->Set(isolate->GetCurrentContext(), + OneByteString(isolate, "errstr"), + js_errmsg) + .IsNothing()) { + return MaybeLocal(); + } return e; } inline void THROW_ERR_SQLITE_ERROR(Isolate* isolate, sqlite3* db) { - isolate->ThrowException(CreateSQLiteError(isolate, db)); + Local e; + if (CreateSQLiteError(isolate, db).ToLocal(&e)) { + isolate->ThrowException(e); + } } inline void THROW_ERR_SQLITE_ERROR(Isolate* isolate, const char* message) { - isolate->ThrowException(CreateSQLiteError(isolate, message)); + Local e; + if (CreateSQLiteError(isolate, message).ToLocal(&e)) { + isolate->ThrowException(e); + } } DatabaseSync::DatabaseSync(Environment* env,