Skip to content

Commit

Permalink
src: add error formatting support
Browse files Browse the repository at this point in the history
  • Loading branch information
devsnek committed Mar 4, 2021
1 parent 3b2863d commit db938cf
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 29 deletions.
28 changes: 18 additions & 10 deletions src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,11 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
Local<Value> resolve_return_value =
maybe_resolve_return_value.ToLocalChecked();
if (!resolve_return_value->IsPromise()) {
env->ThrowError("linking error, expected resolver to return a promise");
THROW_ERR_VM_MODULE_LINK_FAILURE(
env,
"request for '%s' did not return promise",
specifier_std.c_str());
return;
}
Local<Promise> resolve_promise = resolve_return_value.As<Promise>();
obj->resolve_cache_[specifier_std].Reset(env->isolate(), resolve_promise);
Expand Down Expand Up @@ -497,33 +501,37 @@ MaybeLocal<Module> ModuleWrap::ResolveModuleCallback(

Isolate* isolate = env->isolate();

Utf8Value specifier_utf8(isolate, specifier);
std::string specifier_std(*specifier_utf8, specifier_utf8.length());

ModuleWrap* dependent = GetFromModule(env, referrer);
if (dependent == nullptr) {
env->ThrowError("linking error, null dep");
THROW_ERR_VM_MODULE_LINK_FAILURE(
env, "request for '%s' is from invalid module", specifier_std.c_str());
return MaybeLocal<Module>();
}

Utf8Value specifier_utf8(isolate, specifier);
std::string specifier_std(*specifier_utf8, specifier_utf8.length());

if (dependent->resolve_cache_.count(specifier_std) != 1) {
env->ThrowError("linking error, not in local cache");
THROW_ERR_VM_MODULE_LINK_FAILURE(
env, "request for '%s' is not in cache", specifier_std.c_str());
return MaybeLocal<Module>();
}

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

if (resolve_promise->State() != Promise::kFulfilled) {
env->ThrowError("linking error, dependency promises must be resolved on "
"instantiate");
THROW_ERR_VM_MODULE_LINK_FAILURE(
env, "request for '%s' is not yet fulfilled", specifier_std.c_str());
return MaybeLocal<Module>();
}

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

Expand Down
58 changes: 39 additions & 19 deletions src/node_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// Use ostringstream to print exact-width integer types
// because the format specifiers are not available on AIX.
#include <sstream>
#include <cstdarg>

namespace node {

Expand Down Expand Up @@ -75,29 +76,48 @@ void OnFatalError(const char* location, const char* message);
V(ERR_TLS_INVALID_PROTOCOL_METHOD, TypeError) \
V(ERR_TLS_PSK_SET_IDENTIY_HINT_FAILED, Error) \
V(ERR_VM_MODULE_CACHED_DATA_REJECTED, Error) \
V(ERR_VM_MODULE_LINK_FAILURE, Error) \
V(ERR_WASI_NOT_STARTED, Error) \
V(ERR_WORKER_INIT_FAILED, Error) \
V(ERR_PROTO_ACCESS, Error) \
V(ERR_PROTO_ACCESS, 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).Check(); \
return e; \
} \
inline void THROW_ ## code(v8::Isolate* isolate, const char* message) { \
isolate->ThrowException(code(isolate, message)); \
} \
inline void THROW_ ## code(Environment* env, const char* message) { \
THROW_ ## code(env->isolate(), message); \
#define V(code, type) \
inline v8::Local<v8::Value> code( \
v8::Isolate* isolate, const char* message, va_list args) { \
v8::Local<v8::String> js_code = OneByteString(isolate, #code); \
char buffer[256]; \
vsprintf(buffer, message, args); \
v8::Local<v8::String> js_msg = OneByteString(isolate, buffer); \
v8::Local<v8::Object> e = v8::Exception::type(js_msg) \
->ToObject(isolate->GetCurrentContext()) \
.ToLocalChecked(); \
e->Set(isolate->GetCurrentContext(), \
OneByteString(isolate, "code"), \
js_code) \
.Check(); \
return e; \
} \
inline v8::Local<v8::Value> code( \
v8::Isolate* isolate, const char* message, ...) { \
va_list args; \
va_start(args, message); \
v8::Local<v8::Value> e = code(isolate, message, args); \
va_end(args); \
return e; \
} \
inline void THROW_##code(v8::Isolate* isolate, const char* message, ...) { \
va_list args; \
va_start(args, message); \
isolate->ThrowException(code(isolate, message, args)); \
va_end(args); \
} \
inline void THROW_##code(Environment* env, const char* message, ...) { \
va_list args; \
va_start(args, message); \
env->isolate()->ThrowException(code(env->isolate(), message, args)); \
va_end(args); \
}
ERRORS_WITH_CODE(V)
ERRORS_WITH_CODE(V)
#undef V

// Errors with predefined static messages
Expand Down

0 comments on commit db938cf

Please sign in to comment.