Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

thrift node javascript namespace

thrift node javascript namespace

Problem

I seem to be experiencing a node/thrift namespace conflict.

Foo.thrift
...
struct Error {
    1: i32 code,
    2: string message
}
...

which generates the following file via thrift --gen js:node Foo.thrift (thrift v0.9.0)

Foo_types.js
...
Error = module.exports.Error = function(args) {
  this.code = null;
  this.message = null;
  if (args) {
    if (args.code !== undefined) {
      this.code = args.code;
    }
    if (args.message !== undefined) {
      this.message = args.message;
    }
  }
};
Error.prototype = {};
Error.prototype.read = function(input) {
...

I include the module in node

var FooTypes = require('./../gen-nodejs/Foo_types')

I seem to run into a namespace conflict with javascript's Error object

callback(new Error("Couldn't find profile"));

In the callback, it shows that I have an object with code and message vs a plain old JS Error containing "message" even though I didn't ask for FooTypes.Error.

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Error

Has anyone else ran into this? How do I make reference to the plain JS Error?

Thanks

Problem courtesy of: Trial Offer

Solution

You're missing namespace declaration. Try this:

# Foo.thrift file content
namespace js Foo
...
struct Error {
    1: i32 code,
    2: string message
}
...

Then your thrift object would be Foo.Error.

Solution courtesy of: ducin

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

thrift node javascript namespace

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×