Skip to content

Commit 1d0ad63

Browse files
joyeecheungjasnell
authored andcommitted
src: migrate ERR_INDEX_OUT_OF_RANGE in C++
This patch migrates the "Index out of range" errors in C++ to errors with the code `ERR_INDEX_OUT_OF_RANGE` which have equivalents in JavaScript. PR-URL: #20121 Reviewed-By: Daniel Bevenius <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]>
1 parent c218854 commit 1d0ad63

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

src/node_buffer.cc

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "node.h"
2323
#include "node_buffer.h"
24+
#include "node_errors.h"
2425

2526
#include "env-inl.h"
2627
#include "string_bytes.h"
@@ -38,7 +39,7 @@
3839

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

4445
#define SLICE_START_END(start_arg, end_arg, end_max) \
@@ -544,7 +545,7 @@ void Copy(const FunctionCallbackInfo<Value> &args) {
544545
return args.GetReturnValue().Set(0);
545546

546547
if (source_start > ts_obj_length)
547-
return env->ThrowRangeError("Index out of range");
548+
return node::THROW_ERR_INDEX_OUT_OF_RANGE(env);
548549

549550
if (source_end - source_start > target_length - target_start)
550551
source_end = source_start + target_length - target_start;
@@ -728,9 +729,9 @@ void CompareOffset(const FunctionCallbackInfo<Value> &args) {
728729
THROW_AND_RETURN_IF_OOB(ParseArrayIndex(args[5], ts_obj_length, &source_end));
729730

730731
if (source_start > ts_obj_length)
731-
return env->ThrowRangeError("Index out of range");
732+
return node::THROW_ERR_INDEX_OUT_OF_RANGE(env);
732733
if (target_start > target_length)
733-
return env->ThrowRangeError("Index out of range");
734+
return node::THROW_ERR_INDEX_OUT_OF_RANGE(env);
734735

735736
CHECK_LE(source_start, source_end);
736737
CHECK_LE(target_start, target_end);

src/node_errors.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ namespace node {
1717
// a `Local<Value>` containing the TypeError with proper code and message
1818

1919
#define ERRORS_WITH_CODE(V) \
20+
V(ERR_INDEX_OUT_OF_RANGE, RangeError) \
2021
V(ERR_MEMORY_ALLOCATION_FAILED, Error) \
21-
V(ERR_STRING_TOO_LONG, Error) \
22+
V(ERR_STRING_TOO_LONG, Error) \
2223
V(ERR_BUFFER_TOO_LARGE, Error)
2324

2425
#define V(code, type) \
@@ -42,6 +43,7 @@ namespace node {
4243
// Errors with predefined static messages
4344

4445
#define PREDEFINED_ERROR_MESSAGES(V) \
46+
V(ERR_INDEX_OUT_OF_RANGE, "Index out of range") \
4547
V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory")
4648

4749
#define V(code, message) \

test/parallel/test-buffer-alloc.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,11 @@ common.expectsError(() => {
982982
const a = Buffer.alloc(1);
983983
const b = Buffer.alloc(1);
984984
a.copy(b, 0, 0x100000000, 0x100000001);
985-
}, { code: undefined, type: RangeError, message: 'Index out of range' });
985+
}, {
986+
code: 'ERR_INDEX_OUT_OF_RANGE',
987+
type: RangeError,
988+
message: 'Index out of range'
989+
});
986990

987991
// Unpooled buffer (replaces SlowBuffer)
988992
{

0 commit comments

Comments
 (0)