Skip to content

Commit

Permalink
src: handle errors correctly in permission.cc
Browse files Browse the repository at this point in the history
Return an empty `MaybeLocal` to indicate that an exception is pending.

PR-URL: #54541
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
  • Loading branch information
targos committed Oct 2, 2024
1 parent 90ff714 commit 342c324
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions src/permission/permission.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -105,46 +107,43 @@ Permission::Permission() : enabled_(false) {
#undef V
}

Local<Value> CreateAccessDeniedError(Environment* env,
PermissionScope perm,
const std::string_view& res) {
Local<Value> err = ERR_ACCESS_DENIED(env->isolate());
CHECK(err->IsObject());
if (err.As<Object>()
->Set(env->context(),
env->permission_string(),
v8::String::NewFromUtf8(env->isolate(),
Permission::PermissionToString(perm),
v8::NewStringType::kNormal)
.ToLocalChecked())
MaybeLocal<Value> CreateAccessDeniedError(Environment* env,
PermissionScope perm,
const std::string_view& res) {
Local<Object> err = ERR_ACCESS_DENIED(env->isolate());
Local<String> perm_string;
Local<String> 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<Object>()
->Set(env->context(),
env->resource_string(),
v8::String::NewFromUtf8(env->isolate(),
std::string(res).c_str(),
v8::NewStringType::kNormal)
.ToLocalChecked())
.IsNothing())
return Local<Value>();
err->Set(env->context(), env->resource_string(), resource_string)
.IsNothing()) {
return MaybeLocal<Value>();
}
return err;
}

void Permission::ThrowAccessDenied(Environment* env,
PermissionScope perm,
const std::string_view& res) {
Local<Value> err = CreateAccessDeniedError(env, perm, res);
MaybeLocal<Value> 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<Value> err = CreateAccessDeniedError(env, perm, res);
MaybeLocal<Value> err = CreateAccessDeniedError(env, perm, res);
if (err.IsEmpty()) return;
return req_wrap->Reject(err);
return req_wrap->Reject(err.ToLocalChecked());
}

void Permission::EnablePermissions() {
Expand Down

0 comments on commit 342c324

Please sign in to comment.