diff --git a/doc/api/errors.md b/doc/api/errors.md index 5e2b47d8f41051..4032e916904f8e 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -1920,7 +1920,8 @@ The imported module string is an invalid URL, package name, or package subpath specifier. -### ERR_INVALID_OBJECT_DEFINE_PROPERTY + +### ERR\_INVALID\_OBJECT\_DEFINE\_PROPERTY An error occurred while setting an invalid attribute on the property of an object. diff --git a/src/node_env_var.cc b/src/node_env_var.cc index 313eda64954e2b..cdc15348ff949f 100644 --- a/src/node_env_var.cc +++ b/src/node_env_var.cc @@ -401,20 +401,32 @@ static void EnvDefiner(Local property, const PropertyDescriptor& desc, const PropertyCallbackInfo& info) { Environment* env = Environment::GetCurrent(info); - if (desc.has_value() && !desc.configurable() && !desc.enumerable() && - !desc.writable()) { - THROW_ERR_INVALID_OBJECT_DEFINE_PROPERTY(env, - "Must set all attributes with true to 'value'" - " in 'process.env'"); - } else if (desc.has_get() || desc.has_set() || - (desc.has_configurable() && !desc.configurable()) || - (desc.has_enumerable() && !desc.enumerable()) || - (desc.has_writable() && !desc.writable())) { + if (desc.has_value()) { + if (!desc.has_writable() || + !desc.has_enumerable() || + !desc.has_configurable()) { + THROW_ERR_INVALID_OBJECT_DEFINE_PROPERTY(env, + "'process.env' only accepts a " + "configurable, writable," + " and enumerable data descriptor"); + } else if (!desc.configurable() || !desc.enumerable() || !desc.writable()) { + THROW_ERR_INVALID_OBJECT_DEFINE_PROPERTY(env, + "'process.env' only accepts a " + "configurable, writable," + " and enumerable data descriptor"); + } else { + return EnvSetter(property, desc.value(), info); + } + } else if (desc.has_get() || desc.has_set()) { + // we don't accept a getter/setter in 'process.env' THROW_ERR_INVALID_OBJECT_DEFINE_PROPERTY(env, - "Cannot set attributes other than 'value'" - " for properties in 'process.env'"); + "'process.env' does not accept an" + "accessor(getter/setter) descriptor"); } else { - EnvSetter(property, desc.value(), info); + THROW_ERR_INVALID_OBJECT_DEFINE_PROPERTY(env, + "'process.env' only accepts a " + "configurable, writable," + " and enumerable data descriptor"); } } diff --git a/test/parallel/test-process-env-ignore-getter-setter.js b/test/parallel/test-process-env-ignore-getter-setter.js index 3a4338a545500e..7368eb85684c5a 100644 --- a/test/parallel/test-process-env-ignore-getter-setter.js +++ b/test/parallel/test-process-env-ignore-getter-setter.js @@ -11,8 +11,9 @@ assert.throws( { code: 'ERR_INVALID_OBJECT_DEFINE_PROPERTY', name: 'TypeError', - message: 'Must set all attributes with true to \'value\' ' + - 'in \'process.env\'' + message: '\'process.env\' only accepts a ' + + 'configurable, writable,' + + ' and enumerable data descriptor' } ); @@ -32,8 +33,8 @@ assert.throws( { code: 'ERR_INVALID_OBJECT_DEFINE_PROPERTY', name: 'TypeError', - message: 'Cannot set attributes other than \'value\' ' + - 'for properties in \'process.env\'' + message: '\'process.env\' does not accept an' + + 'accessor(getter/setter) descriptor' } ); @@ -49,8 +50,9 @@ attributes.forEach((attribute) => { { code: 'ERR_INVALID_OBJECT_DEFINE_PROPERTY', name: 'TypeError', - message: 'Cannot set attributes other than \'value\' ' + - 'for properties in \'process.env\'' + message: '\'process.env\' only accepts a ' + + 'configurable, writable,' + + ' and enumerable data descriptor' } ); }); diff --git a/test/parallel/test-worker-process-env.js b/test/parallel/test-worker-process-env.js index 2cd1f498575a60..f43c2affa1f48b 100644 --- a/test/parallel/test-worker-process-env.js +++ b/test/parallel/test-worker-process-env.js @@ -48,8 +48,8 @@ if (!workerData && process.argv[2] !== 'child') { { code: 'ERR_INVALID_OBJECT_DEFINE_PROPERTY', name: 'TypeError', - message: 'Must set all attributes with true to \'value\' ' + - 'in \'process.env\'' + message: '\'process.env\' only accepts a configurable, ' + + 'writable, and enumerable data descriptor' } );