Nodejs-pubsub: Unhelp error message on invalid attributes

Created on 10 Jul 2018  路  5Comments  路  Source: googleapis/nodejs-pubsub

The publish(message, attributes) method in the Publisher class provides an unhelpful error message when the attributes object is specified _incorrectly_. It seems like the attribute object must only have values that are strings, Buffers, or ArrayBuffers. When the attributes is specified incorrectly, the Error in the rejected Promise is not very helpful:

TypeError: "string" must be a string, Buffer, or ArrayBuffer
    at Function.byteLength (buffer.js:481:11)
    at BufferWriter.write_string_buffer [as string] (/usr/local/google/home/nareddyt/workspace/untar-cloud-function/node_modules/protobufjs/src/writer_buffer.js:68:22)
    at Type.PubsubMessage$encode [as encode] (eval at Codegen (/usr/local/google/home/nareddyt/workspace/untar-cloud-function/node_modules/@protobufjs/codegen/index.js:50:33), <anonymous>:11:15)
    at Type.encode_setup [as encode] (/usr/local/google/home/nareddyt/workspace/untar-cloud-function/node_modules/protobufjs/src/type.js:485:25)
    at BundleDescriptor.getByteLength [as byteLengthFunction] (/usr/local/google/home/nareddyt/workspace/untar-cloud-function/node_modules/google-gax/lib/grpc.js:269:20)
    at /usr/local/google/home/nareddyt/workspace/untar-cloud-function/node_modules/google-gax/lib/bundling.js:349:38
    at Array.forEach (<anonymous>)
    at BundleExecutor.schedule (/usr/local/google/home/nareddyt/workspace/untar-cloud-function/node_modules/google-gax/lib/bundling.js:348:16)
    at /usr/local/google/home/nareddyt/workspace/untar-cloud-function/node_modules/google-gax/lib/bundling.js:500:20
    at Canceller.call (/usr/local/google/home/nareddyt/workspace/untar-cloud-function/node_modules/google-gax/lib/api_callable.js:109:19)

Note that this error doesn't tell me:

  • Which specific method parameter was incorrect
  • Where in my original node.js module the error occurred (the stacktrace starts in a dependency)
  • What the actual error message means (I thought to myself: "Why is a string not a actually string...?")

Additionally, the restriction on the attributes object is not documented in the SDK docs for the method. This leads to further confusion when trying to debug this error.

Environment details

  • OS: gLinux
  • Node.js version: v8.11.0
  • npm version: 6.1.0
  • @google-cloud/pubsub version: 0.18.0

Steps to reproduce

Run the following code. Note here than I'm passing in a Boolean value in the attributes object, instead of a string. This would also occur if I pass in nested objects, an array, or null.

const PubSub = require('@google-cloud/pubsub');
const pubsub = new PubSub();
const topic = pubsub.topic(PUB_SUB_TOPIC_NAME);
const publisher = topic.publisher();

const message = Buffer.from('Hello World');
const attributes = {
  success: true,    // FIXME: This should be a string, not a boolean!
};

publisher.publish(message, attributes)
    .then(() => console.log('Done'))
    .catch(err => console.error(err));
pubsub triage me

Most helpful comment

Ohhh gosh!!! it took me like an hour trying to figure out where the problem actually was. Thanks God I came across this issue soon enough.

All 5 comments

Thank you for the suggestion. I agree that we could do better type-checking, although I'd be okay with simply better documentation to explain what message attributes are to begin with. In that place, it could mention the type restrictions.

If you're willing, a PR with your interpretation of the best solution would be great. Thanks again for taking the time to help.

Sounds good, I'll make a PR. I'm leaning towards including type checking just because the error isn't very helpful. I'll decide soon!

Hey, just checking in. Could you take a look at my PR when you get a chance? Thanks @stephenplusplus!

Sorry, I missed the emails about it 馃槩 Taking a look now, thanks again!

Ohhh gosh!!! it took me like an hour trying to figure out where the problem actually was. Thanks God I came across this issue soon enough.

Was this page helpful?
0 / 5 - 0 ratings