From 59c3923672d33b4936fc9f82099be522b27f398e Mon Sep 17 00:00:00 2001 From: Shigeki Ohtsu Date: Sun, 26 Feb 2012 21:26:09 +0900 Subject: [PATCH] process: show detailed error message in process.dlopen() --- src/node.cc | 28 +++++++++++++-- test/fixtures/module-loading-error.node | 1 + test/simple/test-module-loading-error.js | 44 ++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/module-loading-error.node create mode 100644 test/simple/test-module-loading-error.js diff --git a/src/node.cc b/src/node.cc index ead527d2771..657945e25d6 100644 --- a/src/node.cc +++ b/src/node.cc @@ -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 UVException(int errorno, const char *syscall, @@ -1581,9 +1595,19 @@ Handle 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 exception = Exception::Error( + String::New("Cannot retrieve an error message in process.dlopen")); + return ThrowException(exception); + } +#ifdef __POSIX__ + Local exception = Exception::Error(String::New(dlerror_msg)); +#else // Windows needs to add the filename into the error message Local 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 index 00000000000..323fae03f46 --- /dev/null +++ b/test/fixtures/module-loading-error.node @@ -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 index 00000000000..c1afd58e077 --- /dev/null +++ b/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); +}