|
| 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 com.google.common.base.Strings; |
| 20 | +import io.cdap.cdap.api.exception.ErrorCategory; |
| 21 | +import io.cdap.cdap.api.exception.ErrorType; |
| 22 | +import io.cdap.plugin.db.DBErrorDetailsProvider; |
| 23 | +import io.cdap.plugin.util.DBUtils; |
| 24 | + |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +/** |
| 29 | + * A custom ErrorDetailsProvider for Postgres plugins. |
| 30 | + */ |
| 31 | +public class PostgresErrorDetailsProvider extends DBErrorDetailsProvider { |
| 32 | + // https://www.postgresql.org/docs/current/errcodes-appendix.html |
| 33 | + private static final Map<String, ErrorType> ERROR_CODE_TO_ERROR_TYPE; |
| 34 | + private static final Map<String, ErrorCategory> ERROR_CODE_TO_ERROR_CATEGORY; |
| 35 | + static { |
| 36 | + ERROR_CODE_TO_ERROR_TYPE = new HashMap<>(); |
| 37 | + ERROR_CODE_TO_ERROR_TYPE.put("01", ErrorType.USER); |
| 38 | + ERROR_CODE_TO_ERROR_TYPE.put("02", ErrorType.USER); |
| 39 | + ERROR_CODE_TO_ERROR_TYPE.put("08", ErrorType.SYSTEM); |
| 40 | + ERROR_CODE_TO_ERROR_TYPE.put("0A", ErrorType.USER); |
| 41 | + ERROR_CODE_TO_ERROR_TYPE.put("22", ErrorType.USER); |
| 42 | + ERROR_CODE_TO_ERROR_TYPE.put("23", ErrorType.USER); |
| 43 | + ERROR_CODE_TO_ERROR_TYPE.put("28", ErrorType.USER); |
| 44 | + ERROR_CODE_TO_ERROR_TYPE.put("40", ErrorType.SYSTEM); |
| 45 | + ERROR_CODE_TO_ERROR_TYPE.put("42", ErrorType.USER); |
| 46 | + ERROR_CODE_TO_ERROR_TYPE.put("53", ErrorType.SYSTEM); |
| 47 | + ERROR_CODE_TO_ERROR_TYPE.put("54", ErrorType.SYSTEM); |
| 48 | + ERROR_CODE_TO_ERROR_TYPE.put("55", ErrorType.USER); |
| 49 | + ERROR_CODE_TO_ERROR_TYPE.put("57", ErrorType.SYSTEM); |
| 50 | + ERROR_CODE_TO_ERROR_TYPE.put("58", ErrorType.SYSTEM); |
| 51 | + ERROR_CODE_TO_ERROR_TYPE.put("P0", ErrorType.SYSTEM); |
| 52 | + ERROR_CODE_TO_ERROR_TYPE.put("XX", ErrorType.SYSTEM); |
| 53 | + |
| 54 | + ErrorCategory.ErrorCategoryEnum plugin = ErrorCategory.ErrorCategoryEnum.PLUGIN; |
| 55 | + ERROR_CODE_TO_ERROR_CATEGORY = new HashMap<>(); |
| 56 | + ERROR_CODE_TO_ERROR_CATEGORY.put("01", new ErrorCategory(plugin, "Warning")); |
| 57 | + ERROR_CODE_TO_ERROR_CATEGORY.put("02", new ErrorCategory(plugin, "No Data")); |
| 58 | + ERROR_CODE_TO_ERROR_CATEGORY.put("08", new ErrorCategory(plugin, "Postgres Server Connection Exception")); |
| 59 | + ERROR_CODE_TO_ERROR_CATEGORY.put("0A", new ErrorCategory(plugin, "Postgres Server Feature Not Supported")); |
| 60 | + ERROR_CODE_TO_ERROR_CATEGORY.put("22", new ErrorCategory(plugin, "Postgres Server Data Exception")); |
| 61 | + ERROR_CODE_TO_ERROR_CATEGORY.put("23", new ErrorCategory(plugin, "Postgres Integrity Constraint Violation")); |
| 62 | + ERROR_CODE_TO_ERROR_CATEGORY.put("28", new ErrorCategory(plugin, "Postgres Invalid Authorization Specification")); |
| 63 | + ERROR_CODE_TO_ERROR_CATEGORY.put("40", new ErrorCategory(plugin, "Transaction Rollback")); |
| 64 | + ERROR_CODE_TO_ERROR_CATEGORY.put("42", new ErrorCategory(plugin, "Syntax Error or Access Rule Violation")); |
| 65 | + ERROR_CODE_TO_ERROR_CATEGORY.put("53", new ErrorCategory(plugin, "Postgres Server Insufficient Resources")); |
| 66 | + ERROR_CODE_TO_ERROR_CATEGORY.put("54", new ErrorCategory(plugin, "Postgres Program Limit Exceeded")); |
| 67 | + ERROR_CODE_TO_ERROR_CATEGORY.put("55", new ErrorCategory(plugin, "Object Not in Prerequisite State")); |
| 68 | + ERROR_CODE_TO_ERROR_CATEGORY.put("57", new ErrorCategory(plugin, "Operator Intervention")); |
| 69 | + ERROR_CODE_TO_ERROR_CATEGORY.put("58", new ErrorCategory(plugin, "Postgres Server System Error")); |
| 70 | + ERROR_CODE_TO_ERROR_CATEGORY.put("P0", new ErrorCategory(plugin, "PL/pgSQL Error")); |
| 71 | + ERROR_CODE_TO_ERROR_CATEGORY.put("XX", new ErrorCategory(plugin, "Postgres Server Internal Error")); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + protected String getExternalDocumentationLink() { |
| 76 | + return DBUtils.POSTGRES_SUPPORTED_DOC_URL; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + protected ErrorType getErrorTypeFromErrorCode(int errorCode, String sqlState) { |
| 81 | + if (!Strings.isNullOrEmpty(sqlState) && sqlState.length() >= 2 && |
| 82 | + ERROR_CODE_TO_ERROR_TYPE.containsKey(sqlState.substring(0, 2))) { |
| 83 | + return ERROR_CODE_TO_ERROR_TYPE.get(sqlState.substring(0, 2)); |
| 84 | + } |
| 85 | + return ErrorType.UNKNOWN; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + protected ErrorCategory getErrorCategoryFromSqlState(String sqlState) { |
| 90 | + if (!Strings.isNullOrEmpty(sqlState) && sqlState.length() >= 2 && |
| 91 | + ERROR_CODE_TO_ERROR_CATEGORY.containsKey(sqlState.substring(0, 2))) { |
| 92 | + return ERROR_CODE_TO_ERROR_CATEGORY.get(sqlState.substring(0, 2)); |
| 93 | + } |
| 94 | + return new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN); |
| 95 | + } |
| 96 | +} |
0 commit comments