Skip to content

Commit

Permalink
[Improve][Common] Introduce new error define rule (apache#5793)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hisoka-X authored Nov 8, 2023
1 parent a08113b commit 9d1b258
Show file tree
Hide file tree
Showing 220 changed files with 1,463 additions and 1,065 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ public interface DataTypeConvertor<T> {
*/
// todo: If the origin data type contains the properties, we can remove the dataTypeProperties.
SeaTunnelDataType<?> toSeaTunnelType(
String field, T connectorDataType, Map<String, Object> dataTypeProperties)
throws DataTypeConvertException;
String field, T connectorDataType, Map<String, Object> dataTypeProperties);

/**
* Transfer the data type from SeaTunnel to connector.
Expand All @@ -60,8 +59,7 @@ SeaTunnelDataType<?> toSeaTunnelType(
T toConnectorType(
String field,
SeaTunnelDataType<?> seaTunnelDataType,
Map<String, Object> dataTypeProperties)
throws DataTypeConvertException;
Map<String, Object> dataTypeProperties);

String getIdentity();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
import org.apache.seatunnel.api.table.type.SqlType;
import org.apache.seatunnel.common.exception.CommonError;
import org.apache.seatunnel.common.utils.JsonUtils;

import java.util.Map;
Expand All @@ -38,15 +39,16 @@ public class SeaTunnelDataTypeConvertorUtil {
* @param columnType column type, should be {@link SeaTunnelDataType##toString}.
* @return {@link SeaTunnelDataType} instance.
*/
public static SeaTunnelDataType<?> deserializeSeaTunnelDataType(String columnType) {
public static SeaTunnelDataType<?> deserializeSeaTunnelDataType(
String field, String columnType) {
SqlType sqlType = null;
try {
sqlType = SqlType.valueOf(columnType.toUpperCase().replace(" ", ""));
} catch (IllegalArgumentException e) {
// nothing
}
if (sqlType == null) {
return parseComplexDataType(columnType);
return parseComplexDataType(field, columnType);
}
switch (sqlType) {
case STRING:
Expand Down Expand Up @@ -76,29 +78,27 @@ public static SeaTunnelDataType<?> deserializeSeaTunnelDataType(String columnTyp
case TIMESTAMP:
return LocalTimeType.LOCAL_DATE_TIME_TYPE;
case MAP:
return parseMapType(columnType);
return parseMapType(field, columnType);
default:
throw new UnsupportedOperationException(
String.format("the type[%s] is not support", columnType));
throw CommonError.unsupportedDataType("SeaTunnel", columnType, field);
}
}

private static SeaTunnelDataType<?> parseComplexDataType(String columnStr) {
private static SeaTunnelDataType<?> parseComplexDataType(String field, String columnStr) {
String column = columnStr.toUpperCase().replace(" ", "");
if (column.startsWith(SqlType.MAP.name())) {
return parseMapType(column);
return parseMapType(field, column);
}
if (column.startsWith(SqlType.ARRAY.name())) {
return parseArrayType(column);
return parseArrayType(field, column);
}
if (column.startsWith(SqlType.DECIMAL.name())) {
return parseDecimalType(column);
}
if (column.trim().startsWith("{")) {
return parseRowType(columnStr);
}
throw new UnsupportedOperationException(
String.format("the type[%s] is not support", columnStr));
throw CommonError.unsupportedDataType("SeaTunnel", columnStr, field);
}

private static SeaTunnelDataType<?> parseRowType(String columnStr) {
Expand All @@ -109,13 +109,13 @@ private static SeaTunnelDataType<?> parseRowType(String columnStr) {
int i = 0;
for (Map.Entry<String, String> entry : fieldsMap.entrySet()) {
fieldsName[i] = entry.getKey();
seaTunnelDataTypes[i] = deserializeSeaTunnelDataType(entry.getValue());
seaTunnelDataTypes[i] = deserializeSeaTunnelDataType(entry.getKey(), entry.getValue());
i++;
}
return new SeaTunnelRowType(fieldsName, seaTunnelDataTypes);
}

private static SeaTunnelDataType<?> parseMapType(String columnStr) {
private static SeaTunnelDataType<?> parseMapType(String field, String columnStr) {
String genericType = getGenericType(columnStr);
int index =
genericType.startsWith(SqlType.DECIMAL.name())
Expand All @@ -128,18 +128,18 @@ private static SeaTunnelDataType<?> parseMapType(String columnStr) {
String keyGenericType = genericType.substring(0, index);
String valueGenericType = genericType.substring(index + 1);
return new MapType<>(
deserializeSeaTunnelDataType(keyGenericType),
deserializeSeaTunnelDataType(valueGenericType));
deserializeSeaTunnelDataType(field, keyGenericType),
deserializeSeaTunnelDataType(field, valueGenericType));
}

private static String getGenericType(String columnStr) {
// get the content between '<' and '>'
return columnStr.substring(columnStr.indexOf("<") + 1, columnStr.lastIndexOf(">"));
}

private static SeaTunnelDataType<?> parseArrayType(String columnStr) {
private static SeaTunnelDataType<?> parseArrayType(String field, String columnStr) {
String genericType = getGenericType(columnStr);
SeaTunnelDataType<?> dataType = deserializeSeaTunnelDataType(genericType);
SeaTunnelDataType<?> dataType = deserializeSeaTunnelDataType(field, genericType);
switch (dataType.getSqlType()) {
case STRING:
return ArrayType.STRING_ARRAY_TYPE;
Expand All @@ -158,9 +158,7 @@ private static SeaTunnelDataType<?> parseArrayType(String columnStr) {
case DOUBLE:
return ArrayType.DOUBLE_ARRAY_TYPE;
default:
String errorMsg =
String.format("Array type not support this genericType [%s]", genericType);
throw new UnsupportedOperationException(errorMsg);
throw CommonError.unsupportedDataType("SeaTunnel", columnStr, field);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public List<Column> parse(ReadonlyConfig schemaConfig) {
String key = entry.getKey();
String value = entry.getValue();
SeaTunnelDataType<?> dataType =
SeaTunnelDataTypeConvertorUtil.deserializeSeaTunnelDataType(value);
SeaTunnelDataTypeConvertorUtil.deserializeSeaTunnelDataType(key, value);
PhysicalColumn column = PhysicalColumn.of(key, dataType, 0, true, null, null);
columns.add(column);
}
Expand All @@ -121,8 +121,10 @@ public List<Column> parse(ReadonlyConfig schemaConfig) {
columnConfig
.getOptional(TableSchemaOptions.ColumnOptions.TYPE)
.map(
SeaTunnelDataTypeConvertorUtil
::deserializeSeaTunnelDataType)
column ->
SeaTunnelDataTypeConvertorUtil
.deserializeSeaTunnelDataType(
name, column))
.orElseThrow(
() ->
new IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,62 @@

package org.apache.seatunnel.api.table.catalog;

import org.apache.seatunnel.common.exception.SeaTunnelRuntimeException;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class SeaTunnelDataTypeConvertorUtilTest {

@Test
void testParseWithUnsupportedType() {
SeaTunnelRuntimeException exception =
Assertions.assertThrows(
SeaTunnelRuntimeException.class,
() ->
SeaTunnelDataTypeConvertorUtil.deserializeSeaTunnelDataType(
"test", "MULTIPLE_ROW"));
Assertions.assertEquals(
"ErrorCode:[COMMON-07], ErrorDescription:['SeaTunnel' unsupported data type 'MULTIPLE_ROW' of 'test']",
exception.getMessage());

SeaTunnelRuntimeException exception2 =
Assertions.assertThrows(
SeaTunnelRuntimeException.class,
() ->
SeaTunnelDataTypeConvertorUtil.deserializeSeaTunnelDataType(
"test", "map<string, MULTIPLE_ROW>"));
Assertions.assertEquals(
"ErrorCode:[COMMON-07], ErrorDescription:['SeaTunnel' unsupported data type 'MULTIPLE_ROW' of 'test']",
exception2.getMessage());

UnsupportedOperationException exception =
SeaTunnelRuntimeException exception3 =
Assertions.assertThrows(
UnsupportedOperationException.class,
() -> SeaTunnelDataTypeConvertorUtil.deserializeSeaTunnelDataType("uuid"));
Assertions.assertEquals("the type[uuid] is not support", exception.getMessage());
SeaTunnelRuntimeException.class,
() ->
SeaTunnelDataTypeConvertorUtil.deserializeSeaTunnelDataType(
"test", "array<MULTIPLE_ROW>"));
Assertions.assertEquals(
"ErrorCode:[COMMON-07], ErrorDescription:['SeaTunnel' unsupported data type 'MULTIPLE_ROW' of 'test']",
exception3.getMessage());

SeaTunnelRuntimeException exception4 =
Assertions.assertThrows(
SeaTunnelRuntimeException.class,
() ->
SeaTunnelDataTypeConvertorUtil.deserializeSeaTunnelDataType(
"test", "uuid"));
Assertions.assertEquals(
"ErrorCode:[COMMON-07], ErrorDescription:['SeaTunnel' unsupported data type 'uuid' of 'test']",
exception4.getMessage());

RuntimeException exception2 =
RuntimeException exception5 =
Assertions.assertThrows(
RuntimeException.class,
() ->
SeaTunnelDataTypeConvertorUtil.deserializeSeaTunnelDataType(
"{uuid}"));
"test", "{uuid}"));
Assertions.assertEquals(
"String json deserialization exception.{uuid}", exception2.getMessage());
"String json deserialization exception.{uuid}", exception5.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.seatunnel.common.exception;

import org.apache.seatunnel.common.constants.PluginType;

import java.util.HashMap;
import java.util.Map;

import static org.apache.seatunnel.common.exception.CommonErrorCode.CONVERT_TO_CONNECTOR_TYPE_ERROR;
import static org.apache.seatunnel.common.exception.CommonErrorCode.CONVERT_TO_CONNECTOR_TYPE_ERROR_SIMPLE;
import static org.apache.seatunnel.common.exception.CommonErrorCode.CONVERT_TO_SEATUNNEL_TYPE_ERROR;
import static org.apache.seatunnel.common.exception.CommonErrorCode.CONVERT_TO_SEATUNNEL_TYPE_ERROR_SIMPLE;
import static org.apache.seatunnel.common.exception.CommonErrorCode.UNSUPPORTED_DATA_TYPE;

/**
* The common error of SeaTunnel. This is an alternative to {@link CommonErrorCodeDeprecated} and is
* used to define non-bug errors or expected errors for all connectors and engines. We need to
* define a corresponding enumeration type in {@link CommonErrorCode} to determine the output error
* message format and content. Then define the corresponding method in {@link CommonError} to
* construct the corresponding error instance.
*/
public class CommonError {

public static SeaTunnelRuntimeException unsupportedDataType(
String identifier, String dataType, String field) {
Map<String, String> params = new HashMap<>();
params.put("identifier", identifier);
params.put("dataType", dataType);
params.put("field", field);
return new SeaTunnelRuntimeException(UNSUPPORTED_DATA_TYPE, params);
}

public static SeaTunnelRuntimeException convertToSeaTunnelTypeError(
String connector, PluginType pluginType, String dataType, String field) {
Map<String, String> params = new HashMap<>();
params.put("connector", connector);
params.put("type", pluginType.getType());
params.put("dataType", dataType);
params.put("field", field);
return new SeaTunnelRuntimeException(CONVERT_TO_SEATUNNEL_TYPE_ERROR, params);
}

public static SeaTunnelRuntimeException convertToSeaTunnelTypeError(
String identifier, String dataType, String field) {
Map<String, String> params = new HashMap<>();
params.put("identifier", identifier);
params.put("dataType", dataType);
params.put("field", field);
return new SeaTunnelRuntimeException(CONVERT_TO_SEATUNNEL_TYPE_ERROR_SIMPLE, params);
}

public static SeaTunnelRuntimeException convertToConnectorTypeError(
String connector, PluginType pluginType, String dataType, String field) {
Map<String, String> params = new HashMap<>();
params.put("connector", connector);
params.put("type", pluginType.getType());
params.put("dataType", dataType);
params.put("field", field);
return new SeaTunnelRuntimeException(CONVERT_TO_CONNECTOR_TYPE_ERROR, params);
}

public static SeaTunnelRuntimeException convertToConnectorTypeError(
String identifier, String dataType, String field) {
Map<String, String> params = new HashMap<>();
params.put("identifier", identifier);
params.put("dataType", dataType);
params.put("field", field);
return new SeaTunnelRuntimeException(CONVERT_TO_CONNECTOR_TYPE_ERROR_SIMPLE, params);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,22 @@

package org.apache.seatunnel.common.exception;

public enum CommonErrorCode implements SeaTunnelErrorCode {
FILE_OPERATION_FAILED(
"COMMON-01", "File operation failed, such as (read,list,write,move,copy,sync) etc..."),
JSON_OPERATION_FAILED("COMMON-02", "Json covert/parse operation failed"),
REFLECT_CLASS_OPERATION_FAILED("COMMON-03", "Reflect class operation failed"),
SERIALIZE_OPERATION_FAILED("COMMON-04", "Serialize class operation failed"),
UNSUPPORTED_OPERATION("COMMON-05", "Unsupported operation"),
ILLEGAL_ARGUMENT("COMMON-06", "Illegal argument"),
UNSUPPORTED_DATA_TYPE("COMMON-07", "Unsupported data type"),
SQL_OPERATION_FAILED(
"COMMON-08", "Sql operation failed, such as (execute,addBatch,close) etc..."),
TABLE_SCHEMA_GET_FAILED("COMMON-09", "Get table schema from upstream data failed"),
FLUSH_DATA_FAILED("COMMON-10", "Flush data operation that in sink connector failed"),
WRITER_OPERATION_FAILED(
"COMMON-11", "Sink writer operation failed, such as (open, close) etc..."),
READER_OPERATION_FAILED(
"COMMON-12", "Source reader operation failed, such as (open, close) etc..."),
HTTP_OPERATION_FAILED(
"COMMON-13", "Http operation failed, such as (open, close, response) etc..."),
KERBEROS_AUTHORIZED_FAILED("COMMON-14", "Kerberos authorized failed"),
CLASS_NOT_FOUND("COMMON-15", "Class load operation failed");
/** SeaTunnel connector error code interface, it only should be invoked by {@link CommonError} */
enum CommonErrorCode implements SeaTunnelErrorCode {
UNSUPPORTED_DATA_TYPE(
"COMMON-07", "'<identifier>' unsupported data type '<dataType>' of '<field>'"),
CONVERT_TO_SEATUNNEL_TYPE_ERROR(
"COMMON-16",
"'<connector>' <type> unsupported convert type '<dataType>' of '<field>' to SeaTunnel data type."),
CONVERT_TO_SEATUNNEL_TYPE_ERROR_SIMPLE(
"COMMON-17",
"'<identifier>' unsupported convert type '<dataType>' of '<field>' to SeaTunnel data type."),
CONVERT_TO_CONNECTOR_TYPE_ERROR(
"COMMON-18",
"'<connector>' <type> unsupported convert SeaTunnel data type '<dataType>' of '<field>' to connector data type."),
CONVERT_TO_CONNECTOR_TYPE_ERROR_SIMPLE(
"COMMON-19",
"'<identifier>' unsupported convert SeaTunnel data type '<dataType>' of '<field>' to connector data type.");

private final String code;
private final String description;
Expand Down
Loading

0 comments on commit 9d1b258

Please sign in to comment.