Skip to content

Commit

Permalink
Merge pull request #90 from nats-io/error-on-throw
Browse files Browse the repository at this point in the history
[FIX] #89
  • Loading branch information
Alberto Ricart authored Sep 6, 2016
2 parents adaaaa5 + 86b9e8c commit d277cab
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
38 changes: 19 additions & 19 deletions lib/nats.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var net = require('net'),
* Constants
*/

var VERSION = '0.6.6',
var VERSION = '0.6.8',

DEFAULT_PORT = 4222,
DEFAULT_PRE = 'nats://localhost:',
Expand Down Expand Up @@ -67,13 +67,13 @@ var VERSION = '0.6.6',
PONG_RESPONSE = 'PONG' + CR_LF,

// Errors
BAD_SUBJECT_ERR = new Error('Subject must be supplied'),
BAD_MSG_ERR = new Error('Message can\'t be a function'),
BAD_REPLY_ERR = new Error('Reply can\'t be a function'),
CONN_CLOSED_ERR = new Error('Connection closed'),
BAD_JSON_MSG_ERR = new Error('Message should be a JSON object'),
BAD_AUTHENTICATION_ERR = new Error('User and Token can not both be provided'),
BAD_SUBJECT = 'Subject must be supplied',
BAD_MSG = 'Message can\'t be a function',
BAD_REPLY = 'Reply can\'t be a function',
CONN_CLOSED = 'Connection closed',
BAD_JSON_MSG = 'Message should be a JSON object',
BAD_AUTHENTICATION = 'User and Token can not both be provided',

// Pedantic Mode support
//Q_SUB = /^([^\.\*>\s]+|>$|\*)(\.([^\.\*>\s]+|>$|\*))*$/, // TODO: remove / never used
//Q_SUB_NO_WC = /^([^\.\*>\s]+)(\.([^\.\*>\s]+))*$/, // TODO: remove / never used
Expand Down Expand Up @@ -224,7 +224,7 @@ Client.prototype.parseOptions = function(opts) {

// Authentication - make sure authentication is valid.
if (client.user && client.token) {
throw(BAD_AUTHENTICATION_ERR);
throw(new Error(BAD_AUTHENTICATION));
}

// Encoding - make sure its valid.
Expand Down Expand Up @@ -920,10 +920,10 @@ Client.prototype.addServer = function(uri) {
Client.prototype.flush = function(opt_callback) {
if (this.closed) {
if (typeof opt_callback === 'function') {
opt_callback(CONN_CLOSED_ERR);
opt_callback(new Error(CONN_CLOSED));
return;
} else {
throw(CONN_CLOSED_ERR);
throw(new Error(CONN_CLOSED));
}
}
if (this.pongs) {
Expand Down Expand Up @@ -952,14 +952,14 @@ Client.prototype.publish = function(subject, msg, opt_reply, opt_callback) {
if (!msg) { msg = EMPTY; }
if (!subject) {
if (opt_callback) {
opt_callback(BAD_SUBJECT_ERR);
opt_callback(new Error(BAD_SUBJECT));
} else {
throw(BAD_SUBJECT_ERR);
throw(new Error(BAD_SUBJECT));
}
}
if (typeof msg === 'function') {
if (opt_callback || opt_reply) {
opt_callback(BAD_MSG_ERR);
opt_callback(new Error(BAD_MSG));
return;
}
opt_callback = msg;
Expand All @@ -968,7 +968,7 @@ Client.prototype.publish = function(subject, msg, opt_reply, opt_callback) {
}
if (typeof opt_reply === 'function') {
if (opt_callback) {
opt_callback(BAD_REPLY_ERR);
opt_callback(new Error(BAD_REPLY));
return;
}
opt_callback = opt_reply;
Expand All @@ -988,12 +988,12 @@ Client.prototype.publish = function(subject, msg, opt_reply, opt_callback) {
var str = msg;
if (this.options.json) {
if (typeof msg !== 'object' || Array.isArray(msg)) {
throw(BAD_JSON_MSG_ERR);
throw(new Error(BAD_JSON_MSG));
}
try {
str = JSON.stringify(msg);
} catch (e) {
throw(BAD_JSON_MSG_ERR);
throw(new Error(BAD_JSON_MSG));
}
}
this.sendCommand(psub + Buffer.byteLength(str) + CR_LF + str + CR_LF);
Expand All @@ -1008,7 +1008,7 @@ Client.prototype.publish = function(subject, msg, opt_reply, opt_callback) {
if (opt_callback !== undefined) {
this.flush(opt_callback);
} else if (this.closed) {
throw(CONN_CLOSED_ERR);
throw(new Error(CONN_CLOSED));
}
};

Expand All @@ -1025,7 +1025,7 @@ Client.prototype.publish = function(subject, msg, opt_reply, opt_callback) {

Client.prototype.subscribe = function(subject, opts, callback) {
if (this.closed) {
throw(CONN_CLOSED_ERR);
throw(new Error(CONN_CLOSED));
}
var qgroup, max;
if (typeof opts === 'function') {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nats",
"version": "0.6.6",
"version": "0.6.8",
"description": "Node.js client for NATS, a lightweight, high-performance cloud native messaging system",
"keywords": [
"nats",
Expand Down

0 comments on commit d277cab

Please sign in to comment.