os: migrate node_os.cc to internal/errors
PR-URL: https://github.com/nodejs/node/pull/16567 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
This commit is contained in:
parent
c3dc0e0d75
commit
056b858e57
34
lib/os.js
34
lib/os.js
@ -27,21 +27,43 @@ const { deprecate } = require('internal/util');
|
|||||||
const { getCIDRSuffix } = require('internal/os');
|
const { getCIDRSuffix } = require('internal/os');
|
||||||
const isWindows = process.platform === 'win32';
|
const isWindows = process.platform === 'win32';
|
||||||
|
|
||||||
|
const errors = require('internal/errors');
|
||||||
|
|
||||||
const {
|
const {
|
||||||
getCPUs,
|
getCPUs,
|
||||||
getFreeMem,
|
getFreeMem,
|
||||||
getHomeDirectory,
|
getHomeDirectory: _getHomeDirectory,
|
||||||
getHostname,
|
getHostname: _getHostname,
|
||||||
getInterfaceAddresses,
|
getInterfaceAddresses: _getInterfaceAddresses,
|
||||||
getLoadAvg,
|
getLoadAvg,
|
||||||
getOSRelease,
|
getOSRelease: _getOSRelease,
|
||||||
getOSType,
|
getOSType: _getOSType,
|
||||||
getTotalMem,
|
getTotalMem,
|
||||||
getUserInfo,
|
getUserInfo: _getUserInfo,
|
||||||
getUptime,
|
getUptime,
|
||||||
isBigEndian
|
isBigEndian
|
||||||
} = process.binding('os');
|
} = 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();
|
getFreeMem[Symbol.toPrimitive] = () => getFreeMem();
|
||||||
getHostname[Symbol.toPrimitive] = () => getHostname();
|
getHostname[Symbol.toPrimitive] = () => getHostname();
|
||||||
getHomeDirectory[Symbol.toPrimitive] = () => getHomeDirectory();
|
getHomeDirectory[Symbol.toPrimitive] = () => getHomeDirectory();
|
||||||
|
@ -72,7 +72,9 @@ static void GetHostname(const FunctionCallbackInfo<Value>& args) {
|
|||||||
#else // __MINGW32__
|
#else // __MINGW32__
|
||||||
int errorno = WSAGetLastError();
|
int errorno = WSAGetLastError();
|
||||||
#endif // __POSIX__
|
#endif // __POSIX__
|
||||||
return env->ThrowErrnoException(errorno, "gethostname");
|
CHECK_GE(args.Length(), 1);
|
||||||
|
env->CollectExceptionInfo(args[args.Length() - 1], errorno, "gethostname");
|
||||||
|
return args.GetReturnValue().SetUndefined();
|
||||||
}
|
}
|
||||||
buf[sizeof(buf) - 1] = '\0';
|
buf[sizeof(buf) - 1] = '\0';
|
||||||
|
|
||||||
@ -87,7 +89,9 @@ static void GetOSType(const FunctionCallbackInfo<Value>& args) {
|
|||||||
#ifdef __POSIX__
|
#ifdef __POSIX__
|
||||||
struct utsname info;
|
struct utsname info;
|
||||||
if (uname(&info) < 0) {
|
if (uname(&info) < 0) {
|
||||||
return env->ThrowErrnoException(errno, "uname");
|
CHECK_GE(args.Length(), 1);
|
||||||
|
env->CollectExceptionInfo(args[args.Length() - 1], errno, "uname");
|
||||||
|
return args.GetReturnValue().SetUndefined();
|
||||||
}
|
}
|
||||||
rval = info.sysname;
|
rval = info.sysname;
|
||||||
#else // __MINGW32__
|
#else // __MINGW32__
|
||||||
@ -105,7 +109,9 @@ static void GetOSRelease(const FunctionCallbackInfo<Value>& args) {
|
|||||||
#ifdef __POSIX__
|
#ifdef __POSIX__
|
||||||
struct utsname info;
|
struct utsname info;
|
||||||
if (uname(&info) < 0) {
|
if (uname(&info) < 0) {
|
||||||
return env->ThrowErrnoException(errno, "uname");
|
CHECK_GE(args.Length(), 1);
|
||||||
|
env->CollectExceptionInfo(args[args.Length() - 1], errno, "uname");
|
||||||
|
return args.GetReturnValue().SetUndefined();
|
||||||
}
|
}
|
||||||
# ifdef _AIX
|
# ifdef _AIX
|
||||||
char release[256];
|
char release[256];
|
||||||
@ -242,7 +248,10 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) {
|
|||||||
if (err == UV_ENOSYS) {
|
if (err == UV_ENOSYS) {
|
||||||
return args.GetReturnValue().Set(ret);
|
return args.GetReturnValue().Set(ret);
|
||||||
} else if (err) {
|
} else if (err) {
|
||||||
return env->ThrowUVException(err, "uv_interface_addresses");
|
CHECK_GE(args.Length(), 1);
|
||||||
|
env->CollectUVExceptionInfo(args[args.Length() - 1], errno,
|
||||||
|
"uv_interface_addresses");
|
||||||
|
return args.GetReturnValue().SetUndefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < count; i++) {
|
for (i = 0; i < count; i++) {
|
||||||
@ -319,7 +328,9 @@ static void GetHomeDirectory(const FunctionCallbackInfo<Value>& args) {
|
|||||||
const int err = uv_os_homedir(buf, &len);
|
const int err = uv_os_homedir(buf, &len);
|
||||||
|
|
||||||
if (err) {
|
if (err) {
|
||||||
return env->ThrowUVException(err, "uv_os_homedir");
|
CHECK_GE(args.Length(), 1);
|
||||||
|
env->CollectUVExceptionInfo(args[args.Length() - 1], err, "uv_os_homedir");
|
||||||
|
return args.GetReturnValue().SetUndefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
Local<String> home = String::NewFromUtf8(env->isolate(),
|
Local<String> home = String::NewFromUtf8(env->isolate(),
|
||||||
@ -351,7 +362,10 @@ static void GetUserInfo(const FunctionCallbackInfo<Value>& args) {
|
|||||||
const int err = uv_os_get_passwd(&pwd);
|
const int err = uv_os_get_passwd(&pwd);
|
||||||
|
|
||||||
if (err) {
|
if (err) {
|
||||||
return env->ThrowUVException(err, "uv_os_get_passwd");
|
CHECK_GE(args.Length(), 2);
|
||||||
|
env->CollectUVExceptionInfo(args[args.Length() - 1], err,
|
||||||
|
"uv_os_get_passwd");
|
||||||
|
return args.GetReturnValue().SetUndefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
Local<Value> error;
|
Local<Value> error;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user