Last active
November 22, 2017 21:30
-
-
Save cjbj/78a587855a81edec3fb4dedc8aece522 to your computer and use it in GitHub Desktop.
improve require() failure messages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/oracledb.js b/lib/oracledb.js | |
index 10025481..869a6fb2 100644 | |
--- a/lib/oracledb.js | |
+++ b/lib/oracledb.js | |
@@ -41,28 +41,32 @@ var binaryDebugPath = '../build/Debug/oracledb.node'; | |
try { | |
oracledbCLib = require(binaryReleasePath); | |
} catch (err) { | |
- if (err.code !== 'MODULE_NOT_FOUND') { | |
- throw err; | |
- } else { | |
+ var nodeInfo = process.versions.node + ' (' + process.platform + ', ' + process.arch +')\n'; | |
+ var fullReleasePath = require('path').resolve(__dirname, binaryReleasePath); | |
+ if (err.code === 'MODULE_NOT_FOUND') { | |
try { | |
oracledbCLib = require(binaryDebugPath); | |
} catch (err) { | |
- if (err.code !== 'MODULE_NOT_FOUND') { | |
- throw err; | |
+ if (err.code === 'MODULE_NOT_FOUND') { | |
+ // Neither Release or Debug binary was found but assume users wanted Release binary | |
+ nodeInfo += 'Cannot find module ' + fullReleasePath + '\n' + getInfo(); | |
+ throw new Error(nodbUtil.getErrorMessage('NJS-045', nodeInfo)); | |
} else { | |
- var nodeInfo = process.versions.node + ' (' + process.platform + ', ' + process.arch +')\n'; | |
- var fullPath = require('path').resolve(__dirname, binaryReleasePath); | |
- try { | |
- require('fs').statSync(fullPath); | |
- nodeInfo += getInfo(); // file exists, so get message about Oracle Client | |
- throw new Error(nodbUtil.getErrorMessage('NJS-045', nodeInfo)); | |
- } catch(err) { | |
- // file couldn't be found | |
- nodeInfo += 'Cannot read ' + fullPath + '\n' + err.message; | |
- throw new Error(nodbUtil.getErrorMessage('NJS-045', nodeInfo)); | |
- } | |
+ nodeInfo += 'Cannot load ' + binaryDebugPath + '\n'; | |
+ nodeInfo += 'Node.js require() error was: \n ' + err.message + '\n' + getInfo(); | |
+ throw new Error(nodbUtil.getErrorMessage('NJS-045', nodeInfo)); | |
} | |
} | |
+ } else { | |
+ if (err.message.startsWith('DPI-1047:')) { | |
+ // Release add-on binary loaded OK, but ODPI-C can't load Oracle client | |
+ nodeInfo += 'Node.js require() error was: \n ' + err.message + '\n'; | |
+ nodeInfo += 'Node.js require() mapped to ' + fullReleasePath + '\n' + getInfo(); | |
+ throw new Error(nodbUtil.getErrorMessage('NJS-045', nodeInfo)); | |
+ } else { | |
+ nodeInfo += 'Cannot load ' + fullReleasePath + '\n' + err.message + '\n' + getInfo(); | |
+ throw new Error(nodbUtil.getErrorMessage('NJS-045', nodeInfo)); | |
+ } | |
} | |
} | |
@@ -72,9 +76,10 @@ oracledbCLib.Oracledb.prototype.newLob = function(iLob) { | |
// Return a string with installation usage tips that may be helpful | |
-// (although ODPI-C should catch most cases) | |
function getInfo() { | |
var arch, url, mesg = ''; | |
+ mesg = 'Node-oracledb installation instructions: '; | |
+ mesg += 'https://github.com/oracle/node-oracledb/blob/master/INSTALL.md\n'; | |
if (process.platform === 'linux') { | |
if (process.arch === 'x64') { | |
url = 'http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html\n'; | |
@@ -84,14 +89,13 @@ function getInfo() { | |
arch = '32-bit'; | |
} | |
mesg += 'You must have ' + arch + ' Oracle client libraries in LD_LIBRARY_PATH, or configured with ldconfig.\n'; | |
- mesg += 'If you do not already have libraries, install the Instant Client Basic or Basic Light package from \n'; | |
+ mesg += 'If you do not have Oracle Database on this computer, then install the Instant Client Basic or Basic Light package from \n'; | |
mesg += url; | |
} else if (process.platform === 'darwin') { | |
+ url = 'http://www.oracle.com/technetwork/topics/intel-macsoft-096467.html'; | |
if (process.arch === 'x64') { | |
- url = 'http://www.oracle.com/technetwork/topics/winx64soft-089540.html'; | |
arch = '64-bit'; | |
} else { | |
- url = 'http://www.oracle.com/technetwork/topics/winsoft-085727.html'; | |
arch = '32-bit'; | |
} | |
mesg += 'You must have the ' + arch + ' Oracle Instant Client Basic or Basic Light package in ~/lib or /usr/local/lib\n'; | |
@@ -105,7 +109,7 @@ function getInfo() { | |
arch = '32-bit'; | |
} | |
mesg += 'You must have ' + arch + ' Oracle client libraries in your PATH environment variable.\n'; | |
- mesg += 'If you do not already have libraries, install the Instant Client Basic or Basic Light package from\n'; | |
+ mesg += 'If you do not have Oracle Database on this computer, then install the Instant Client Basic or Basic Light package from\n'; | |
mesg += url; | |
mesg += 'A Microsoft Visual Studio Redistributable suitable for your Oracle client library version must be available.\n'; | |
} else { | |
@@ -116,11 +120,9 @@ function getInfo() { | |
arch = '32-bit'; | |
} | |
mesg += 'You must have ' + arch + ' Oracle client libraries in your operating system library search path.\n'; | |
- mesg += 'If you do not already have libraries, install an Instant Client Basic or Basic Light package from: \n'; | |
+ mesg += 'If you do not have Oracle Database on this computer, then install an Instant Client Basic or Basic Light package from: \n'; | |
mesg += url; | |
} | |
- mesg += 'Node-oracledb installation instructions: '; | |
- mesg += 'https://github.com/oracle/node-oracledb/blob/master/INSTALL.md\n\n'; | |
return mesg; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment