Skip to content

Commit d59c6de

Browse files
devsnekdanielleadams
authored andcommittedMar 16, 2021
src: add error formatting support
PR-URL: #37598 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 60d8afa commit d59c6de

File tree

4 files changed

+52
-29
lines changed

4 files changed

+52
-29
lines changed
 

‎doc/api/errors.md

+5
Original file line numberDiff line numberDiff line change
@@ -2313,6 +2313,11 @@ than the parent module. Linked modules must share the same context.
23132313

23142314
The linker function returned a module for which linking has failed.
23152315

2316+
<a id="ERR_VM_MODULE_LINK_FAILURE"></a>
2317+
### `ERR_VM_MODULE_LINK_FAILURE`
2318+
2319+
The module was unable to be linked due to a failure.
2320+
23162321
<a id="ERR_VM_MODULE_NOT_MODULE"></a>
23172322
### `ERR_VM_MODULE_NOT_MODULE`
23182323

‎lib/internal/vm/module.js

+2
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,8 @@ class SourceTextModule extends Module {
317317
throw new ERR_VM_MODULE_DIFFERENT_CONTEXT();
318318
}
319319
if (module.status === 'errored') {
320+
// TODO(devsnek): replace with ERR_VM_MODULE_LINK_FAILURE
321+
// and error cause proposal.
320322
throw new ERR_VM_MODULE_LINKING_ERRORED();
321323
}
322324
if (module.status === 'unlinked') {

‎src/module_wrap.cc

+14-10
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,9 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
291291
Local<Value> resolve_return_value =
292292
maybe_resolve_return_value.ToLocalChecked();
293293
if (!resolve_return_value->IsPromise()) {
294-
env->ThrowError("linking error, expected resolver to return a promise");
294+
THROW_ERR_VM_MODULE_LINK_FAILURE(
295+
env, "request for '%s' did not return promise", specifier_std);
296+
return;
295297
}
296298
Local<Promise> resolve_promise = resolve_return_value.As<Promise>();
297299
obj->resolve_cache_[specifier_std].Reset(env->isolate(), resolve_promise);
@@ -485,33 +487,35 @@ MaybeLocal<Module> ModuleWrap::ResolveCallback(Local<Context> context,
485487

486488
Isolate* isolate = env->isolate();
487489

490+
Utf8Value specifier_utf8(isolate, specifier);
491+
std::string specifier_std(*specifier_utf8, specifier_utf8.length());
492+
488493
ModuleWrap* dependent = GetFromModule(env, referrer);
489494
if (dependent == nullptr) {
490-
env->ThrowError("linking error, null dep");
495+
THROW_ERR_VM_MODULE_LINK_FAILURE(
496+
env, "request for '%s' is from invalid module", specifier_std);
491497
return MaybeLocal<Module>();
492498
}
493499

494-
Utf8Value specifier_utf8(isolate, specifier);
495-
std::string specifier_std(*specifier_utf8, specifier_utf8.length());
496-
497500
if (dependent->resolve_cache_.count(specifier_std) != 1) {
498-
env->ThrowError("linking error, not in local cache");
501+
THROW_ERR_VM_MODULE_LINK_FAILURE(
502+
env, "request for '%s' is not in cache", specifier_std);
499503
return MaybeLocal<Module>();
500504
}
501505

502506
Local<Promise> resolve_promise =
503507
dependent->resolve_cache_[specifier_std].Get(isolate);
504508

505509
if (resolve_promise->State() != Promise::kFulfilled) {
506-
env->ThrowError("linking error, dependency promises must be resolved on "
507-
"instantiate");
510+
THROW_ERR_VM_MODULE_LINK_FAILURE(
511+
env, "request for '%s' is not yet fulfilled", specifier_std);
508512
return MaybeLocal<Module>();
509513
}
510514

511515
Local<Object> module_object = resolve_promise->Result().As<Object>();
512516
if (module_object.IsEmpty() || !module_object->IsObject()) {
513-
env->ThrowError("linking error, expected a valid module object from "
514-
"resolver");
517+
THROW_ERR_VM_MODULE_LINK_FAILURE(
518+
env, "request for '%s' did not return an object", specifier_std);
515519
return MaybeLocal<Module>();
516520
}
517521

‎src/node_errors.h

+31-19
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
55

6+
#include "debug_utils-inl.h"
67
#include "env.h"
78
#include "v8.h"
89

@@ -75,29 +76,40 @@ void OnFatalError(const char* location, const char* message);
7576
V(ERR_TLS_INVALID_PROTOCOL_METHOD, TypeError) \
7677
V(ERR_TLS_PSK_SET_IDENTIY_HINT_FAILED, Error) \
7778
V(ERR_VM_MODULE_CACHED_DATA_REJECTED, Error) \
79+
V(ERR_VM_MODULE_LINK_FAILURE, Error) \
7880
V(ERR_WASI_NOT_STARTED, Error) \
7981
V(ERR_WORKER_INIT_FAILED, Error) \
80-
V(ERR_PROTO_ACCESS, Error) \
82+
V(ERR_PROTO_ACCESS, Error)
8183

82-
#define V(code, type) \
83-
inline v8::Local<v8::Value> code(v8::Isolate* isolate, \
84-
const char* message) { \
85-
v8::Local<v8::String> js_code = OneByteString(isolate, #code); \
86-
v8::Local<v8::String> js_msg = OneByteString(isolate, message); \
87-
v8::Local<v8::Object> e = \
88-
v8::Exception::type(js_msg)->ToObject( \
89-
isolate->GetCurrentContext()).ToLocalChecked(); \
90-
e->Set(isolate->GetCurrentContext(), OneByteString(isolate, "code"), \
91-
js_code).Check(); \
92-
return e; \
93-
} \
94-
inline void THROW_ ## code(v8::Isolate* isolate, const char* message) { \
95-
isolate->ThrowException(code(isolate, message)); \
96-
} \
97-
inline void THROW_ ## code(Environment* env, const char* message) { \
98-
THROW_ ## code(env->isolate(), message); \
84+
#define V(code, type) \
85+
template <typename... Args> \
86+
inline v8::Local<v8::Value> code( \
87+
v8::Isolate* isolate, const char* format, Args&&... args) { \
88+
std::string message = SPrintF(format, std::forward<Args>(args)...); \
89+
v8::Local<v8::String> js_code = OneByteString(isolate, #code); \
90+
v8::Local<v8::String> js_msg = \
91+
OneByteString(isolate, message.c_str(), message.length()); \
92+
v8::Local<v8::Object> e = v8::Exception::type(js_msg) \
93+
->ToObject(isolate->GetCurrentContext()) \
94+
.ToLocalChecked(); \
95+
e->Set(isolate->GetCurrentContext(), \
96+
OneByteString(isolate, "code"), \
97+
js_code) \
98+
.Check(); \
99+
return e; \
100+
} \
101+
template <typename... Args> \
102+
inline void THROW_##code( \
103+
v8::Isolate* isolate, const char* format, Args&&... args) { \
104+
isolate->ThrowException( \
105+
code(isolate, format, std::forward<Args>(args)...)); \
106+
} \
107+
template <typename... Args> \
108+
inline void THROW_##code( \
109+
Environment* env, const char* format, Args&&... args) { \
110+
THROW_##code(env->isolate(), format, std::forward<Args>(args)...); \
99111
}
100-
ERRORS_WITH_CODE(V)
112+
ERRORS_WITH_CODE(V)
101113
#undef V
102114

103115
// Errors with predefined static messages

0 commit comments

Comments
 (0)
Please sign in to comment.