summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoger Meier <roger@apache.org>2014-10-09 02:01:17 +0200
committerRoger Meier <roger@apache.org>2014-10-09 02:01:17 +0200
commit25023b16eb4f26d7fcf986e817e8c437e2f08bf3 (patch)
tree87f07a4b2a0f6236f65508417c52d00d49fd5b48
parentf49ea0691cd788ee3f622f3e97b8d1dcbf0ff89f (diff)
downloadthrift-25023b16eb4f26d7fcf986e817e8c437e2f08bf3.tar.gz
THRIFT-809 Javascript client: Please make required fields actually required.
Patch: noazark Github Pull Request: This closes #232
-rw-r--r--compiler/cpp/src/generate/t_js_generator.cc8
-rw-r--r--lib/nodejs/lib/thrift/protocol.js36
-rw-r--r--lib/nodejs/lib/thrift/thrift.js18
3 files changed, 34 insertions, 28 deletions
diff --git a/compiler/cpp/src/generate/t_js_generator.cc b/compiler/cpp/src/generate/t_js_generator.cc
index 74ec15aac..12b7daa0e 100644
--- a/compiler/cpp/src/generate/t_js_generator.cc
+++ b/compiler/cpp/src/generate/t_js_generator.cc
@@ -708,8 +708,12 @@ void t_js_generator::generate_js_struct_definition(ofstream& out,
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
out << indent() << indent() << "if (args." << (*m_iter)->get_name() << " !== undefined) {" << endl
- << indent() << indent() << indent() << "this." << (*m_iter)->get_name() << " = args." << (*m_iter)->get_name() << ";" << endl
- << indent() << indent() << "}" << endl;
+ << indent() << indent() << indent() << "this." << (*m_iter)->get_name() << " = args." << (*m_iter)->get_name() << ";" << endl;
+ if (!(*m_iter)->get_req()) {
+ out << indent() << indent() << "} else {" << endl
+ << indent() << indent() << indent() << "throw new Thrift.TProtocolException(Thrift.TProtocolExceptionType.UNKNOWN, 'Required field " << (*m_iter)->get_name() << " is unset!');" << endl;
+ }
+ out << indent() << indent() << "}" << endl;
if (gen_ts_) {
f_types_ts_ << (*m_iter)->get_name() << ts_get_req(*m_iter) << ": " << ts_get_type((*m_iter)->get_type()) << "; ";
}
diff --git a/lib/nodejs/lib/thrift/protocol.js b/lib/nodejs/lib/thrift/protocol.js
index 6c4d9e611..c8e8a4e6c 100644
--- a/lib/nodejs/lib/thrift/protocol.js
+++ b/lib/nodejs/lib/thrift/protocol.js
@@ -25,22 +25,6 @@ var binary = require('./binary'),
var InputBufferUnderrunError = require('./transport').InputBufferUnderrunError;
-var UNKNOWN = 0,
- INVALID_DATA = 1,
- NEGATIVE_SIZE = 2,
- SIZE_LIMIT = 3,
- BAD_VERSION = 4,
- NOT_IMPLEMENTED = 5,
- DEPTH_LIMIT = 6;
-
-var TProtocolException = function(type, message) {
- Error.call(this, message);
- this.name = 'TProtocolException';
- this.type = type;
-};
-util.inherits(TProtocolException, Error);
-
-
//
// BINARY PROTOCOL
//
@@ -186,14 +170,14 @@ TBinaryProtocol.prototype.readMessageBegin = function() {
var version = sz & VERSION_MASK;
if (version != VERSION_1) {
console.log("BAD: " + version);
- throw new TProtocolException(BAD_VERSION, "Bad version in readMessageBegin: " + sz);
+ throw new Thrift.TProtocolException(Thrift.TProtocolExceptionType.BAD_VERSION, "Bad version in readMessageBegin: " + sz);
}
type = sz & TYPE_MASK;
name = this.readString();
seqid = this.readI32();
} else {
if (this.strictRead) {
- throw new TProtocolException(BAD_VERSION, "No protocol version header");
+ throw new Thrift.TProtocolException(Thrift.TProtocolExceptionType.BAD_VERSION, "No protocol version header");
}
name = this.trans.read(sz);
type = this.readByte();
@@ -550,7 +534,7 @@ TCompactProtocol.prototype.getTType = function(type) {
case TCompactProtocol.Types.CT_STRUCT:
return Type.STRUCT;
default:
- throw new TProtocolException(INVALID_DATA, "Unknown type: " + type);
+ throw new Thrift.TProtocolException(Thift.TProtocolExceptionType.INVALID_DATA, "Unknown type: " + type);
}
return Type.STOP;
};
@@ -808,7 +792,7 @@ TCompactProtocol.prototype.i64ToZigzag = function(l) {
l = new Int64(l);
}
if (! (l instanceof Int64)) {
- throw new TProtocolException(INVALID_DATA, "Expected Int64 or Number, found: " + l);
+ throw new Thrift.TProtocolException(Thrift.TProtocolExceptionType.INVALID_DATA, "Expected Int64 or Number, found: " + l);
}
var hi = l.buffer.readUInt32BE(0, true);
var lo = l.buffer.readUInt32BE(4, true);
@@ -907,8 +891,8 @@ TCompactProtocol.prototype.readFieldEnd = function() {
TCompactProtocol.prototype.readMapBegin = function() {
var msize = this.readVarint32();
if (msize < 0) {
- throw new TProtocolException(NEGATIVE_SIZE, "Negative map size");
- }
+ throw new Thrift.TProtocolException(Thrift.TProtocolExceptionType.NEGATIVE_SIZE, "Negative map size");
+ }
var kvType = 0;
if (msize !== 0) {
@@ -932,8 +916,8 @@ TCompactProtocol.prototype.readListBegin = function() {
}
if (lsize < 0) {
- throw TProtocolException(NEGATIVE_SIZE, "Negative list size");
- }
+ throw new Thrift.TProtocolException(Thrift.TProtocolExceptionType.NEGATIVE_SIZE, "Negative list size");
+ }
var elemType = this.getTType(size_and_type & 0x0000000f);
@@ -993,7 +977,7 @@ TCompactProtocol.prototype.readBinary = function() {
// Catch error cases
if (size < 0) {
- throw new TProtocolException(NEGATIVE_SIZE, "Negative binary/string size");
+ throw new Thrift.TProtocolException(Thrift.TProtocolExceptionType.NEGATIVE_SIZE, "Negative binary/string size");
}
var value = this.trans.readString(size);
@@ -1042,7 +1026,7 @@ TCompactProtocol.prototype.readVarint64 = function() {
break;
}
if (rsize >= 10) {
- throw new TProtocolException(INVALID_DATA, "Variable-length int over 10 bytes.");
+ throw new Thrift.TProtocolException(Thrift.TProtocolExceptionType.INVALID_DATA, "Variable-length int over 10 bytes.");
}
}
var i64 = new Int64(hi, lo);
diff --git a/lib/nodejs/lib/thrift/thrift.js b/lib/nodejs/lib/thrift/thrift.js
index aabe11f5d..e02bb7dd0 100644
--- a/lib/nodejs/lib/thrift/thrift.js
+++ b/lib/nodejs/lib/thrift/thrift.js
@@ -126,6 +126,24 @@ TApplicationException.prototype.write = function(output){
output.writeStructEnd();
};
+var TProtocolExceptionType = exports.TProtocolExceptionType = {
+ UNKNOWN: 0,
+ INVALID_DATA: 1,
+ NEGATIVE_SIZE: 2,
+ SIZE_LIMIT: 3,
+ BAD_VERSION: 4,
+ NOT_IMPLEMENTED: 5,
+ DEPTH_LIMIT: 6
+}
+
+
+var TProtocolException = exports.TProtocolException = function(type, message) {
+ Error.call(this, message);
+ this.name = 'TProtocolException';
+ this.type = type;
+};
+util.inherits(TProtocolException, Error);
+
exports.objectLength = function(obj) {
return Object.keys(obj).length;
};