Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
process: show detailed error message in process.dlopen()
Browse files Browse the repository at this point in the history
  • Loading branch information
Shigeki Ohtsu authored and bnoordhuis committed Mar 6, 2012
1 parent 39ce942 commit 59c3923
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/node.cc
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/module-loading-error.node
@@ -0,0 +1 @@
foobar
44 changes: 44 additions & 0 deletions test/simple/test-module-loading-error.js
@@ -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);
}

0 comments on commit 59c3923

Please sign in to comment.