Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions lib/nodejs/test/generated-exceptions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

"use strict";

const test = require("tape");
const thrift = require("thrift");

// ES5 generated types (pre-ES6 path)
const ttypesEs5 = require("./gen-nodejs/ThriftTest_types");
// ES6 generated types
const ttypesEs6 = require("./gen-nodejs-es6/ThriftTest_types");

function serializeBinary(data) {
let buff;
const transport = new thrift.TBufferedTransport(null, function (msg) {
buff = msg;
});
const prot = new thrift.TBinaryProtocol(transport);
data[Symbol.for("write")](prot);
prot.flush();
return buff;
}

// Test that ES6 generated exception constructor passes the exception name
// (not the args object) to super(), matching the ES5 behavior.
// Regression test for: https://github.com/apache/thrift/pull/3372

test("ES6 generated exception - constructor sets name and message correctly", function t(assert) {
const e = new ttypesEs6.Xception({ errorCode: 1001, message: "test error" });
assert.ok(e instanceof thrift.Thrift.TException, "is instanceof TException");
assert.ok(e instanceof Error, "is instanceof Error");
assert.equal(e.name, "Xception", "name is set to exception class name");
assert.equal(e.errorCode, 1001, "custom field errorCode is set");
assert.equal(typeof e.stack, "string", "has stack trace");
assert.end();
});

test("ES6 generated exception - super() receives string, not args object", function t(assert) {
const e = new ttypesEs6.Xception({ errorCode: 1001, message: "test error" });
// The bug was that super(args) passed the args object to TException,
// which would cause message to be "[object Object]"
assert.notEqual(
e.message,
"[object Object]",
"message is not '[object Object]' (would indicate args object was passed to super)",
);
assert.end();
});

test("ES6 generated exception - serialization does not throw", function t(assert) {
const e = new ttypesEs6.Xception({ errorCode: 1001, message: "test error" });
assert.doesNotThrow(function () {
serializeBinary(e);
}, "serializing an ES6 exception should not throw");
assert.end();
});

test("ES5 generated exception - constructor sets name and message correctly", function t(assert) {
const e = new ttypesEs5.Xception({ errorCode: 1001, message: "test error" });
assert.ok(e instanceof thrift.Thrift.TException, "is instanceof TException");
assert.ok(e instanceof Error, "is instanceof Error");
assert.equal(e.name, "Xception", "name is set to exception class name");
assert.equal(e.errorCode, 1001, "custom field errorCode is set");
assert.end();
});

test("ES5 and ES6 generated exceptions have consistent behavior", function t(assert) {
const es5 = new ttypesEs5.Xception({
errorCode: 1001,
message: "test error",
});
const es6 = new ttypesEs6.Xception({
errorCode: 1001,
message: "test error",
});
assert.equal(es5.name, es6.name, "name matches between ES5 and ES6");
assert.equal(
es5.errorCode,
es6.errorCode,
"errorCode matches between ES5 and ES6",
);
assert.end();
});
1 change: 1 addition & 0 deletions lib/nodejs/test/testAll.sh
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ node ${DIR}/binary.test.js || TESTOK=1
node ${DIR}/header.test.js || TESTOK=1
node ${DIR}/int64.test.js || TESTOK=1
node ${DIR}/deep-constructor.test.js || TESTOK=1
node ${DIR}/generated-exceptions.test.js || TESTOK=1
node ${DIR}/include.test.mjs || TESTOK=1
node ${DIR}/thrift_4987_xhr_protocol.test.mjs || TESTOK=1

Expand Down
Loading