-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <me@gus.host> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
- Loading branch information
1 parent
1f29963
commit 289d152
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Value>` 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<v8::Value> code(v8::Isolate* isolate, \ | ||
const char* message) { \ | ||
v8::Local<v8::String> js_code = OneByteString(isolate, #code); \ | ||
v8::Local<v8::String> js_msg = OneByteString(isolate, message); \ | ||
v8::Local<v8::Object> 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<v8::Value> code(v8::Isolate* isolate) { \ | ||
return code(isolate, message); \ | ||
} | ||
PREDEFINED_ERROR_MESSAGES(V) | ||
#undef V | ||
|
||
// Errors with predefined non-static messages | ||
|
||
inline v8::Local<v8::Value> 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<v8::Value> 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_ |