Skip to content

Commit 3babdd6

Browse files
Jens-Gclaude
andcommitted
Add test for ES6 generated exception constructor
Client: js Add unit tests verifying that ES6-generated exception classes pass the exception name string to super() instead of the args object. This is a regression test for the bug fixed in PR #3372, where super(args) caused TBinaryProtocol.writeStringOrBinary to throw. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent ca8c2c8 commit 3babdd6

2 files changed

Lines changed: 88 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
"use strict";
21+
22+
const test = require("tape");
23+
const thrift = require("thrift");
24+
25+
// ES5 generated types (pre-ES6 path)
26+
const ttypesEs5 = require("./gen-nodejs/ThriftTest_types");
27+
// ES6 generated types
28+
const ttypesEs6 = require("./gen-nodejs-es6/ThriftTest_types");
29+
30+
function serializeBinary(data) {
31+
let buff;
32+
const transport = new thrift.TBufferedTransport(null, function (msg) {
33+
buff = msg;
34+
});
35+
const prot = new thrift.TBinaryProtocol(transport);
36+
data[Symbol.for("write")](prot);
37+
prot.flush();
38+
return buff;
39+
}
40+
41+
// Test that ES6 generated exception constructor passes the exception name
42+
// (not the args object) to super(), matching the ES5 behavior.
43+
// Regression test for: https://github.com/apache/thrift/pull/3372
44+
45+
test("ES6 generated exception - constructor sets name and message correctly", function t(assert) {
46+
const e = new ttypesEs6.Xception({ errorCode: 1001, message: "test error" });
47+
assert.ok(e instanceof thrift.Thrift.TException, "is instanceof TException");
48+
assert.ok(e instanceof Error, "is instanceof Error");
49+
assert.equal(e.name, "Xception", "name is set to exception class name");
50+
assert.equal(e.errorCode, 1001, "custom field errorCode is set");
51+
assert.equal(typeof e.stack, "string", "has stack trace");
52+
assert.end();
53+
});
54+
55+
test("ES6 generated exception - super() receives string, not args object", function t(assert) {
56+
const e = new ttypesEs6.Xception({ errorCode: 1001, message: "test error" });
57+
// The bug was that super(args) passed the args object to TException,
58+
// which would cause message to be "[object Object]"
59+
assert.notEqual(e.message, "[object Object]",
60+
"message is not '[object Object]' (would indicate args object was passed to super)");
61+
assert.end();
62+
});
63+
64+
test("ES6 generated exception - serialization does not throw", function t(assert) {
65+
const e = new ttypesEs6.Xception({ errorCode: 1001, message: "test error" });
66+
assert.doesNotThrow(function () {
67+
serializeBinary(e);
68+
}, "serializing an ES6 exception should not throw");
69+
assert.end();
70+
});
71+
72+
test("ES5 generated exception - constructor sets name and message correctly", function t(assert) {
73+
const e = new ttypesEs5.Xception({ errorCode: 1001, message: "test error" });
74+
assert.ok(e instanceof thrift.Thrift.TException, "is instanceof TException");
75+
assert.ok(e instanceof Error, "is instanceof Error");
76+
assert.equal(e.name, "Xception", "name is set to exception class name");
77+
assert.equal(e.errorCode, 1001, "custom field errorCode is set");
78+
assert.end();
79+
});
80+
81+
test("ES5 and ES6 generated exceptions have consistent behavior", function t(assert) {
82+
const es5 = new ttypesEs5.Xception({ errorCode: 1001, message: "test error" });
83+
const es6 = new ttypesEs6.Xception({ errorCode: 1001, message: "test error" });
84+
assert.equal(es5.name, es6.name, "name matches between ES5 and ES6");
85+
assert.equal(es5.errorCode, es6.errorCode, "errorCode matches between ES5 and ES6");
86+
assert.end();
87+
});

lib/nodejs/test/testAll.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ node ${DIR}/binary.test.js || TESTOK=1
133133
node ${DIR}/header.test.js || TESTOK=1
134134
node ${DIR}/int64.test.js || TESTOK=1
135135
node ${DIR}/deep-constructor.test.js || TESTOK=1
136+
node ${DIR}/generated-exceptions.test.js || TESTOK=1
136137
node ${DIR}/include.test.mjs || TESTOK=1
137138
node ${DIR}/thrift_4987_xhr_protocol.test.mjs || TESTOK=1
138139

0 commit comments

Comments
 (0)