Skip to content

Commit

Permalink
cleanup some error handling in BunProcess (#14178)
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro authored Sep 26, 2024
1 parent afe974a commit d42c032
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
25 changes: 9 additions & 16 deletions src/bun.js/bindings/BunProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "wtf/text/OrdinalNumber.h"

#include "AsyncContextFrame.h"
#include "ErrorCode.h"

#include "napi_handle_scope.h"

Expand Down Expand Up @@ -442,24 +443,18 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionUmask,
JSValue numberValue = callFrame->argument(0);

if (!numberValue.isNumber()) {
throwTypeError(globalObject, throwScope, "The \"mask\" argument must be a number"_s);
return {};
return Bun::ERR::INVALID_ARG_TYPE(throwScope, globalObject, "mask"_s, "number"_s, numberValue);
}

if (!numberValue.isAnyInt()) {
throwNodeRangeError(globalObject, throwScope, "The \"mask\" argument must be an integer"_s);
return {};
return Bun::ERR::OUT_OF_RANGE(throwScope, globalObject, "mask"_s, "an integer"_s, numberValue);
}

double number = numberValue.toNumber(globalObject);
int64_t newUmask = isInt52(number) ? tryConvertToInt52(number) : numberValue.toInt32(globalObject);
RETURN_IF_EXCEPTION(throwScope, JSC::JSValue::encode(JSC::JSValue {}));
if (newUmask < 0 || newUmask > 4294967295) {
StringBuilder messageBuilder;
messageBuilder.append("The \"mask\" value must be in range [0, 4294967295]. Received value: "_s);
messageBuilder.append(int52ToString(vm, newUmask, 10)->getString(globalObject));
throwNodeRangeError(globalObject, throwScope, messageBuilder.toString());
return {};
return Bun::ERR::OUT_OF_RANGE(throwScope, globalObject, "mask"_s, 0, 4294967295, numberValue);
}

return JSC::JSValue::encode(JSC::jsNumber(umask(newUmask)));
Expand Down Expand Up @@ -2895,11 +2890,11 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionKill,
(JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
{
auto scope = DECLARE_THROW_SCOPE(globalObject->vm());
int pid = callFrame->argument(0).toInt32(globalObject);
auto pid_value = callFrame->argument(0);
int pid = pid_value.toInt32(globalObject);
RETURN_IF_EXCEPTION(scope, {});
if (pid < 0) {
throwNodeRangeError(globalObject, scope, "pid must be a positive integer"_s);
return {};
return Bun::ERR::OUT_OF_RANGE(scope, globalObject, "pid"_s, "a positive integer"_s, pid_value);
}
JSC::JSValue signalValue = callFrame->argument(1);
int signal = SIGTERM;
Expand All @@ -2912,13 +2907,11 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionKill,
signal = num;
RETURN_IF_EXCEPTION(scope, {});
} else {
throwNodeRangeError(globalObject, scope, "Unknown signal name"_s);
return {};
return Bun::ERR::UNKNOWN_SIGNAL(scope, globalObject, signalValue);
}
RETURN_IF_EXCEPTION(scope, {});
} else if (!signalValue.isUndefinedOrNull()) {
throwTypeError(globalObject, scope, "signal must be a string or number"_s);
return {};
return Bun::ERR::INVALID_ARG_TYPE(scope, globalObject, "signal"_s, "string or number"_s, signalValue);
}

auto global = jsCast<Zig::GlobalObject*>(globalObject);
Expand Down
2 changes: 1 addition & 1 deletion test/js/node/process/process.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ it("process.umask()", () => {
for (let notNumber of notNumbers) {
expect(() => {
process.umask(notNumber);
}).toThrow('The "mask" argument must be a number');
}).toThrow('The "mask" argument must be of type number');
}

let rangeErrors = [NaN, -1.4, Infinity, -Infinity, -1, 1.3, 4294967296];
Expand Down

0 comments on commit d42c032

Please sign in to comment.