This is an automated email from the ASF dual-hosted git repository.
baodi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar-client-node.git
The following commit(s) were added to refs/heads/master by this push:
new 64e44c1 [Fix] Wrap c++ exception when creating client. (#307)
64e44c1 is described below
commit 64e44c14fa2bfc525b277fcab02c9c4b9b38d3d7
Author: Baodi Shi <[email protected]>
AuthorDate: Tue Mar 21 17:10:29 2023 +0800
[Fix] Wrap c++ exception when creating client. (#307)
* [Fix] Wrapper exception when create client.
* code format
* Add unit test and fix judge
* code format
* Optimize unit test.
---
src/Client.cc | 19 +++++++++++--------
tests/client.test.js | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 64 insertions(+), 8 deletions(-)
diff --git a/src/Client.cc b/src/Client.cc
index 55ddf28..94b84a5 100644
--- a/src/Client.cc
+++ b/src/Client.cc
@@ -93,12 +93,11 @@ Client::Client(const Napi::CallbackInfo &info) :
Napi::ObjectWrap<Client>(info)
Napi::HandleScope scope(env);
Napi::Object clientConfig = info[0].As<Napi::Object>();
- if (!clientConfig.Has(CFG_SERVICE_URL) ||
!clientConfig.Get(CFG_SERVICE_URL).IsString()) {
- if (clientConfig.Get(CFG_SERVICE_URL).ToString().Utf8Value().empty()) {
- Napi::Error::New(env, "Service URL is required and must be specified as
a string")
- .ThrowAsJavaScriptException();
- return;
- }
+ if (!clientConfig.Has(CFG_SERVICE_URL) ||
!clientConfig.Get(CFG_SERVICE_URL).IsString() ||
+ clientConfig.Get(CFG_SERVICE_URL).ToString().Utf8Value().empty()) {
+ Napi::Error::New(env, "Service URL is required and must be specified as a
string")
+ .ThrowAsJavaScriptException();
+ return;
}
Napi::String serviceUrl = clientConfig.Get(CFG_SERVICE_URL).ToString();
@@ -186,8 +185,12 @@ Client::Client(const Napi::CallbackInfo &info) :
Napi::ObjectWrap<Client>(info)
pulsar_client_configuration_set_stats_interval_in_seconds(cClientConfig.get(),
statsIntervalInSeconds);
}
- this->cClient = std::shared_ptr<pulsar_client_t>(
- pulsar_client_create(serviceUrl.Utf8Value().c_str(),
cClientConfig.get()), pulsar_client_free);
+ try {
+ this->cClient = std::shared_ptr<pulsar_client_t>(
+ pulsar_client_create(serviceUrl.Utf8Value().c_str(),
cClientConfig.get()), pulsar_client_free);
+ } catch (const std::exception &e) {
+ Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
+ }
}
Client::~Client() {}
diff --git a/tests/client.test.js b/tests/client.test.js
new file mode 100644
index 0000000..03e46d1
--- /dev/null
+++ b/tests/client.test.js
@@ -0,0 +1,53 @@
+/**
+ * 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.
+ */
+
+const Pulsar = require('../index.js');
+
+(() => {
+ describe('Client', () => {
+ describe('CreateFailedByUrlSetIncorrect', () => {
+ test('No Set Url', async () => {
+ await expect(() => new Pulsar.Client({
+ operationTimeoutSeconds: 30,
+ })).toThrow('Service URL is required and must be specified as a
string');
+ });
+
+ test('Set empty url', async () => {
+ await expect(() => new Pulsar.Client({
+ serviceUrl: '',
+ operationTimeoutSeconds: 30,
+ })).toThrow('Service URL is required and must be specified as a
string');
+ });
+
+ test('Set invalid url', async () => {
+ await expect(() => new Pulsar.Client({
+ serviceUrl: 'invalid://localhost:6655',
+ operationTimeoutSeconds: 30,
+ })).toThrow('Invalid scheme: invalid');
+ });
+
+ test('Set not string url', async () => {
+ await expect(() => new Pulsar.Client({
+ serviceUrl: -1,
+ operationTimeoutSeconds: 30,
+ })).toThrow('Service URL is required and must be specified as a
string');
+ });
+ });
+ });
+})();