blob: 9a3c54fb8095547fb473cdff738b6fab4dfd19db [file] [log] [blame]
Jens Geyer3babdd62026-04-09 01:35:12 +02001/*
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
22const test = require("tape");
23const thrift = require("thrift");
24
25// ES5 generated types (pre-ES6 path)
26const ttypesEs5 = require("./gen-nodejs/ThriftTest_types");
27// ES6 generated types
28const ttypesEs6 = require("./gen-nodejs-es6/ThriftTest_types");
29
30function 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
45test("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
55test("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]"
Jens Geyer98a71372026-04-09 01:50:39 +020059 assert.notEqual(
60 e.message,
61 "[object Object]",
62 "message is not '[object Object]' (would indicate args object was passed to super)",
63 );
Jens Geyer3babdd62026-04-09 01:35:12 +020064 assert.end();
65});
66
67test("ES6 generated exception - serialization does not throw", function t(assert) {
68 const e = new ttypesEs6.Xception({ errorCode: 1001, message: "test error" });
69 assert.doesNotThrow(function () {
70 serializeBinary(e);
71 }, "serializing an ES6 exception should not throw");
72 assert.end();
73});
74
75test("ES5 generated exception - constructor sets name and message correctly", function t(assert) {
76 const e = new ttypesEs5.Xception({ errorCode: 1001, message: "test error" });
77 assert.ok(e instanceof thrift.Thrift.TException, "is instanceof TException");
78 assert.ok(e instanceof Error, "is instanceof Error");
79 assert.equal(e.name, "Xception", "name is set to exception class name");
80 assert.equal(e.errorCode, 1001, "custom field errorCode is set");
81 assert.end();
82});
83
84test("ES5 and ES6 generated exceptions have consistent behavior", function t(assert) {
Jens Geyer98a71372026-04-09 01:50:39 +020085 const es5 = new ttypesEs5.Xception({
86 errorCode: 1001,
87 message: "test error",
88 });
89 const es6 = new ttypesEs6.Xception({
90 errorCode: 1001,
91 message: "test error",
92 });
Jens Geyer3babdd62026-04-09 01:35:12 +020093 assert.equal(es5.name, es6.name, "name matches between ES5 and ES6");
Jens Geyer98a71372026-04-09 01:50:39 +020094 assert.equal(
95 es5.errorCode,
96 es6.errorCode,
97 "errorCode matches between ES5 and ES6",
98 );
Jens Geyer3babdd62026-04-09 01:35:12 +020099 assert.end();
100});