Skip to content

Commit 289d152

Browse files
committed
src: add error code helpers to src/node_errors.h
This commit adds node::ERR_*(isolate, message) helpers in the C++ land to assign error codes to existing C++ errors. The following errors are added: - ERR_MEMORY_ALLOCATION_FAILED - ERR_STRING_TOO_LARGE PR-URL: #19739 Fixes: #3175 Fixes: #9489 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 1f29963 commit 289d152

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

doc/api/errors.md

+12
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,12 @@ An attempt was made to open an IPC communication channel with a synchronously
12421242
forked Node.js process. See the documentation for the [`child_process`][] module
12431243
for more information.
12441244

1245+
<a id="ERR_MEMORY_ALLOCATION_FAILED"></a>
1246+
### ERR_MEMORY_ALLOCATION_FAILED
1247+
1248+
An attempt was made to allocate memory (usually in the C++ layer) but it
1249+
failed.
1250+
12451251
<a id="ERR_METHOD_NOT_IMPLEMENTED"></a>
12461252
### ERR_METHOD_NOT_IMPLEMENTED
12471253

@@ -1468,6 +1474,12 @@ additional details.
14681474
A stream method was called that cannot complete because the stream was
14691475
destroyed using `stream.destroy()`.
14701476

1477+
<a id="ERR_STRING_TOO_LARGE"></a>
1478+
### ERR_STRING_TOO_LARGE
1479+
1480+
An attempt has been made to create a string larger than the maximum allowed
1481+
size.
1482+
14711483
<a id="ERR_TLS_CERT_ALTNAME_INVALID"></a>
14721484
### ERR_TLS_CERT_ALTNAME_INVALID
14731485

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@
316316
'src/node_contextify.cc',
317317
'src/node_debug_options.cc',
318318
'src/node_domain.cc',
319+
'src/node_errors.h',
319320
'src/node_file.cc',
320321
'src/node_http2.cc',
321322
'src/node_http_parser.cc',

src/node_errors.h

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#ifndef SRC_NODE_ERRORS_H_
2+
#define SRC_NODE_ERRORS_H_
3+
4+
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5+
6+
#include "node.h"
7+
#include "util-inl.h"
8+
#include "env-inl.h"
9+
#include "v8.h"
10+
11+
namespace node {
12+
13+
// Helpers to construct errors similar to the ones provided by
14+
// lib/internal/errors.js.
15+
// Example: with `V(ERR_INVALID_ARG_TYPE, TypeError)`, there will be
16+
// `node::ERR_INVALID_ARG_TYPE(isolate, "message")` returning
17+
// a `Local<Value>` containing the TypeError with proper code and message
18+
19+
#define ERRORS_WITH_CODE(V) \
20+
V(ERR_MEMORY_ALLOCATION_FAILED, Error) \
21+
V(ERR_STRING_TOO_LARGE, Error) \
22+
V(ERR_BUFFER_TOO_LARGE, Error)
23+
24+
#define V(code, type) \
25+
inline v8::Local<v8::Value> code(v8::Isolate* isolate, \
26+
const char* message) { \
27+
v8::Local<v8::String> js_code = OneByteString(isolate, #code); \
28+
v8::Local<v8::String> js_msg = OneByteString(isolate, message); \
29+
v8::Local<v8::Object> e = \
30+
v8::Exception::type(js_msg)->ToObject( \
31+
isolate->GetCurrentContext()).ToLocalChecked(); \
32+
e->Set(isolate->GetCurrentContext(), OneByteString(isolate, "code"), \
33+
js_code).FromJust(); \
34+
return e; \
35+
}
36+
ERRORS_WITH_CODE(V)
37+
#undef V
38+
39+
// Errors with predefined static messages
40+
41+
#define PREDEFINED_ERROR_MESSAGES(V) \
42+
V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory")
43+
44+
#define V(code, message) \
45+
inline v8::Local<v8::Value> code(v8::Isolate* isolate) { \
46+
return code(isolate, message); \
47+
}
48+
PREDEFINED_ERROR_MESSAGES(V)
49+
#undef V
50+
51+
// Errors with predefined non-static messages
52+
53+
inline v8::Local<v8::Value> ERR_BUFFER_TOO_LARGE(v8::Isolate *isolate) {
54+
char message[128];
55+
snprintf(message, sizeof(message),
56+
"Cannot create a Buffer larger than 0x%lx bytes",
57+
v8::TypedArray::kMaxLength);
58+
return ERR_BUFFER_TOO_LARGE(isolate, message);
59+
}
60+
61+
inline v8::Local<v8::Value> ERR_STRING_TOO_LARGE(v8::Isolate *isolate) {
62+
char message[128];
63+
snprintf(message, sizeof(message),
64+
"Cannot create a string larger than 0x%x bytes",
65+
v8::String::kMaxLength);
66+
return ERR_STRING_TOO_LARGE(isolate, message);
67+
}
68+
69+
} // namespace node
70+
71+
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
72+
73+
#endif // SRC_NODE_ERRORS_H_

0 commit comments

Comments
 (0)