Skip to content

Commit c011917

Browse files
committed
Add PostgresErrorDetailsProvider
1 parent cc7eb3b commit c011917

File tree

5 files changed

+111
-4
lines changed

5 files changed

+111
-4
lines changed

database-commons/src/main/java/io/cdap/plugin/db/DBErrorDetailsProvider.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ private ProgramFailureException getProgramFailureException(SQLException e, Error
7575
errorMessageWithDetails = String.format("%s For more details, see %s", errorMessageWithDetails,
7676
externalDocumentationLink);
7777
}
78-
return ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN),
79-
errorMessage, errorMessageWithDetails, getErrorTypeFromErrorCode(errorCode), false, e);
78+
79+
return ErrorUtils.getProgramFailureException(getErrorCategoryFromSqlState(sqlState), errorMessage,
80+
errorMessageWithDetails, getErrorTypeFromErrorCode(errorCode, sqlState), false, e);
8081
}
8182

8283
/**
@@ -118,7 +119,11 @@ protected String getExternalDocumentationLink() {
118119
return null;
119120
}
120121

121-
protected ErrorType getErrorTypeFromErrorCode(int errorCode) {
122+
protected ErrorType getErrorTypeFromErrorCode(int errorCode, String sqlState) {
122123
return ErrorType.UNKNOWN;
123124
}
125+
126+
protected ErrorCategory getErrorCategoryFromSqlState(String sqlState) {
127+
return new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN);
128+
}
124129
}

mysql-plugin/src/main/java/io/cdap/plugin/mysql/MysqlErrorDetailsProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected String getExternalDocumentationLink() {
3030
}
3131

3232
@Override
33-
protected ErrorType getErrorTypeFromErrorCode(int errorCode) {
33+
protected ErrorType getErrorTypeFromErrorCode(int errorCode, String sqlState) {
3434
// https://dev.mysql.com/doc/refman/9.0/en/error-message-elements.html#error-code-ranges
3535
if (errorCode >= 1000 && errorCode <= 5999) {
3636
return ErrorType.USER;
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright © 2024 Cask Data, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* 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, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package io.cdap.plugin.postgres;
18+
19+
import io.cdap.cdap.api.exception.ErrorCategory;
20+
import io.cdap.cdap.api.exception.ErrorType;
21+
import io.cdap.plugin.db.DBErrorDetailsProvider;
22+
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
26+
/**
27+
* A custom ErrorDetailsProvider for Postgres plugins.
28+
*/
29+
public class PostgresErrorDetailsProvider extends DBErrorDetailsProvider {
30+
// https://www.postgresql.org/docs/current/errcodes-appendix.html
31+
private static final Map<String, ErrorType> ERROR_CODE_TO_ERROR_TYPE;
32+
private static final Map<String, ErrorCategory> ERROR_CODE_TO_ERROR_CATEGORY;
33+
static {
34+
ERROR_CODE_TO_ERROR_TYPE = new HashMap<>();
35+
ERROR_CODE_TO_ERROR_TYPE.put("01", ErrorType.USER);
36+
ERROR_CODE_TO_ERROR_TYPE.put("02", ErrorType.USER);
37+
ERROR_CODE_TO_ERROR_TYPE.put("08", ErrorType.SYSTEM);
38+
ERROR_CODE_TO_ERROR_TYPE.put("0A", ErrorType.USER);
39+
ERROR_CODE_TO_ERROR_TYPE.put("22", ErrorType.USER);
40+
ERROR_CODE_TO_ERROR_TYPE.put("23", ErrorType.USER);
41+
ERROR_CODE_TO_ERROR_TYPE.put("28", ErrorType.USER);
42+
ERROR_CODE_TO_ERROR_TYPE.put("40", ErrorType.SYSTEM);
43+
ERROR_CODE_TO_ERROR_TYPE.put("42", ErrorType.USER);
44+
ERROR_CODE_TO_ERROR_TYPE.put("53", ErrorType.SYSTEM);
45+
ERROR_CODE_TO_ERROR_TYPE.put("54", ErrorType.SYSTEM);
46+
ERROR_CODE_TO_ERROR_TYPE.put("55", ErrorType.USER);
47+
ERROR_CODE_TO_ERROR_TYPE.put("57", ErrorType.SYSTEM);
48+
ERROR_CODE_TO_ERROR_TYPE.put("58", ErrorType.SYSTEM);
49+
ERROR_CODE_TO_ERROR_TYPE.put("P0", ErrorType.SYSTEM);
50+
ERROR_CODE_TO_ERROR_TYPE.put("XX", ErrorType.SYSTEM);
51+
52+
ErrorCategory.ErrorCategoryEnum plugin = ErrorCategory.ErrorCategoryEnum.PLUGIN;
53+
ERROR_CODE_TO_ERROR_CATEGORY = new HashMap<>();
54+
ERROR_CODE_TO_ERROR_CATEGORY.put("01", new ErrorCategory(plugin, "Warning"));
55+
ERROR_CODE_TO_ERROR_CATEGORY.put("02", new ErrorCategory(plugin, "No Data"));
56+
ERROR_CODE_TO_ERROR_CATEGORY.put("08", new ErrorCategory(plugin, "Connection Exception"));
57+
ERROR_CODE_TO_ERROR_CATEGORY.put("0A", new ErrorCategory(plugin, "Feature Not Supported"));
58+
ERROR_CODE_TO_ERROR_CATEGORY.put("22", new ErrorCategory(plugin, "Data Exception"));
59+
ERROR_CODE_TO_ERROR_CATEGORY.put("23", new ErrorCategory(plugin, "Integrity Constraint Violation"));
60+
ERROR_CODE_TO_ERROR_CATEGORY.put("28", new ErrorCategory(plugin, "Invalid Authorization Specification"));
61+
ERROR_CODE_TO_ERROR_CATEGORY.put("40", new ErrorCategory(plugin, "Transaction Rollback"));
62+
ERROR_CODE_TO_ERROR_CATEGORY.put("42", new ErrorCategory(plugin, "Syntax Error or Access Rule Violation"));
63+
ERROR_CODE_TO_ERROR_CATEGORY.put("53", new ErrorCategory(plugin, "Insufficient Resources"));
64+
ERROR_CODE_TO_ERROR_CATEGORY.put("54", new ErrorCategory(plugin, "Program Limit Exceeded"));
65+
ERROR_CODE_TO_ERROR_CATEGORY.put("55", new ErrorCategory(plugin, "Object Not in Prerequisite State"));
66+
ERROR_CODE_TO_ERROR_CATEGORY.put("57", new ErrorCategory(plugin, "Operator Intervention"));
67+
ERROR_CODE_TO_ERROR_CATEGORY.put("58", new ErrorCategory(plugin, "System Error"));
68+
ERROR_CODE_TO_ERROR_CATEGORY.put("P0", new ErrorCategory(plugin, "PL/pgSQL Error"));
69+
ERROR_CODE_TO_ERROR_CATEGORY.put("XX", new ErrorCategory(plugin, "Internal Error"));
70+
}
71+
72+
@Override
73+
protected String getExternalDocumentationLink() {
74+
return "https://www.postgresql.org/docs/current/errcodes-appendix.html";
75+
}
76+
77+
@Override
78+
protected ErrorType getErrorTypeFromErrorCode(int errorCode, String sqlState) {
79+
if (sqlState.length() >= 2 && ERROR_CODE_TO_ERROR_TYPE.containsKey(sqlState.substring(0, 2))) {
80+
return ERROR_CODE_TO_ERROR_TYPE.get(sqlState.substring(0, 2));
81+
}
82+
return ErrorType.UNKNOWN;
83+
}
84+
85+
@Override
86+
protected ErrorCategory getErrorCategoryFromSqlState(String sqlState) {
87+
if (sqlState.length() >= 2 && ERROR_CODE_TO_ERROR_CATEGORY.containsKey(sqlState.substring(0, 2))) {
88+
return ERROR_CODE_TO_ERROR_CATEGORY.get(sqlState.substring(0, 2));
89+
}
90+
return new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN);
91+
}
92+
}

postgresql-plugin/src/main/java/io/cdap/plugin/postgres/PostgresSink.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ protected LineageRecorder getLineageRecorder(BatchSinkContext context) {
116116
return new LineageRecorder(context, asset);
117117
}
118118

119+
@Override
120+
protected String getErrorDetailsProviderClassName() {
121+
return PostgresErrorDetailsProvider.class.getName();
122+
}
123+
119124
/**
120125
* PostgreSQL action configuration.
121126
*/

postgresql-plugin/src/main/java/io/cdap/plugin/postgres/PostgresSource.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ protected SchemaReader getSchemaReader() {
6767
return new PostgresSchemaReader();
6868
}
6969

70+
@Override
71+
protected String getErrorDetailsProviderClassName() {
72+
return PostgresErrorDetailsProvider.class.getName();
73+
}
74+
7075
@Override
7176
protected Class<? extends DBWritable> getDBRecordType() {
7277
return PostgresDBRecord.class;

0 commit comments

Comments
 (0)