Skip to content

Commit

Permalink
Merge pull request #1481 from data-integrations/improve-error-message
Browse files Browse the repository at this point in the history
[PLUGINk-1807] Add resource name in validation error message
  • Loading branch information
itsankit-google authored Jan 7, 2025
2 parents c5f4d69 + 16a22b1 commit 204080b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ public static Table getBigQueryTable(String projectId, String dataset, String ta
*/
public static void validateBucket(String bucket, String bucketPropertyName, FailureCollector collector) {
// Allowed character validation for bucket name as per https://cloud.google.com/storage/docs/naming
String errorMessage = "Bucket name can only contain lowercase letters, numbers, '.', '_', and '-'.";
String errorMessage = "Bucket name '%s' can only contain lowercase letters, numbers, '.', '_', and '-'.";
match(bucket, bucketPropertyName, BUCKET_PATTERN, collector, errorMessage);
}

Expand All @@ -685,7 +685,7 @@ public static void validateBucket(String bucket, String bucketPropertyName, Fail
*/
public static void validateDataset(String dataset, String datasetPropertyName, FailureCollector collector) {
// Allowed character validation for dataset name as per https://cloud.google.com/bigquery/docs/datasets
String errorMessage = "Dataset name can only contain letters (lower or uppercase), numbers and '_'.";
String errorMessage = "Dataset name '%s' can only contain letters (lower or uppercase), numbers and '_'.";
match(dataset, datasetPropertyName, DATASET_PATTERN, collector, errorMessage);
}

Expand All @@ -698,7 +698,7 @@ public static void validateDataset(String dataset, String datasetPropertyName, F
*/
public static void validateTable(String table, String tablePropertyName, FailureCollector collector) {
// Allowed character validation for table name as per https://cloud.google.com/bigquery/docs/tables
String errorMessage = "Table name can only contain letters (lower or uppercase), numbers, '_' and '-'.";
String errorMessage = "Table name '%s' can only contain letters (lower or uppercase), numbers, '_' and '-'.";
match(table, tablePropertyName, TABLE_PATTERN, collector, errorMessage);
}

Expand Down Expand Up @@ -739,7 +739,7 @@ private static void match(String text, String propertyName, String pattern,
if (!Strings.isNullOrEmpty(text)) {
Pattern p = Pattern.compile(pattern);
if (!p.matcher(text).matches()) {
collector.addFailure(errorMessage, null).withConfigProperty(propertyName);
collector.addFailure(String.format(errorMessage, text), null).withConfigProperty(propertyName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ private ProgramFailureException getProgramFailureException(HttpResponseException
}

private String getErrorMessage(GoogleJsonResponseException exception) {
if (!Strings.isNullOrEmpty(exception.getMessage())) {
return exception.getMessage();
}
if (exception.getDetails() != null) {
try {
return exception.getDetails().toPrettyString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class BigQuerySinkConfigTest {
Method validateTimePartitioningColumnMethod;
Map<String, String> arguments;
private static final String invalidTableNameErrorMessage =
"Table name can only contain letters (lower or uppercase), numbers, '_' and '-'.";
"Table name '%s' can only contain letters (lower or uppercase), numbers, '_' and '-'.";

@Before
public void setup() throws NoSuchMethodException {
Expand Down Expand Up @@ -297,7 +297,8 @@ public void testValidateTableNameWithInvalidTableName() {
config = configBuilder.setTable(tableName).build();
config.validate(collector);
Assert.assertEquals(1, collector.getValidationFailures().size());
Assert.assertEquals(invalidTableNameErrorMessage, collector.getValidationFailures().get(0).getMessage());
Assert.assertEquals(String.format(invalidTableNameErrorMessage, tableName),
collector.getValidationFailures().get(0).getMessage());
}
}

Expand Down

0 comments on commit 204080b

Please sign in to comment.