synchronize all impls of ERR_INVALID_ARG_TYPE (#17158)

This commit is contained in:
Meghan Denny
2025-02-25 15:35:14 -08:00
committed by GitHub
parent 2f48282cbd
commit 2db9ab4c72
12 changed files with 71 additions and 65 deletions

View File

@@ -16,6 +16,7 @@
#include "JavaScriptCore/JSType.h"
#include "JavaScriptCore/Symbol.h"
#include "wtf/Assertions.h"
#include "wtf/Vector.h"
#include "wtf/text/ASCIIFastPath.h"
#include "wtf/text/ASCIILiteral.h"
#include "wtf/text/MakeString.h"
@@ -464,12 +465,56 @@ extern "C" BunString Bun__ErrorCode__determineSpecificType(JSC::JSGlobalObject*
namespace Message {
void addList(WTF::StringBuilder& result, WTF::Vector<WTF::String>& types)
{
switch (types.size()) {
case 0:
return;
case 1:
result.append(types.at(0));
return;
case 2:
result.append(types.at(0));
result.append(" or "_s);
result.append(types.at(1));
return;
case 3:
result.append(types.at(0));
result.append(", "_s);
result.append(types.at(1));
result.append(", or "_s);
result.append(types.at(2));
return;
default: {
for (unsigned i = 0; i < types.size() - 1; i++) {
result.append(types.at(i));
result.append(", "_s);
}
result.append("or "_s);
result.append(types.at(types.size() - 1));
return;
}
}
}
void addParameter(WTF::StringBuilder& result, const StringView& arg_name)
{
if (arg_name.endsWith(" argument"_s)) {
result.append(arg_name);
} else {
result.append("\""_s);
result.append(arg_name);
result.append("\" "_s);
result.append(arg_name.contains('.') ? "property"_s : "argument"_s);
}
}
WTF::String ERR_INVALID_ARG_TYPE(JSC::ThrowScope& scope, JSC::JSGlobalObject* globalObject, const StringView& arg_name, const StringView& expected_type, JSValue actual_value)
{
WTF::StringBuilder result;
result.append("The \""_s);
result.append(arg_name);
result.append("\" argument must be of type "_s);
result.append("The "_s);
addParameter(result, arg_name);
result.append(" must be of type "_s);
result.append(expected_type);
result.append(". Received "_s);
determineSpecificType(JSC::getVM(globalObject), globalObject, result, actual_value);
@@ -482,15 +527,9 @@ WTF::String ERR_INVALID_ARG_TYPE(JSC::ThrowScope& scope, JSC::JSGlobalObject* gl
WTF::StringBuilder result;
result.append("The "_s);
if (arg_name.endsWith(" argument"_s)) {
result.append(arg_name);
} else {
result.append("\""_s);
result.append(arg_name);
result.append("\" argument"_s);
}
result.append(" must be of type "_s);
addParameter(result, arg_name);
result.append(" must be "_s);
result.append("of type "_s);
unsigned length = expected_types.size();
if (length == 1) {
@@ -582,24 +621,8 @@ namespace ERR {
JSC::EncodedJSValue INVALID_ARG_TYPE(JSC::ThrowScope& throwScope, JSC::JSGlobalObject* globalObject, const WTF::String& arg_name, const WTF::String& expected_type, JSC::JSValue val_actual_value)
{
WTF::StringBuilder builder;
auto arg_kind = arg_name.contains('.') ? "property"_s : "argument"_s;
auto ty_first_char = expected_type[0];
auto ty_kind = ty_first_char >= 'A' && ty_first_char <= 'Z' ? "an instance of"_s : "of type"_s;
builder.append("The \""_s);
builder.append(arg_name);
builder.append("\" "_s);
builder.append(arg_kind);
builder.append(" must be "_s);
builder.append(ty_kind);
builder.append(" "_s);
builder.append(expected_type);
builder.append(". Received "_s);
determineSpecificType(globalObject->vm(), globalObject, builder, val_actual_value);
RETURN_IF_EXCEPTION(throwScope, {});
throwScope.throwException(globalObject, createError(globalObject, ErrorCode::ERR_INVALID_ARG_TYPE, builder.toString()));
auto message = Message::ERR_INVALID_ARG_TYPE(throwScope, globalObject, arg_name, expected_type, val_actual_value);
throwScope.throwException(globalObject, createError(globalObject, ErrorCode::ERR_INVALID_ARG_TYPE, message));
return {};
}
@@ -609,25 +632,8 @@ JSC::EncodedJSValue INVALID_ARG_TYPE(JSC::ThrowScope& throwScope, JSC::JSGlobalO
RETURN_IF_EXCEPTION(throwScope, {});
auto arg_name = jsString->view(globalObject);
RETURN_IF_EXCEPTION(throwScope, {});
WTF::StringBuilder builder;
auto arg_kind = arg_name->contains('.') ? "property"_s : "argument"_s;
auto ty_first_char = expected_type[0];
auto ty_kind = ty_first_char >= 'A' && ty_first_char <= 'Z' ? "an instance of"_s : "of type"_s;
builder.append("The \""_s);
builder.append(arg_name);
builder.append("\" "_s);
builder.append(arg_kind);
builder.append(" must be "_s);
builder.append(ty_kind);
builder.append(" "_s);
builder.append(expected_type);
builder.append(". Received "_s);
determineSpecificType(globalObject->vm(), globalObject, builder, val_actual_value);
RETURN_IF_EXCEPTION(throwScope, {});
throwScope.throwException(globalObject, createError(globalObject, ErrorCode::ERR_INVALID_ARG_TYPE, builder.toString()));
auto message = Message::ERR_INVALID_ARG_TYPE(throwScope, globalObject, arg_name, expected_type, val_actual_value);
throwScope.throwException(globalObject, createError(globalObject, ErrorCode::ERR_INVALID_ARG_TYPE, message));
return {};
}