blob: 21049168729128e5f7754144be84cc99710fc304 [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]"
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
64test("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
72test("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
81test("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});