Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: assign error codes to more errors thrown in C++ #20121

Closed
wants to merge 10 commits into from
8 changes: 4 additions & 4 deletions src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "module_wrap.h"

#include "env.h"
#include "node_errors.h"
#include "node_url.h"
#include "util-inl.h"
#include "node_internals.h"
Expand Down Expand Up @@ -677,15 +678,14 @@ void ModuleWrap::Resolve(const FunctionCallbackInfo<Value>& args) {
URL url(*url_utf8, url_utf8.length());

if (url.flags() & URL_FLAGS_FAILED) {
env->ThrowError("second argument is not a URL string");
return;
return node::THROW_ERR_INVALID_ARG_TYPE(
env, "second argument is not a URL string");
}

Maybe<URL> result = node::loader::Resolve(env, specifier_std, url);
if (result.IsNothing() || (result.FromJust().flags() & URL_FLAGS_FAILED)) {
std::string msg = "Cannot find module " + specifier_std;
env->ThrowError(msg.c_str());
return;
return node::THROW_ERR_MISSING_MODULE(env, msg.c_str());
}

args.GetReturnValue().Set(result.FromJust().ToObject(env));
Expand Down
21 changes: 13 additions & 8 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "node.h"
#include "node_buffer.h"
#include "node_errors.h"

#include "env-inl.h"
#include "string_bytes.h"
Expand All @@ -36,9 +37,12 @@

#define MIN(a, b) ((a) < (b) ? (a) : (b))

#define THROW_AND_RETURN_UNLESS_BUFFER(env, obj) \
THROW_AND_RETURN_IF_NOT_BUFFER(env, obj, "argument")

#define THROW_AND_RETURN_IF_OOB(r) \
do { \
if (!(r)) return env->ThrowRangeError("Index out of range"); \
if (!(r)) return node::THROW_ERR_INDEX_OUT_OF_RANGE(env); \
} while (0)

#define SLICE_START_END(start_arg, end_arg, end_max) \
Expand Down Expand Up @@ -544,7 +548,7 @@ void Copy(const FunctionCallbackInfo<Value> &args) {
return args.GetReturnValue().Set(0);

if (source_start > ts_obj_length)
return env->ThrowRangeError("Index out of range");
return node::THROW_ERR_INDEX_OUT_OF_RANGE(env);

if (source_end - source_start > target_length - target_start)
source_end = source_start + target_length - target_start;
Expand Down Expand Up @@ -656,17 +660,18 @@ void StringWrite(const FunctionCallbackInfo<Value>& args) {
THROW_AND_RETURN_UNLESS_BUFFER(env, args.This());
SPREAD_BUFFER_ARG(args.This(), ts_obj);

if (!args[0]->IsString())
return env->ThrowTypeError("Argument must be a string");
THROW_AND_RETURN_IF_NOT_STRING(env, args[0], "argument");

Local<String> str = args[0]->ToString(env->context()).ToLocalChecked();

size_t offset;
size_t max_length;

THROW_AND_RETURN_IF_OOB(ParseArrayIndex(args[1], 0, &offset));
if (offset > ts_obj_length)
return env->ThrowRangeError("Offset is out of bounds");
if (offset > ts_obj_length) {
return node::THROW_ERR_BUFFER_OUT_OF_BOUNDS(
env, "\"offset\" is outside of buffer bounds");
}

THROW_AND_RETURN_IF_OOB(ParseArrayIndex(args[2], ts_obj_length - offset,
&max_length));
Expand Down Expand Up @@ -728,9 +733,9 @@ void CompareOffset(const FunctionCallbackInfo<Value> &args) {
THROW_AND_RETURN_IF_OOB(ParseArrayIndex(args[5], ts_obj_length, &source_end));

if (source_start > ts_obj_length)
return env->ThrowRangeError("Index out of range");
return node::THROW_ERR_INDEX_OUT_OF_RANGE(env);
if (target_start > target_length)
return env->ThrowRangeError("Index out of range");
return node::THROW_ERR_INDEX_OUT_OF_RANGE(env);

CHECK_LE(source_start, source_end);
CHECK_LE(target_start, target_end);
Expand Down
Loading