GeezerVison is showing github.com — src: add error formatting support · lerouxb/node@853086f
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/errors.md +5Lines changed: 5 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -2323,6 +2323,11 @@ than the parent module. Linked modules must share the same context. | 2323 | 2323 |
| 2324 | 2324 | The linker function returned a module for which linking has failed. | 2325 | 2325 |
| | 2326 | +<a id="ERR_VM_MODULE_LINK_FAILURE"></a> | | 2327 | +### `ERR_VM_MODULE_LINK_FAILURE` | | 2328 | + | | 2329 | +The module was unable to be linked due to a failure. | | 2330 | + | 2326 | 2331 | <a id="ERR_VM_MODULE_NOT_MODULE"></a> | 2327 | 2332 | ### `ERR_VM_MODULE_NOT_MODULE` | 2328 | 2333 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/vm/module.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -317,6 +317,8 @@ class SourceTextModule extends Module { | 317 | 317 | throw new ERR_VM_MODULE_DIFFERENT_CONTEXT(); | 318 | 318 | } | 319 | 319 | if (module.status === 'errored') { | | 320 | + // TODO(devsnek): replace with ERR_VM_MODULE_LINK_FAILURE | | 321 | + // and error cause proposal. | 320 | 322 | throw new ERR_VM_MODULE_LINKING_ERRORED(); | 321 | 323 | } | 322 | 324 | if (module.status === 'unlinked') { | |
Collapse file Copy file name to clipboardExpand all lines: src/module_wrap.cc +14-10Lines changed: 14 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -297,7 +297,9 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) { | 297 | 297 | Local<Value> resolve_return_value = | 298 | 298 | maybe_resolve_return_value.ToLocalChecked(); | 299 | 299 | if (!resolve_return_value->IsPromise()) { | 300 | | - env->ThrowError("linking error, expected resolver to return a promise"); | | 300 | + THROW_ERR_VM_MODULE_LINK_FAILURE( | | 301 | + env, "request for '%s' did not return promise", specifier_std); | | 302 | + return; | 301 | 303 | } | 302 | 304 | Local<Promise> resolve_promise = resolve_return_value.As<Promise>(); | 303 | 305 | obj->resolve_cache_[specifier_std].Reset(env->isolate(), resolve_promise); | @@ -497,33 +499,35 @@ MaybeLocal<Module> ModuleWrap::ResolveModuleCallback( | 497 | 499 |
| 498 | 500 | Isolate* isolate = env->isolate(); | 499 | 501 |
| | 502 | + Utf8Value specifier_utf8(isolate, specifier); | | 503 | + std::string specifier_std(*specifier_utf8, specifier_utf8.length()); | | 504 | + | 500 | 505 | ModuleWrap* dependent = GetFromModule(env, referrer); | 501 | 506 | if (dependent == nullptr) { | 502 | | - env->ThrowError("linking error, null dep"); | | 507 | + THROW_ERR_VM_MODULE_LINK_FAILURE( | | 508 | + env, "request for '%s' is from invalid module", specifier_std); | 503 | 509 | return MaybeLocal<Module>(); | 504 | 510 | } | 505 | 511 |
| 506 | | - Utf8Value specifier_utf8(isolate, specifier); | 507 | | - std::string specifier_std(*specifier_utf8, specifier_utf8.length()); | 508 | | - | 509 | 512 | if (dependent->resolve_cache_.count(specifier_std) != 1) { | 510 | | - env->ThrowError("linking error, not in local cache"); | | 513 | + THROW_ERR_VM_MODULE_LINK_FAILURE( | | 514 | + env, "request for '%s' is not in cache", specifier_std); | 511 | 515 | return MaybeLocal<Module>(); | 512 | 516 | } | 513 | 517 |
| 514 | 518 | Local<Promise> resolve_promise = | 515 | 519 | dependent->resolve_cache_[specifier_std].Get(isolate); | 516 | 520 |
| 517 | 521 | if (resolve_promise->State() != Promise::kFulfilled) { | 518 | | - env->ThrowError("linking error, dependency promises must be resolved on " | 519 | | - "instantiate"); | | 522 | + THROW_ERR_VM_MODULE_LINK_FAILURE( | | 523 | + env, "request for '%s' is not yet fulfilled", specifier_std); | 520 | 524 | return MaybeLocal<Module>(); | 521 | 525 | } | 522 | 526 |
| 523 | 527 | Local<Object> module_object = resolve_promise->Result().As<Object>(); | 524 | 528 | if (module_object.IsEmpty() || !module_object->IsObject()) { | 525 | | - env->ThrowError("linking error, expected a valid module object from " | 526 | | - "resolver"); | | 529 | + THROW_ERR_VM_MODULE_LINK_FAILURE( | | 530 | + env, "request for '%s' did not return an object", specifier_std); | 527 | 531 | return MaybeLocal<Module>(); | 528 | 532 | } | 529 | 533 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/node_errors.h +31-19Lines changed: 31 additions & 19 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 |
| 4 | 4 | #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | 5 | 5 |
| | 6 | +#include "debug_utils-inl.h" | 6 | 7 | #include "env.h" | 7 | 8 | #include "v8.h" | 8 | 9 |
| @@ -75,29 +76,40 @@ void OnFatalError(const char* location, const char* message); | 75 | 76 | V(ERR_TLS_INVALID_PROTOCOL_METHOD, TypeError) \ | 76 | 77 | V(ERR_TLS_PSK_SET_IDENTIY_HINT_FAILED, Error) \ | 77 | 78 | V(ERR_VM_MODULE_CACHED_DATA_REJECTED, Error) \ | | 79 | + V(ERR_VM_MODULE_LINK_FAILURE, Error) \ | 78 | 80 | V(ERR_WASI_NOT_STARTED, Error) \ | 79 | 81 | V(ERR_WORKER_INIT_FAILED, Error) \ | 80 | | - V(ERR_PROTO_ACCESS, Error) \ | | 82 | + V(ERR_PROTO_ACCESS, Error) | 81 | 83 |
| 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)...); \ | 99 | 111 | } | 100 | | - ERRORS_WITH_CODE(V) | | 112 | +ERRORS_WITH_CODE(V) | 101 | 113 | #undef V | 102 | 114 |
| 103 | 115 | // Errors with predefined static messages | |
|