Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errors: move error creation helpers to internal/errors.js #18546

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ const SEND_BUFFER = false;
// Lazily loaded
var cluster = null;

const errnoException = util._errnoException;
const exceptionWithHostPort = util._exceptionWithHostPort;
const errnoException = errors.errnoException;
const exceptionWithHostPort = errors.exceptionWithHostPort;


function lookup4(lookup, address, callback) {
Expand Down
47 changes: 8 additions & 39 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,10 @@

'use strict';

const util = require('util');

const cares = process.binding('cares_wrap');
const { isIP, isIPv4, isLegalPort } = require('internal/net');
const { customPromisifyArgs } = require('internal/util');
const errors = require('internal/errors');
const {
UV_EAI_MEMORY,
UV_EAI_NODATA,
UV_EAI_NONAME
} = process.binding('uv');

const {
GetAddrInfoReqWrap,
Expand All @@ -40,36 +33,12 @@ const {
ChannelWrap,
} = cares;

function errnoException(err, syscall, hostname) {
// FIXME(bnoordhuis) Remove this backwards compatibility nonsense and pass
// the true error to the user. ENOTFOUND is not even a proper POSIX error!
if (err === UV_EAI_MEMORY ||
err === UV_EAI_NODATA ||
err === UV_EAI_NONAME) {
err = 'ENOTFOUND';
}
var ex = null;
if (typeof err === 'string') { // c-ares error code.
const errHost = hostname ? ` ${hostname}` : '';
ex = new Error(`${syscall} ${err}${errHost}`);
ex.code = err;
ex.errno = err;
ex.syscall = syscall;
} else {
ex = util._errnoException(err, syscall);
}
if (hostname) {
ex.hostname = hostname;
}
return ex;
}

const IANA_DNS_PORT = 53;

const dnsException = errors.dnsException;

function onlookup(err, addresses) {
if (err) {
return this.callback(errnoException(err, 'getaddrinfo', this.hostname));
return this.callback(dnsException(err, 'getaddrinfo', this.hostname));
}
if (this.family) {
this.callback(null, addresses[0], this.family);
Expand All @@ -81,7 +50,7 @@ function onlookup(err, addresses) {

function onlookupall(err, addresses) {
if (err) {
return this.callback(errnoException(err, 'getaddrinfo', this.hostname));
return this.callback(dnsException(err, 'getaddrinfo', this.hostname));
}

var family = this.family;
Expand Down Expand Up @@ -161,7 +130,7 @@ function lookup(hostname, options, callback) {

var err = cares.getaddrinfo(req, hostname, family, hints, verbatim);
if (err) {
process.nextTick(callback, errnoException(err, 'getaddrinfo', hostname));
process.nextTick(callback, dnsException(err, 'getaddrinfo', hostname));
return {};
}
return req;
Expand All @@ -173,7 +142,7 @@ Object.defineProperty(lookup, customPromisifyArgs,

function onlookupservice(err, host, service) {
if (err)
return this.callback(errnoException(err, 'getnameinfo', this.host));
return this.callback(dnsException(err, 'getnameinfo', this.host));

this.callback(null, host, service);
}
Expand Down Expand Up @@ -202,7 +171,7 @@ function lookupService(host, port, callback) {
req.oncomplete = onlookupservice;

var err = cares.getnameinfo(req, host, port);
if (err) throw errnoException(err, 'getnameinfo', host);
if (err) throw dnsException(err, 'getnameinfo', host);
return req;
}

Expand All @@ -215,7 +184,7 @@ function onresolve(err, result, ttls) {
result = result.map((address, index) => ({ address, ttl: ttls[index] }));

if (err)
this.callback(errnoException(err, this.bindingName, this.hostname));
this.callback(dnsException(err, this.bindingName, this.hostname));
else
this.callback(null, result);
}
Expand Down Expand Up @@ -253,7 +222,7 @@ function resolver(bindingName) {
req.oncomplete = onresolve;
req.ttl = !!(options && options.ttl);
var err = this._handle[bindingName](req, name);
if (err) throw errnoException(err, bindingName);
if (err) throw dnsException(err, bindingName);
return req;
}
Object.defineProperty(query, 'name', { value: bindingName });
Expand Down
40 changes: 20 additions & 20 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const { kMaxLength } = require('buffer');
const isWindows = process.platform === 'win32';

const DEBUG = process.env.NODE_DEBUG && /fs/.test(process.env.NODE_DEBUG);
const errnoException = util._errnoException;
const errnoException = errors.errnoException;

let truncateWarn = true;

Expand Down Expand Up @@ -406,7 +406,7 @@ fs.accessSync = function(path, mode) {
binding.access(pathModule.toNamespacedPath(path), mode, undefined, ctx);

if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
throw errors.uvException(ctx);
}
};

Expand Down Expand Up @@ -637,7 +637,7 @@ function tryStatSync(fd, isUserFd) {
binding.fstat(fd, undefined, ctx);
if (ctx.errno !== undefined && !isUserFd) {
fs.closeSync(fd);
throw new errors.uvException(ctx);
throw errors.uvException(ctx);
}
}

Expand Down Expand Up @@ -735,7 +735,7 @@ fs.closeSync = function(fd) {
const ctx = {};
binding.close(fd, undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
throw errors.uvException(ctx);
}
};

Expand Down Expand Up @@ -928,7 +928,7 @@ fs.renameSync = function(oldPath, newPath) {
binding.rename(pathModule.toNamespacedPath(oldPath),
pathModule.toNamespacedPath(newPath), undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
throw errors.uvException(ctx);
}
};

Expand Down Expand Up @@ -998,7 +998,7 @@ fs.ftruncateSync = function(fd, len = 0) {
const ctx = {};
binding.ftruncate(fd, len, undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
throw errors.uvException(ctx);
}
};

Expand Down Expand Up @@ -1032,7 +1032,7 @@ fs.fdatasyncSync = function(fd) {
const ctx = {};
binding.fdatasync(fd, undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
throw errors.uvException(ctx);
}
};

Expand All @@ -1048,7 +1048,7 @@ fs.fsyncSync = function(fd) {
const ctx = {};
binding.fsync(fd, undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
throw errors.uvException(ctx);
}
};

Expand Down Expand Up @@ -1133,7 +1133,7 @@ fs.fstatSync = function(fd) {
const ctx = { fd };
binding.fstat(fd, undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
throw errors.uvException(ctx);
}
return statsFromValues();
};
Expand All @@ -1145,7 +1145,7 @@ fs.lstatSync = function(path) {
const ctx = { path };
binding.lstat(pathModule.toNamespacedPath(path), undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
throw errors.uvException(ctx);
}
return statsFromValues();
};
Expand All @@ -1157,7 +1157,7 @@ fs.statSync = function(path) {
const ctx = { path };
binding.stat(pathModule.toNamespacedPath(path), undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
throw errors.uvException(ctx);
}
return statsFromValues();
};
Expand All @@ -1183,7 +1183,7 @@ fs.readlinkSync = function(path, options) {
const result = binding.readlink(pathModule.toNamespacedPath(path),
options.encoding, undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
throw errors.uvException(ctx);
}
return result;
};
Expand Down Expand Up @@ -1263,7 +1263,7 @@ fs.symlinkSync = function(target, path, type) {
pathModule.toNamespacedPath(path), flags, undefined, ctx);

if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
throw errors.uvException(ctx);
} else if (ctx.error) {
// TODO(joyeecheung): this is an encoding error usually caused by memory
// problems. We need to figure out proper error code(s) for this.
Expand Down Expand Up @@ -1308,7 +1308,7 @@ fs.linkSync = function(existingPath, newPath) {
pathModule.toNamespacedPath(newPath),
undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
throw errors.uvException(ctx);
}
return result;
};
Expand All @@ -1331,7 +1331,7 @@ fs.unlinkSync = function(path) {
const ctx = { path };
binding.unlink(pathModule.toNamespacedPath(path), undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
throw errors.uvException(ctx);
}
};

Expand Down Expand Up @@ -1937,7 +1937,7 @@ fs.realpathSync = function realpathSync(p, options) {
const ctx = { path: base };
binding.lstat(pathModule.toNamespacedPath(base), undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
throw errors.uvException(ctx);
}
knownHard[base] = true;
}
Expand Down Expand Up @@ -1981,7 +1981,7 @@ fs.realpathSync = function realpathSync(p, options) {
const ctx = { path: base };
binding.lstat(baseLong, undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
throw errors.uvException(ctx);
}

if ((statValues[1/*mode*/] & S_IFMT) !== S_IFLNK) {
Expand All @@ -2006,11 +2006,11 @@ fs.realpathSync = function realpathSync(p, options) {
const ctx = { path: base };
binding.stat(baseLong, undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
throw errors.uvException(ctx);
}
linkTarget = binding.readlink(baseLong, undefined, undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
throw errors.uvException(ctx);
}
}
resolvedLink = pathModule.resolve(previous, linkTarget);
Expand All @@ -2031,7 +2031,7 @@ fs.realpathSync = function realpathSync(p, options) {
const ctx = { path: base };
binding.lstat(pathModule.toNamespacedPath(base), undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
throw errors.uvException(ctx);
}
knownHard[base] = true;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const {
UV_ESRCH
} = process.binding('uv');

const errnoException = util._errnoException;
const errnoException = errors.errnoException;
const { SocketListSend, SocketListReceive } = SocketList;

const MAX_HANDLE_RETRANSMISSIONS = 3;
Expand Down
Loading