diff --git a/doc/api/errors.md b/doc/api/errors.md index 936b1b0cd5d53c..e96848b2960d40 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -1242,6 +1242,12 @@ An attempt was made to open an IPC communication channel with a synchronously forked Node.js process. See the documentation for the [`child_process`][] module for more information. + +### ERR_MEMORY_ALLOCATION_FAILED + +An attempt was made to allocate memory (usually in the C++ layer) but it +failed. + ### ERR_METHOD_NOT_IMPLEMENTED @@ -1468,6 +1474,12 @@ additional details. A stream method was called that cannot complete because the stream was destroyed using `stream.destroy()`. + +### ERR_STRING_TOO_LARGE + +An attempt has been made to create a string larger than the maximum allowed +size. + ### ERR_TLS_CERT_ALTNAME_INVALID diff --git a/node.gyp b/node.gyp index 4bc9a3650f7781..4709e5ec1e2cb6 100644 --- a/node.gyp +++ b/node.gyp @@ -316,6 +316,7 @@ 'src/node_contextify.cc', 'src/node_debug_options.cc', 'src/node_domain.cc', + 'src/node_errors.h', 'src/node_file.cc', 'src/node_http2.cc', 'src/node_http_parser.cc', diff --git a/src/node_errors.h b/src/node_errors.h new file mode 100644 index 00000000000000..8e328ac2f30644 --- /dev/null +++ b/src/node_errors.h @@ -0,0 +1,73 @@ +#ifndef SRC_NODE_ERRORS_H_ +#define SRC_NODE_ERRORS_H_ + +#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS + +#include "node.h" +#include "util-inl.h" +#include "env-inl.h" +#include "v8.h" + +namespace node { + +// Helpers to construct errors similar to the ones provided by +// lib/internal/errors.js. +// Example: with `V(ERR_INVALID_ARG_TYPE, TypeError)`, there will be +// `node::ERR_INVALID_ARG_TYPE(isolate, "message")` returning +// a `Local` containing the TypeError with proper code and message + +#define ERRORS_WITH_CODE(V) \ + V(ERR_MEMORY_ALLOCATION_FAILED, Error) \ + V(ERR_STRING_TOO_LARGE, Error) \ + V(ERR_BUFFER_TOO_LARGE, Error) + +#define V(code, type) \ + inline v8::Local code(v8::Isolate* isolate, \ + const char* message) { \ + v8::Local js_code = OneByteString(isolate, #code); \ + v8::Local js_msg = OneByteString(isolate, message); \ + v8::Local e = \ + v8::Exception::type(js_msg)->ToObject( \ + isolate->GetCurrentContext()).ToLocalChecked(); \ + e->Set(isolate->GetCurrentContext(), OneByteString(isolate, "code"), \ + js_code).FromJust(); \ + return e; \ + } + ERRORS_WITH_CODE(V) +#undef V + +// Errors with predefined static messages + +#define PREDEFINED_ERROR_MESSAGES(V) \ + V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") + +#define V(code, message) \ + inline v8::Local code(v8::Isolate* isolate) { \ + return code(isolate, message); \ + } + PREDEFINED_ERROR_MESSAGES(V) +#undef V + +// Errors with predefined non-static messages + +inline v8::Local ERR_BUFFER_TOO_LARGE(v8::Isolate *isolate) { + char message[128]; + snprintf(message, sizeof(message), + "Cannot create a Buffer larger than 0x%lx bytes", + v8::TypedArray::kMaxLength); + return ERR_BUFFER_TOO_LARGE(isolate, message); +} + +inline v8::Local ERR_STRING_TOO_LARGE(v8::Isolate *isolate) { + char message[128]; + snprintf(message, sizeof(message), + "Cannot create a string larger than 0x%x bytes", + v8::String::kMaxLength); + return ERR_STRING_TOO_LARGE(isolate, message); +} + +} // namespace node + +#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS + +#endif // SRC_NODE_ERRORS_H_