googleapis / google-cloud-node

Google Cloud Client Library for Node.js
https://cloud.google.com/nodejs
Apache License 2.0
2.93k stars 598 forks source link

Scoping error in packages/common/src/util.js #1654

Closed abawany closed 8 years ago

abawany commented 8 years ago

When Google Storage is used to access a file that does not exist, the following uncaught exception is generated by the package:

error:  ApiError: Not Found
    at new util.ApiError (/tmp/node_modules/google-cloud/node_modules/@google-cloud/common/src/util.js:107:10)
    at Object.parseHttpRespMessage (/tmp/node_modules/google-cloud/node_modules/@google-cloud/common/src/util.js:149:33)
    at Object.handleResp (/tmp/node_modules/google-cloud/node_modules/@google-cloud/common/src/util.js:124:18)
    at .<anonymous> (/tmp/node_modules/google-cloud/node_modules/@google-cloud/storage/src/file.js:632:21)
    at emitOne (events.js:96:13)
    at emit (events.js:188:7)
    at emitOne (events.js:96:13)
    at DestroyableTransform.emit (events.js:188:7)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:188:7)

Going to line 107 of the offending file shows this:

106: util.ApiError = function(errorBody) {
107:  return new ApiError(errorBody);
108: };

(EDIT: alleged remedy was not a solution either). ApiError is defined above this code as var ApiError = ....

Environment details

Making sure to follow these steps will guarantee the quickest resolution possible.

Thanks!

abawany commented 8 years ago

On capturing a stack trace at the offending line, it appears that the function above is invoked thrice. The first invocation works but the next two fail. The code that invokes the above function is as follows (line 149 of the same file):

function parseHttpRespMessage(httpRespMessage) {
  var parsedHttpRespMessage = {
    resp: httpRespMessage
  };

  if (httpRespMessage.statusCode < 200 || httpRespMessage.statusCode > 299) {
    // Unknown error. Format according to ApiError standard.
    parsedHttpRespMessage.err = new util.ApiError({ // this is line 149
      errors: [],
      code: httpRespMessage.statusCode,
      message: httpRespMessage.statusMessage,
      response: httpRespMessage
    });
  }

  return parsedHttpRespMessage;
}
stephenplusplus commented 8 years ago

Can you show the code you're using that access our library?

abawany commented 8 years ago

Sure. I was previously using gcloud@0.37.0 and hadn't noticed these issues. Moving to google-cloud (starting with 0.40.0 and now at 0.41.2) shows these issues.

var gc = require('google-cloud') ({ projectId: 'something' });
var gsInst = gc.storage({ projectId: 'something' });
var bucket = gsInst.bucket('bucket');
var destFile = bucket.file(fromFile);
var strm = destFile.createReadStream({validation: 'crc32c'});
strm
  .on('error', function(e) { 
    log.inst.error({e: e.stack});
    return cb(e); 
  })
  .pipe(res)
  .on('response', function(r) { log.inst.debug({r: r}); })
  .on('end', function() { 
    log.inst.debug({end: true}); 
    return res.send(200); 
  }
);
stephenplusplus commented 8 years ago

I haven't been able to reproduce with the following code using @google-cloud/storage@0.2.0 (included in google-cloud > 41.0):

{
  "name": "@google-cloud/gissue-1654",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@google-cloud/storage": "^0.2.0"
  }
}
var storage = require('@google-cloud/storage')({ authInfo })
var stream = require('stream')

var file = storage.bucket('stephen-has-a-new-bucket').file('non-existent-file')

file.createReadStream({ validation: 'crc32c' })
  .on('error', function(e) {
    console.log('error', e)
  })
  .on('data', function() {})

Would you mind trying it in this format, which doesn't have other dependencies?

abawany commented 8 years ago

The code is behaving as expected. The issue that caused the uce was due to passing the error object returned by this call to gcloud logging instead of error.stack. Here was the uce for reference - I will close this issue.

{ t: 'object',
  x: 
   TypeError: obj.hasOwnProperty is not a function
       at Function.GrpcService.objToStruct_ (/tmp/node_modules/google-cloud/node_modules/@google-cloud/common/src/grpc-service.js:669:13)
       at Function.GrpcService.encodeValue_ (/tmp/node_modules/google-cloud/node_modules/@google-cloud/common/src/grpc-service.js:506:32)
       at Function.GrpcService.objToStruct_ (/tmp/node_modules/google-cloud/node_modules/@google-cloud/common/src/grpc-service.js:676:50)
       at Function.GrpcService.encodeValue_ (/tmp/node_modules/google-cloud/node_modules/@google-cloud/common/src/grpc-service.js:506:32)
       at Function.GrpcService.objToStruct_ (/tmp/node_modules/google-cloud/node_modules/@google-cloud/common/src/grpc-service.js:676:50)
       at Function.GrpcService.encodeValue_ (/tmp/node_modules/google-cloud/node_modules/@google-cloud/common/src/grpc-service.js:506:32)
       at Function.GrpcService.objToStruct_ (/tmp/node_modules/google-cloud/node_modules/@google-cloud/common/src/grpc-service.js:676:50)
       at Function.GrpcService.encodeValue_ (/tmp/node_modules/google-cloud/node_modules/@google-cloud/common/src/grpc-service.js:506:32)
       at Function.GrpcService.objToStruct_ (/tmp/node_modules/google-cloud/node_modules/@google-cloud/common/src/grpc-service.js:676:50)
       at Function.GrpcService.encodeValue_ (/tmp/node_modules/google-cloud/node_modules/@google-cloud/common/src/grpc-service.js:506:32),
  e: '{}' }
stephenplusplus commented 8 years ago

Phew, glad it worked out!

abawany commented 3 years ago

@JustinBeckwith and @stephenplusplus not sure why this 4 year old bug was re-assigned to me but it is closed anyways so no harm no foul, I guess ?