summaryrefslogtreecommitdiff
path: root/lib/dgram.js
diff options
context:
space:
mode:
authorMichaël Zasso <targos@protonmail.com>2018-02-27 14:55:32 +0100
committerMichaël Zasso <targos@protonmail.com>2018-03-05 19:51:30 +0100
commit1e8d110e640c658e4f6ed7540db62d063269ba6c (patch)
treeafe2636b1b190fee00006beaa484fa77d5949770 /lib/dgram.js
parent023f49c5a938ef631260b76876155eaf957084be (diff)
downloadandroid-node-v8-1e8d110e640c658e4f6ed7540db62d063269ba6c.tar.gz
android-node-v8-1e8d110e640c658e4f6ed7540db62d063269ba6c.tar.bz2
android-node-v8-1e8d110e640c658e4f6ed7540db62d063269ba6c.zip
lib: port errors to new system
This is a first batch of updates that touches non-underscored modules in lib. PR-URL: https://github.com/nodejs/node/pull/19034 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Diffstat (limited to 'lib/dgram.js')
-rw-r--r--lib/dgram.js67
1 files changed, 35 insertions, 32 deletions
diff --git a/lib/dgram.js b/lib/dgram.js
index a31384a586..78730635cb 100644
--- a/lib/dgram.js
+++ b/lib/dgram.js
@@ -23,6 +23,17 @@
const assert = require('assert');
const errors = require('internal/errors');
+const {
+ ERR_INVALID_ARG_TYPE,
+ ERR_MISSING_ARGS,
+ ERR_SOCKET_ALREADY_BOUND,
+ ERR_SOCKET_BAD_BUFFER_SIZE,
+ ERR_SOCKET_BAD_PORT,
+ ERR_SOCKET_BAD_TYPE,
+ ERR_SOCKET_BUFFER_SIZE,
+ ERR_SOCKET_CANNOT_SEND,
+ ERR_SOCKET_DGRAM_NOT_RUNNING
+} = errors.codes;
const { Buffer } = require('buffer');
const dns = require('dns');
const util = require('util');
@@ -65,7 +76,7 @@ function newHandle(type, lookup) {
if (lookup === undefined)
lookup = dns.lookup;
else if (typeof lookup !== 'function')
- throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'lookup', 'Function');
+ throw new ERR_INVALID_ARG_TYPE('lookup', 'Function');
if (type === 'udp4') {
const handle = new UDP();
@@ -81,7 +92,7 @@ function newHandle(type, lookup) {
return handle;
}
- throw new errors.TypeError('ERR_SOCKET_BAD_TYPE');
+ throw new ERR_SOCKET_BAD_TYPE();
}
@@ -173,13 +184,12 @@ function replaceHandle(self, newHandle) {
function bufferSize(self, size, buffer) {
if (size >>> 0 !== size)
- throw new errors.TypeError('ERR_SOCKET_BAD_BUFFER_SIZE');
+ throw new ERR_SOCKET_BAD_BUFFER_SIZE();
const ctx = {};
const ret = self._handle.bufferSize(size, buffer, ctx);
if (ret === undefined) {
- throw new errors.Error('ERR_SOCKET_BUFFER_SIZE',
- new errors.SystemError(ctx));
+ throw new ERR_SOCKET_BUFFER_SIZE(new errors.SystemError(ctx));
}
return ret;
}
@@ -190,7 +200,7 @@ Socket.prototype.bind = function(port_, address_ /*, callback*/) {
this._healthCheck();
if (this._bindState !== BIND_STATE_UNBOUND)
- throw new errors.Error('ERR_SOCKET_ALREADY_BOUND');
+ throw new ERR_SOCKET_ALREADY_BOUND();
this._bindState = BIND_STATE_BINDING;
@@ -290,19 +300,19 @@ Socket.prototype.sendto = function(buffer,
address,
callback) {
if (typeof offset !== 'number') {
- throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'offset', 'number');
+ throw new ERR_INVALID_ARG_TYPE('offset', 'number');
}
if (typeof length !== 'number') {
- throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'length', 'number');
+ throw new ERR_INVALID_ARG_TYPE('length', 'number');
}
if (typeof port !== 'number') {
- throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'port', 'number');
+ throw new ERR_INVALID_ARG_TYPE('port', 'number');
}
if (typeof address !== 'string') {
- throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'address', 'string');
+ throw new ERR_INVALID_ARG_TYPE('address', 'string');
}
this.send(buffer, offset, length, port, address, callback);
@@ -313,9 +323,8 @@ function sliceBuffer(buffer, offset, length) {
if (typeof buffer === 'string') {
buffer = Buffer.from(buffer);
} else if (!isUint8Array(buffer)) {
- throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
- 'buffer',
- ['Buffer', 'Uint8Array', 'string']);
+ throw new ERR_INVALID_ARG_TYPE('buffer',
+ ['Buffer', 'Uint8Array', 'string']);
}
offset = offset >>> 0;
@@ -363,7 +372,7 @@ function onListenSuccess() {
function onListenError(err) {
this.removeListener('listening', onListenSuccess);
this._queue = undefined;
- this.emit('error', new errors.Error('ERR_SOCKET_CANNOT_SEND'));
+ this.emit('error', new ERR_SOCKET_CANNOT_SEND());
}
@@ -406,21 +415,19 @@ Socket.prototype.send = function(buffer,
if (typeof buffer === 'string') {
list = [ Buffer.from(buffer) ];
} else if (!isUint8Array(buffer)) {
- throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
- 'buffer',
- ['Buffer', 'Uint8Array', 'string']);
+ throw new ERR_INVALID_ARG_TYPE('buffer',
+ ['Buffer', 'Uint8Array', 'string']);
} else {
list = [ buffer ];
}
} else if (!(list = fixBufferList(buffer))) {
- throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
- 'buffer list arguments',
- ['Buffer', 'string']);
+ throw new ERR_INVALID_ARG_TYPE('buffer list arguments',
+ ['Buffer', 'string']);
}
port = port >>> 0;
if (port === 0 || port > 65535)
- throw new errors.RangeError('ERR_SOCKET_BAD_PORT', port);
+ throw new ERR_SOCKET_BAD_PORT(port);
// Normalize callback so it's either a function or undefined but not anything
// else.
@@ -431,9 +438,7 @@ Socket.prototype.send = function(buffer,
callback = address;
address = undefined;
} else if (address && typeof address !== 'string') {
- throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
- 'address',
- ['string', 'falsy']);
+ throw new ERR_INVALID_ARG_TYPE('address', ['string', 'falsy']);
}
this._healthCheck();
@@ -555,7 +560,7 @@ Socket.prototype.setBroadcast = function(arg) {
Socket.prototype.setTTL = function(ttl) {
if (typeof ttl !== 'number') {
- throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'ttl', 'number', ttl);
+ throw new ERR_INVALID_ARG_TYPE('ttl', 'number', ttl);
}
var err = this._handle.setTTL(ttl);
@@ -569,7 +574,7 @@ Socket.prototype.setTTL = function(ttl) {
Socket.prototype.setMulticastTTL = function(ttl) {
if (typeof ttl !== 'number') {
- throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'ttl', 'number', ttl);
+ throw new ERR_INVALID_ARG_TYPE('ttl', 'number', ttl);
}
var err = this._handle.setMulticastTTL(ttl);
@@ -595,9 +600,7 @@ Socket.prototype.setMulticastInterface = function(interfaceAddress) {
this._healthCheck();
if (typeof interfaceAddress !== 'string') {
- throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
- 'interfaceAddress',
- 'string');
+ throw new ERR_INVALID_ARG_TYPE('interfaceAddress', 'string');
}
const err = this._handle.setMulticastInterface(interfaceAddress);
@@ -611,7 +614,7 @@ Socket.prototype.addMembership = function(multicastAddress,
this._healthCheck();
if (!multicastAddress) {
- throw new errors.TypeError('ERR_MISSING_ARGS', 'multicastAddress');
+ throw new ERR_MISSING_ARGS('multicastAddress');
}
var err = this._handle.addMembership(multicastAddress, interfaceAddress);
@@ -626,7 +629,7 @@ Socket.prototype.dropMembership = function(multicastAddress,
this._healthCheck();
if (!multicastAddress) {
- throw new errors.TypeError('ERR_MISSING_ARGS', 'multicastAddress');
+ throw new ERR_MISSING_ARGS('multicastAddress');
}
var err = this._handle.dropMembership(multicastAddress, interfaceAddress);
@@ -639,7 +642,7 @@ Socket.prototype.dropMembership = function(multicastAddress,
Socket.prototype._healthCheck = function() {
if (!this._handle) {
// Error message from dgram_legacy.js.
- throw new errors.Error('ERR_SOCKET_DGRAM_NOT_RUNNING');
+ throw new ERR_SOCKET_DGRAM_NOT_RUNNING();
}
};