|
| 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 | +} |
0 commit comments