|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package com.dtstack.flink.sql.enums; |
| 20 | + |
| 21 | +import org.apache.commons.lang.StringUtils; |
| 22 | +import org.apache.commons.lang.exception.ExceptionUtils; |
| 23 | + |
| 24 | +import java.util.Arrays; |
| 25 | + |
| 26 | +/** |
| 27 | + * Date: 2020/4/2 |
| 28 | + * Company: www.dtstack.com |
| 29 | + * @author maqi |
| 30 | + */ |
| 31 | +public enum EConnectionErrorCode { |
| 32 | + ERROR_NOT_MATCH(0, "错误信息未匹配", new String[]{}), |
| 33 | + CONN_DB_INVALID(1, "数据库连接失效,请重新打开", new String[]{"the last packet successfully received from the server was", "Zookeeper session has been expired"}), |
| 34 | + CONN_DB_FAILED(2, "数据库连接失败,请检查用户名或密码是否正确", new String[]{"Access denied for user"}), |
| 35 | + DB_TABLE_NOT_EXIST(3, "操作的表名不存在", new String[]{"doesn't exist"}); |
| 36 | + |
| 37 | + private int code; |
| 38 | + private String description; |
| 39 | + private String[] baseErrorInfo; |
| 40 | + |
| 41 | + EConnectionErrorCode(int code, String description, String[] baseErrorInfo) { |
| 42 | + this.code = code; |
| 43 | + this.description = description; |
| 44 | + this.baseErrorInfo = baseErrorInfo; |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + public static EConnectionErrorCode resolveErrorCodeFromException(Throwable e) { |
| 49 | + final String stackErrorMsg = ExceptionUtils.getFullStackTrace(e); |
| 50 | + return Arrays.stream(values()) |
| 51 | + .filter(errorCode -> matchKnowError(errorCode, stackErrorMsg)) |
| 52 | + .findAny() |
| 53 | + .orElse(ERROR_NOT_MATCH); |
| 54 | + } |
| 55 | + |
| 56 | + public static boolean matchKnowError(EConnectionErrorCode errorCode, String errorMsg) { |
| 57 | + return Arrays.stream(errorCode.baseErrorInfo) |
| 58 | + .filter(baseInfo -> StringUtils.containsIgnoreCase(errorMsg, baseInfo)) |
| 59 | + .findAny() |
| 60 | + .isPresent(); |
| 61 | + } |
| 62 | +} |
0 commit comments