Skip to content

Commit

Permalink
Merge pull request #501 from stephenplusplus/spp--pubsub-publish-erro…
Browse files Browse the repository at this point in the history
…r-message

pubsub: type check input. fixes #500
  • Loading branch information
ryanseys committed Apr 15, 2015
2 parents 3ce39f6 + eef7ce2 commit 64b6140
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
4 changes: 1 addition & 3 deletions lib/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,7 @@ function getType(value) {
*/
function prop(name) {
return function(item) {
if (name in item) {
return item[name];
}
return item[name];
};
}

Expand Down
15 changes: 10 additions & 5 deletions lib/pubsub/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Topic.formatMessage_ = function(message) {

/**
* Format the name of a topic. A Topic's full name is in the format of
* 'projects/{projectId}/topics/{topicName}.
* 'projects/{projectId}/topics/{topicName}'.
*
* @private
*
Expand All @@ -102,8 +102,8 @@ Topic.formatName_ = function(projectId, name) {
};

/**
* Wrapper for makeReq_ that automatically attempts to create a topic if it
* does not yet exist.
* Wrapper for makeReq_ that automatically attempts to create a topic if it does
* not yet exist.
*
* @private
*/
Expand All @@ -130,10 +130,11 @@ Topic.prototype.autoCreateWrapper_ = function(method, path, q, body, callback) {
};

/**
* Publish the provided message or array of messages. A message can be of any
* type. On success, an array of messageIds is returned in the response.
* Publish the provided message or array of messages. On success, an array of
* messageIds is returned in the response.
*
* @throws {Error} If no message is provided.
* @throws {Error} If a message is missing a data property.
*
* @param {object|object[]} message - The message(s) to publish.
* @param {*} message.data - The contents of the message.
Expand Down Expand Up @@ -178,6 +179,10 @@ Topic.prototype.publish = function(messages, callback) {
throw new Error('Cannot publish without a message.');
}

if (!messages.every(util.prop('data'))) {
throw new Error('Cannot publish message without a `data` property.');
}

callback = callback || util.noop;

var body = {
Expand Down
10 changes: 8 additions & 2 deletions test/pubsub/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,17 @@ describe('Topic', function() {
it('should throw if no message is provided', function() {
assert.throws(function() {
topic.publish();
}, /Cannot publish/);
}, /Cannot publish without a message/);

assert.throws(function() {
topic.publish([]);
}, /Cannot publish/);
}, /Cannot publish without a message/);
});

it('should throw if a message has no data', function() {
assert.throws(function() {
topic.publish(message);
}, /Cannot publish message without a `data` property/);
});

it('should send correct api request', function(done) {
Expand Down

0 comments on commit 64b6140

Please sign in to comment.