From 056b858e57a72c5429d9278bc7b77d1922a2dbe4 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Fri, 27 Oct 2017 16:27:16 -0700 Subject: os: migrate node_os.cc to internal/errors PR-URL: https://github.com/nodejs/node/pull/16567 Reviewed-By: Joyee Cheung Reviewed-By: Michael Dawson --- lib/os.js | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) (limited to 'lib/os.js') diff --git a/lib/os.js b/lib/os.js index 6af5d15cbc..7c07a5b0d3 100644 --- a/lib/os.js +++ b/lib/os.js @@ -27,21 +27,43 @@ const { deprecate } = require('internal/util'); const { getCIDRSuffix } = require('internal/os'); const isWindows = process.platform === 'win32'; +const errors = require('internal/errors'); + const { getCPUs, getFreeMem, - getHomeDirectory, - getHostname, - getInterfaceAddresses, + getHomeDirectory: _getHomeDirectory, + getHostname: _getHostname, + getInterfaceAddresses: _getInterfaceAddresses, getLoadAvg, - getOSRelease, - getOSType, + getOSRelease: _getOSRelease, + getOSType: _getOSType, getTotalMem, - getUserInfo, + getUserInfo: _getUserInfo, getUptime, isBigEndian } = process.binding('os'); +function getCheckedFunction(fn) { + return function checkError(...args) { + const ctx = {}; + const ret = fn(...args, ctx); + if (ret === undefined) { + const err = new errors.SystemError(ctx); + Error.captureStackTrace(err, checkError); + throw err; + } + return ret; + }; +} + +const getHomeDirectory = getCheckedFunction(_getHomeDirectory); +const getHostname = getCheckedFunction(_getHostname); +const getInterfaceAddresses = getCheckedFunction(_getInterfaceAddresses); +const getOSRelease = getCheckedFunction(_getOSRelease); +const getOSType = getCheckedFunction(_getOSType); +const getUserInfo = getCheckedFunction(_getUserInfo); + getFreeMem[Symbol.toPrimitive] = () => getFreeMem(); getHostname[Symbol.toPrimitive] = () => getHostname(); getHomeDirectory[Symbol.toPrimitive] = () => getHomeDirectory(); -- cgit v1.2.3