Skip to content

Commit 103e1eb

Browse files
fix: Better error message when subscription path is invalid. (#87)
1 parent ba26a26 commit 103e1eb

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

src/main/java/com/google/cloud/pubsublite/spark/PslDataSourceOptions.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.google.cloud.pubsublite.internal.wire.ServiceClients.addDefaultSettings;
2020

21+
import com.google.api.gax.rpc.ApiException;
2122
import com.google.auto.value.AutoValue;
2223
import com.google.cloud.pubsublite.AdminClient;
2324
import com.google.cloud.pubsublite.AdminClientSettings;
@@ -79,9 +80,16 @@ public static PslDataSourceOptions fromSparkDataSourceOptions(DataSourceOptions
7980
options
8081
.get(Constants.MAX_MESSAGE_PER_BATCH_CONFIG_KEY)
8182
.ifPresent(mmpb -> builder.setMaxMessagesPerBatch(Long.parseLong(mmpb)));
83+
String subscriptionPathVal = options.get(Constants.SUBSCRIPTION_CONFIG_KEY).get();
84+
SubscriptionPath subscriptionPath;
85+
try {
86+
subscriptionPath = SubscriptionPath.parse(subscriptionPathVal);
87+
} catch (ApiException e) {
88+
throw new IllegalArgumentException(
89+
"Unable to parse subscription path " + subscriptionPathVal);
90+
}
8291
return builder
83-
.setSubscriptionPath(
84-
SubscriptionPath.parse(options.get(Constants.SUBSCRIPTION_CONFIG_KEY).get()))
92+
.setSubscriptionPath(subscriptionPath)
8593
.setFlowControlSettings(
8694
FlowControlSettings.builder()
8795
.setMessagesOutstanding(
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.pubsublite.spark;
18+
19+
import static org.junit.Assert.assertThrows;
20+
21+
import com.google.common.collect.ImmutableMap;
22+
import org.apache.spark.sql.sources.v2.DataSourceOptions;
23+
import org.junit.Test;
24+
25+
public class PslDataSourceOptionsTest {
26+
27+
@Test
28+
public void testInvalidSubPath() {
29+
DataSourceOptions options =
30+
new DataSourceOptions(ImmutableMap.of(Constants.SUBSCRIPTION_CONFIG_KEY, "invalid/path"));
31+
assertThrows(
32+
IllegalArgumentException.class,
33+
() -> PslDataSourceOptions.fromSparkDataSourceOptions(options));
34+
}
35+
}

0 commit comments

Comments
 (0)