Skip to content

Commit 95f0ed7

Browse files
authored
Make DLP Java samples exception handling consistent (GoogleCloudPlatform#3060)
Fixes internal bug b/158239329 Also follows up on a comment from @lesv on GoogleCloudPlatform#3055 - [X] I have followed [Sample Format Guide](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/SAMPLE_FORMAT.md) - [X] `pom.xml` parent set to latest `shared-configuration` - [X] Appropriate changes to README are included in PR - [X] API's need to be enabled to test (tell us) (**Nothing New**) - [X] Environment Variables need to be set (ask us to set them) (**Nothing New**) - [X] Tests pass (`mvn -P lint clean verify`) * (Note- `Checkstyle` passing is required; `Spotbugs`, `ErrorProne`, `PMD`, etc. `ERROR`'s are advisory only) - [X] Please **merge** this PR for me once it is approved.
1 parent 319f03a commit 95f0ed7

19 files changed

+70
-62
lines changed

dlp/src/main/java/dlp/snippets/InspectImageFile.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,21 @@
3030
import com.google.privacy.dlp.v2.LocationName;
3131
import com.google.protobuf.ByteString;
3232
import java.io.FileInputStream;
33+
import java.io.IOException;
3334
import java.util.ArrayList;
3435
import java.util.List;
3536

3637
public class InspectImageFile {
3738

38-
public static void inspectImageFile() {
39+
public static void inspectImageFile() throws IOException {
3940
// TODO(developer): Replace these variables before running the sample.
4041
String projectId = "your-project-id";
4142
String filePath = "path/to/image.png";
4243
inspectImageFile(projectId, filePath);
4344
}
4445

4546
// Inspects the specified image file.
46-
public static void inspectImageFile(String projectId, String filePath) {
47+
public static void inspectImageFile(String projectId, String filePath) throws IOException {
4748
// Initialize client that will be used to send requests. This client only needs to be created
4849
// once, and can be reused for multiple requests. After completing all of your requests, call
4950
// the "close" method on the client to safely clean up any remaining background resources.
@@ -83,8 +84,6 @@ public static void inspectImageFile(String projectId, String filePath) {
8384
System.out.println("\tInfo type: " + f.getInfoType().getName());
8485
System.out.println("\tLikelihood: " + f.getLikelihood());
8586
}
86-
} catch (Exception e) {
87-
System.out.println("Error during inspectImageFile: \n" + e.toString());
8887
}
8988
}
9089
}

dlp/src/main/java/dlp/snippets/InspectPhoneNumber.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,19 @@
2727
import com.google.privacy.dlp.v2.InspectContentResponse;
2828
import com.google.privacy.dlp.v2.Likelihood;
2929
import com.google.privacy.dlp.v2.LocationName;
30+
import java.io.IOException;
3031

3132
public class InspectPhoneNumber {
3233

33-
public static void inspectString() {
34+
public static void inspectString() throws IOException {
3435
// TODO(developer): Replace these variables before running the sample.
3536
String projectId = "your-project-id";
3637
String textToInspect = "My name is Gary and my email is gary@example.com";
3738
inspectString(projectId, textToInspect);
3839
}
3940

4041
// Inspects the provided text.
41-
public static void inspectString(String projectId, String textToInspect) {
42+
public static void inspectString(String projectId, String textToInspect) throws IOException {
4243
// Initialize client that will be used to send requests. This client only needs to be created
4344
// once, and can be reused for multiple requests. After completing all of your requests, call
4445
// the "close" method on the client to safely clean up any remaining background resources.
@@ -78,8 +79,6 @@ public static void inspectString(String projectId, String textToInspect) {
7879
System.out.println("\tInfo type: " + f.getInfoType().getName());
7980
System.out.println("\tLikelihood: " + f.getLikelihood());
8081
}
81-
} catch (Exception e) {
82-
System.out.println("Error during inspectString: \n" + e.toString());
8382
}
8483
}
8584
}

dlp/src/main/java/dlp/snippets/InspectString.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,21 @@
2929
import com.google.privacy.dlp.v2.InspectContentResponse;
3030
import com.google.privacy.dlp.v2.LocationName;
3131
import com.google.protobuf.ByteString;
32+
import java.io.IOException;
3233
import java.util.ArrayList;
3334
import java.util.List;
3435

3536
public class InspectString {
3637

37-
public static void inspectString() {
38+
public static void inspectString() throws IOException {
3839
// TODO(developer): Replace these variables before running the sample.
3940
String projectId = "your-project-id";
4041
String textToInspect = "My name is Gary and my email is gary@example.com";
4142
inspectString(projectId, textToInspect);
4243
}
4344

4445
// Inspects the provided text.
45-
public static void inspectString(String projectId, String textToInspect) {
46+
public static void inspectString(String projectId, String textToInspect) throws IOException {
4647
// Initialize client that will be used to send requests. This client only needs to be created
4748
// once, and can be reused for multiple requests. After completing all of your requests, call
4849
// the "close" method on the client to safely clean up any remaining background resources.
@@ -84,8 +85,6 @@ public static void inspectString(String projectId, String textToInspect) {
8485
System.out.println("\tInfo type: " + f.getInfoType().getName());
8586
System.out.println("\tLikelihood: " + f.getLikelihood());
8687
}
87-
} catch (Exception e) {
88-
System.out.println("Error during inspectString: \n" + e.toString());
8988
}
9089
}
9190
}

dlp/src/main/java/dlp/snippets/InspectStringCustomExcludingSubstring.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@
3737
import com.google.privacy.dlp.v2.LocationName;
3838
import com.google.privacy.dlp.v2.MatchingType;
3939
import com.google.protobuf.ByteString;
40+
import java.io.IOException;
4041
import java.util.Arrays;
4142
import java.util.List;
4243

4344
public class InspectStringCustomExcludingSubstring {
4445

45-
public static void inspectStringCustomExcludingSubstring() {
46+
public static void inspectStringCustomExcludingSubstring() throws IOException {
4647
// TODO(developer): Replace these variables before running the sample.
4748
String projectId = "your-project-id";
4849
String textToInspect = "Name: Doe, John. Name: Example, Jimmy";
@@ -54,7 +55,7 @@ public static void inspectStringCustomExcludingSubstring() {
5455

5556
// Inspects the provided text, avoiding matches specified in the exclusion list.
5657
public static void inspectStringCustomExcludingSubstring(String projectId, String textToInspect,
57-
String customDetectorPattern, List<String> excludedSubstringList) {
58+
String customDetectorPattern, List<String> excludedSubstringList) throws IOException {
5859
// Initialize client that will be used to send requests. This client only needs to be created
5960
// once, and can be reused for multiple requests. After completing all of your requests, call
6061
// the "close" method on the client to safely clean up any remaining background resources.
@@ -112,8 +113,6 @@ public static void inspectStringCustomExcludingSubstring(String projectId, Strin
112113
System.out.println("\tInfo type: " + f.getInfoType().getName());
113114
System.out.println("\tLikelihood: " + f.getLikelihood());
114115
}
115-
} catch (Exception e) {
116-
System.out.println("Error during inspectString: \n" + e.toString());
117116
}
118117
}
119118
}

dlp/src/main/java/dlp/snippets/InspectStringWithExclusionDict.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@
3535
import com.google.privacy.dlp.v2.LocationName;
3636
import com.google.privacy.dlp.v2.MatchingType;
3737
import com.google.protobuf.ByteString;
38+
import java.io.IOException;
3839
import java.util.ArrayList;
3940
import java.util.Arrays;
4041
import java.util.List;
4142

4243
public class InspectStringWithExclusionDict {
4344

44-
public static void inspectStringWithExclusionDict() {
45+
public static void inspectStringWithExclusionDict() throws IOException {
4546
// TODO(developer): Replace these variables before running the sample.
4647
String projectId = "your-project-id";
4748
String textToInspect = "Some email addresses: gary@example.com, example@example.com";
@@ -51,7 +52,7 @@ public static void inspectStringWithExclusionDict() {
5152

5253
// Inspects the provided text, avoiding matches specified in the exclusion list.
5354
public static void inspectStringWithExclusionDict(String projectId, String textToInspect,
54-
List<String> excludedMatchList) {
55+
List<String> excludedMatchList) throws IOException {
5556
// Initialize client that will be used to send requests. This client only needs to be created
5657
// once, and can be reused for multiple requests. After completing all of your requests, call
5758
// the "close" method on the client to safely clean up any remaining background resources.
@@ -110,8 +111,6 @@ public static void inspectStringWithExclusionDict(String projectId, String textT
110111
System.out.println("\tInfo type: " + f.getInfoType().getName());
111112
System.out.println("\tLikelihood: " + f.getLikelihood());
112113
}
113-
} catch (Exception e) {
114-
System.out.println("Error during inspectString: \n" + e.toString());
115114
}
116115
}
117116
}

dlp/src/main/java/dlp/snippets/InspectStringWithExclusionDictSubstring.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@
3535
import com.google.privacy.dlp.v2.LocationName;
3636
import com.google.privacy.dlp.v2.MatchingType;
3737
import com.google.protobuf.ByteString;
38+
import java.io.IOException;
3839
import java.util.ArrayList;
3940
import java.util.Arrays;
4041
import java.util.List;
4142

4243
public class InspectStringWithExclusionDictSubstring {
4344

44-
public static void inspectStringWithExclusionDictSubstring() {
45+
public static void inspectStringWithExclusionDictSubstring() throws IOException {
4546
// TODO(developer): Replace these variables before running the sample.
4647
String projectId = "your-project-id";
4748
String textToInspect = "Some email addresses: gary@example.com, TEST@example.com";
@@ -51,7 +52,7 @@ public static void inspectStringWithExclusionDictSubstring() {
5152

5253
// Inspects the provided text, avoiding matches specified in the exclusion list.
5354
public static void inspectStringWithExclusionDictSubstring(String projectId, String textToInspect,
54-
List<String> excludedSubstringList) {
55+
List<String> excludedSubstringList) throws IOException {
5556
// Initialize client that will be used to send requests. This client only needs to be created
5657
// once, and can be reused for multiple requests. After completing all of your requests, call
5758
// the "close" method on the client to safely clean up any remaining background resources.
@@ -111,8 +112,6 @@ public static void inspectStringWithExclusionDictSubstring(String projectId, Str
111112
System.out.println("\tInfo type: " + f.getInfoType().getName());
112113
System.out.println("\tLikelihood: " + f.getLikelihood());
113114
}
114-
} catch (Exception e) {
115-
System.out.println("Error during inspectString: \n" + e.toString());
116115
}
117116
}
118117
}

dlp/src/main/java/dlp/snippets/InspectStringWithExclusionRegex.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@
3434
import com.google.privacy.dlp.v2.LocationName;
3535
import com.google.privacy.dlp.v2.MatchingType;
3636
import com.google.protobuf.ByteString;
37+
import java.io.IOException;
3738
import java.util.ArrayList;
3839
import java.util.List;
3940

4041
public class InspectStringWithExclusionRegex {
4142

42-
public static void inspectStringWithExclusionRegex() {
43+
public static void inspectStringWithExclusionRegex() throws IOException {
4344
// TODO(developer): Replace these variables before running the sample.
4445
String projectId = "your-project-id";
4546
String textToInspect = "Some email addresses: gary@example.com, bob@example.org";
@@ -49,7 +50,7 @@ public static void inspectStringWithExclusionRegex() {
4950

5051
// Inspects the provided text, avoiding matches specified in the exclusion list.
5152
public static void inspectStringWithExclusionRegex(String projectId, String textToInspect,
52-
String excludedRegex) {
53+
String excludedRegex) throws IOException {
5354
// Initialize client that will be used to send requests. This client only needs to be created
5455
// once, and can be reused for multiple requests. After completing all of your requests, call
5556
// the "close" method on the client to safely clean up any remaining background resources.
@@ -107,8 +108,6 @@ public static void inspectStringWithExclusionRegex(String projectId, String text
107108
System.out.println("\tInfo type: " + f.getInfoType().getName());
108109
System.out.println("\tLikelihood: " + f.getLikelihood());
109110
}
110-
} catch (Exception e) {
111-
System.out.println("Error during inspectString: \n" + e.toString());
112111
}
113112
}
114113
}

dlp/src/main/java/dlp/snippets/InspectTextFile.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,21 @@
3030
import com.google.privacy.dlp.v2.LocationName;
3131
import com.google.protobuf.ByteString;
3232
import java.io.FileInputStream;
33+
import java.io.IOException;
3334
import java.util.ArrayList;
3435
import java.util.List;
3536

3637
public class InspectTextFile {
3738

38-
public static void inspectTextFile() {
39+
public static void inspectTextFile() throws IOException {
3940
// TODO(developer): Replace these variables before running the sample.
4041
String projectId = "your-project-id";
4142
String filePath = "path/to/file.txt";
4243
inspectTextFile(projectId, filePath);
4344
}
4445

4546
// Inspects the specified text file.
46-
public static void inspectTextFile(String projectId, String filePath) {
47+
public static void inspectTextFile(String projectId, String filePath) throws IOException {
4748
// Initialize client that will be used to send requests. This client only needs to be created
4849
// once, and can be reused for multiple requests. After completing all of your requests, call
4950
// the "close" method on the client to safely clean up any remaining background resources.
@@ -83,8 +84,6 @@ public static void inspectTextFile(String projectId, String filePath) {
8384
System.out.println("\tInfo type: " + f.getInfoType().getName());
8485
System.out.println("\tLikelihood: " + f.getLikelihood());
8586
}
86-
} catch (Exception e) {
87-
System.out.println("Error during inspectFile: \n" + e.toString());
8887
}
8988
}
9089
}

dlp/src/main/java/dlp/snippets/RedactImageFile.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,22 @@
3030
import com.google.protobuf.ByteString;
3131
import java.io.FileInputStream;
3232
import java.io.FileOutputStream;
33+
import java.io.IOException;
3334
import java.util.ArrayList;
3435
import java.util.List;
3536

3637
class RedactImageFile {
3738

38-
public static void main(String[] args) {
39+
public static void main(String[] args) throws IOException {
3940
// TODO(developer): Replace these variables before running the sample.
4041
String projectId = "my-project-id";
4142
String inputPath = "src/test/resources/test.png";
4243
String outputPath = "redacted.png";
4344
redactImageFile(projectId, inputPath, outputPath);
4445
}
4546

46-
static void redactImageFile(String projectId, String inputPath, String outputPath) {
47+
static void redactImageFile(String projectId, String inputPath, String outputPath)
48+
throws IOException {
4749
// Initialize client that will be used to send requests. This client only needs to be created
4850
// once, and can be reused for multiple requests. After completing all of your requests, call
4951
// the "close" method on the client to safely clean up any remaining background resources.
@@ -82,8 +84,6 @@ static void redactImageFile(String projectId, String inputPath, String outputPat
8284
redacted.close();
8385
System.out.println("Redacted image written to " + outputPath);
8486

85-
} catch (Exception e) {
86-
System.out.println("Error during inspectFile: \n" + e.toString());
8787
}
8888
}
8989
}

dlp/src/main/java/dlp/snippets/RiskAnalysisCategoricalStats.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,16 @@
3939
import com.google.pubsub.v1.ProjectSubscriptionName;
4040
import com.google.pubsub.v1.ProjectTopicName;
4141
import com.google.pubsub.v1.PubsubMessage;
42+
import java.io.IOException;
4243
import java.util.List;
44+
import java.util.concurrent.ExecutionException;
4345
import java.util.concurrent.TimeUnit;
4446
import java.util.concurrent.TimeoutException;
4547

4648
class RiskAnalysisCategoricalStats {
4749

48-
public static void categoricalStatsAnalysis() throws Exception {
50+
public static void categoricalStatsAnalysis()
51+
throws InterruptedException, ExecutionException, IOException {
4952
// TODO(developer): Replace these variables before running the sample.
5053
String projectId = "your-project-id";
5154
String datasetId = "your-bigquery-dataset-id";
@@ -57,7 +60,7 @@ public static void categoricalStatsAnalysis() throws Exception {
5760

5861
public static void categoricalStatsAnalysis(
5962
String projectId, String datasetId, String tableId, String topicId, String subscriptionId)
60-
throws Exception {
63+
throws ExecutionException, InterruptedException, IOException {
6164
// Initialize client that will be used to send requests. This client only needs to be created
6265
// once, and can be reused for multiple requests. After completing all of your requests, call
6366
// the "close" method on the client to safely clean up any remaining background resources.

0 commit comments

Comments
 (0)