Skip to content

Commit 64b6140

Browse files
committed
Merge pull request #501 from stephenplusplus/spp--pubsub-publish-error-message
pubsub: type check input. fixes #500
2 parents 3ce39f6 + eef7ce2 commit 64b6140

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

lib/common/util.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,7 @@ function getType(value) {
245245
*/
246246
function prop(name) {
247247
return function(item) {
248-
if (name in item) {
249-
return item[name];
250-
}
248+
return item[name];
251249
};
252250
}
253251

lib/pubsub/topic.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Topic.formatMessage_ = function(message) {
8787

8888
/**
8989
* Format the name of a topic. A Topic's full name is in the format of
90-
* 'projects/{projectId}/topics/{topicName}.
90+
* 'projects/{projectId}/topics/{topicName}'.
9191
*
9292
* @private
9393
*
@@ -102,8 +102,8 @@ Topic.formatName_ = function(projectId, name) {
102102
};
103103

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

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

182+
if (!messages.every(util.prop('data'))) {
183+
throw new Error('Cannot publish message without a `data` property.');
184+
}
185+
181186
callback = callback || util.noop;
182187

183188
var body = {

test/pubsub/topic.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,17 @@ describe('Topic', function() {
129129
it('should throw if no message is provided', function() {
130130
assert.throws(function() {
131131
topic.publish();
132-
}, /Cannot publish/);
132+
}, /Cannot publish without a message/);
133133

134134
assert.throws(function() {
135135
topic.publish([]);
136-
}, /Cannot publish/);
136+
}, /Cannot publish without a message/);
137+
});
138+
139+
it('should throw if a message has no data', function() {
140+
assert.throws(function() {
141+
topic.publish(message);
142+
}, /Cannot publish message without a `data` property/);
137143
});
138144

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

0 commit comments

Comments
 (0)