process: show detailed error message in process.dlopen()
authorShigeki Ohtsu <ohtsu@iij.ad.jp>
Sun, 26 Feb 2012 12:26:09 +0000 (21:26 +0900)
committerBen Noordhuis <info@bnoordhuis.nl>
Tue, 6 Mar 2012 00:34:39 +0000 (01:34 +0100)
src/node.cc
test/fixtures/module-loading-error.node [new file with mode: 0644]
test/simple/test-module-loading-error.js [new file with mode: 0644]

index ead527d..657945e 100644 (file)
@@ -814,6 +814,20 @@ static const char* get_uv_errno_message(int errorno) {
 }
 
 
+static bool get_uv_dlerror_message(uv_lib_t lib, char* error_msg, int size) {
+  int r;
+  const char *msg;
+  if ((msg = uv_dlerror(lib)) == NULL) {
+    r = snprintf(error_msg, size, "%s", "Unable to load shared library ");
+  } else {
+    r = snprintf(error_msg, size, "%s", msg);
+    uv_dlerror_free(lib, msg);
+  }
+  // return bool if the error message be written correctly
+  return (0 < r && r < size);
+}
+
+
 // hack alert! copy of ErrnoException, tuned for uv errors
 Local<Value> UVException(int errorno,
                          const char *syscall,
@@ -1581,9 +1595,19 @@ Handle<Value> DLOpen(const v8::Arguments& args) {
 
   err = uv_dlopen(*filename, &lib);
   if (err.code != UV_OK) {
+    // Retrieve uv_dlerror() message and throw exception with it
+    char dlerror_msg[1024];
+    if (!get_uv_dlerror_message(lib, dlerror_msg, sizeof dlerror_msg)) {
+      Local<Value> exception = Exception::Error(
+          String::New("Cannot retrieve an error message in process.dlopen"));
+      return ThrowException(exception);
+    }
+#ifdef __POSIX__
+    Local<Value> exception = Exception::Error(String::New(dlerror_msg));
+#else  // Windows needs to add the filename into the error message
     Local<Value> exception = Exception::Error(
-        String::Concat(String::New("Unable to load shared library "),
-        args[0]->ToString()));
+        String::Concat(String::New(dlerror_msg), args[0]->ToString()));
+#endif
     return ThrowException(exception);
   }
 
diff --git a/test/fixtures/module-loading-error.node b/test/fixtures/module-loading-error.node
new file mode 100644 (file)
index 0000000..323fae0
--- /dev/null
@@ -0,0 +1 @@
+foobar
diff --git a/test/simple/test-module-loading-error.js b/test/simple/test-module-loading-error.js
new file mode 100644 (file)
index 0000000..c1afd58
--- /dev/null
@@ -0,0 +1,44 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+var common = require('../common');
+var assert = require('assert');
+
+common.debug('load test-module-loading-error.js');
+
+var error_desc = {
+  win32: '%1 is not a valid Win32 application',
+  linux: 'file too short',
+  sunos: 'unknown file type'
+};
+
+var dlerror_msg = error_desc[process.platform];
+
+if (!dlerror_msg) {
+  console.error('Skipping test, platform not supported.');
+  process.exit();
+}
+
+try {
+  require('../fixtures/module-loading-error.node');
+} catch (e) {
+  assert.notEqual(e.toString().indexOf(dlerror_msg), -1);
+}