diff --git a/src/permission/permission.cc b/src/permission/permission.cc index ad393df0e38976..7c84aa044dbbf5 100644 --- a/src/permission/permission.cc +++ b/src/permission/permission.cc @@ -18,6 +18,8 @@ namespace node { using v8::Context; using v8::FunctionCallbackInfo; using v8::Local; +using v8::MaybeLocal; +using v8::NewStringType; using v8::Object; using v8::String; using v8::Value; @@ -105,46 +107,43 @@ Permission::Permission() : enabled_(false) { #undef V } -Local CreateAccessDeniedError(Environment* env, - PermissionScope perm, - const std::string_view& res) { - Local err = ERR_ACCESS_DENIED(env->isolate()); - CHECK(err->IsObject()); - if (err.As() - ->Set(env->context(), - env->permission_string(), - v8::String::NewFromUtf8(env->isolate(), - Permission::PermissionToString(perm), - v8::NewStringType::kNormal) - .ToLocalChecked()) +MaybeLocal CreateAccessDeniedError(Environment* env, + PermissionScope perm, + const std::string_view& res) { + Local err = ERR_ACCESS_DENIED(env->isolate()); + Local perm_string; + Local resource_string; + if (!String::NewFromUtf8(env->isolate(), + Permission::PermissionToString(perm), + NewStringType::kNormal) + .ToLocal(&perm_string) || + !String::NewFromUtf8( + env->isolate(), std::string(res).c_str(), NewStringType::kNormal) + .ToLocal(&resource_string) || + err->Set(env->context(), env->permission_string(), perm_string) .IsNothing() || - err.As() - ->Set(env->context(), - env->resource_string(), - v8::String::NewFromUtf8(env->isolate(), - std::string(res).c_str(), - v8::NewStringType::kNormal) - .ToLocalChecked()) - .IsNothing()) - return Local(); + err->Set(env->context(), env->resource_string(), resource_string) + .IsNothing()) { + return MaybeLocal(); + } return err; } void Permission::ThrowAccessDenied(Environment* env, PermissionScope perm, const std::string_view& res) { - Local err = CreateAccessDeniedError(env, perm, res); + MaybeLocal err = CreateAccessDeniedError(env, perm, res); if (err.IsEmpty()) return; - env->isolate()->ThrowException(err); + env->isolate()->ThrowException(err.ToLocalChecked()); } void Permission::AsyncThrowAccessDenied(Environment* env, fs::FSReqBase* req_wrap, PermissionScope perm, const std::string_view& res) { - Local err = CreateAccessDeniedError(env, perm, res); + MaybeLocal err = CreateAccessDeniedError(env, perm, res); if (err.IsEmpty()) return; - return req_wrap->Reject(err); + return req_wrap->Reject(err.ToLocalChecked()); } void Permission::EnablePermissions() {