Skip to content

Commit 9d1b258

Browse files
authored
[Improve][Common] Introduce new error define rule (#5793)
1 parent a08113b commit 9d1b258

220 files changed

Lines changed: 1463 additions & 1065 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

seatunnel-api/src/main/java/org/apache/seatunnel/api/table/catalog/DataTypeConvertException.java

Lines changed: 0 additions & 40 deletions
This file was deleted.

seatunnel-api/src/main/java/org/apache/seatunnel/api/table/catalog/DataTypeConvertor.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ public interface DataTypeConvertor<T> {
4444
*/
4545
// todo: If the origin data type contains the properties, we can remove the dataTypeProperties.
4646
SeaTunnelDataType<?> toSeaTunnelType(
47-
String field, T connectorDataType, Map<String, Object> dataTypeProperties)
48-
throws DataTypeConvertException;
47+
String field, T connectorDataType, Map<String, Object> dataTypeProperties);
4948

5049
/**
5150
* Transfer the data type from SeaTunnel to connector.
@@ -60,8 +59,7 @@ SeaTunnelDataType<?> toSeaTunnelType(
6059
T toConnectorType(
6160
String field,
6261
SeaTunnelDataType<?> seaTunnelDataType,
63-
Map<String, Object> dataTypeProperties)
64-
throws DataTypeConvertException;
62+
Map<String, Object> dataTypeProperties);
6563

6664
String getIdentity();
6765
}

seatunnel-api/src/main/java/org/apache/seatunnel/api/table/catalog/SeaTunnelDataTypeConvertorUtil.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
2929
import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
3030
import org.apache.seatunnel.api.table.type.SqlType;
31+
import org.apache.seatunnel.common.exception.CommonError;
3132
import org.apache.seatunnel.common.utils.JsonUtils;
3233

3334
import java.util.Map;
@@ -38,15 +39,16 @@ public class SeaTunnelDataTypeConvertorUtil {
3839
* @param columnType column type, should be {@link SeaTunnelDataType##toString}.
3940
* @return {@link SeaTunnelDataType} instance.
4041
*/
41-
public static SeaTunnelDataType<?> deserializeSeaTunnelDataType(String columnType) {
42+
public static SeaTunnelDataType<?> deserializeSeaTunnelDataType(
43+
String field, String columnType) {
4244
SqlType sqlType = null;
4345
try {
4446
sqlType = SqlType.valueOf(columnType.toUpperCase().replace(" ", ""));
4547
} catch (IllegalArgumentException e) {
4648
// nothing
4749
}
4850
if (sqlType == null) {
49-
return parseComplexDataType(columnType);
51+
return parseComplexDataType(field, columnType);
5052
}
5153
switch (sqlType) {
5254
case STRING:
@@ -76,29 +78,27 @@ public static SeaTunnelDataType<?> deserializeSeaTunnelDataType(String columnTyp
7678
case TIMESTAMP:
7779
return LocalTimeType.LOCAL_DATE_TIME_TYPE;
7880
case MAP:
79-
return parseMapType(columnType);
81+
return parseMapType(field, columnType);
8082
default:
81-
throw new UnsupportedOperationException(
82-
String.format("the type[%s] is not support", columnType));
83+
throw CommonError.unsupportedDataType("SeaTunnel", columnType, field);
8384
}
8485
}
8586

86-
private static SeaTunnelDataType<?> parseComplexDataType(String columnStr) {
87+
private static SeaTunnelDataType<?> parseComplexDataType(String field, String columnStr) {
8788
String column = columnStr.toUpperCase().replace(" ", "");
8889
if (column.startsWith(SqlType.MAP.name())) {
89-
return parseMapType(column);
90+
return parseMapType(field, column);
9091
}
9192
if (column.startsWith(SqlType.ARRAY.name())) {
92-
return parseArrayType(column);
93+
return parseArrayType(field, column);
9394
}
9495
if (column.startsWith(SqlType.DECIMAL.name())) {
9596
return parseDecimalType(column);
9697
}
9798
if (column.trim().startsWith("{")) {
9899
return parseRowType(columnStr);
99100
}
100-
throw new UnsupportedOperationException(
101-
String.format("the type[%s] is not support", columnStr));
101+
throw CommonError.unsupportedDataType("SeaTunnel", columnStr, field);
102102
}
103103

104104
private static SeaTunnelDataType<?> parseRowType(String columnStr) {
@@ -109,13 +109,13 @@ private static SeaTunnelDataType<?> parseRowType(String columnStr) {
109109
int i = 0;
110110
for (Map.Entry<String, String> entry : fieldsMap.entrySet()) {
111111
fieldsName[i] = entry.getKey();
112-
seaTunnelDataTypes[i] = deserializeSeaTunnelDataType(entry.getValue());
112+
seaTunnelDataTypes[i] = deserializeSeaTunnelDataType(entry.getKey(), entry.getValue());
113113
i++;
114114
}
115115
return new SeaTunnelRowType(fieldsName, seaTunnelDataTypes);
116116
}
117117

118-
private static SeaTunnelDataType<?> parseMapType(String columnStr) {
118+
private static SeaTunnelDataType<?> parseMapType(String field, String columnStr) {
119119
String genericType = getGenericType(columnStr);
120120
int index =
121121
genericType.startsWith(SqlType.DECIMAL.name())
@@ -128,18 +128,18 @@ private static SeaTunnelDataType<?> parseMapType(String columnStr) {
128128
String keyGenericType = genericType.substring(0, index);
129129
String valueGenericType = genericType.substring(index + 1);
130130
return new MapType<>(
131-
deserializeSeaTunnelDataType(keyGenericType),
132-
deserializeSeaTunnelDataType(valueGenericType));
131+
deserializeSeaTunnelDataType(field, keyGenericType),
132+
deserializeSeaTunnelDataType(field, valueGenericType));
133133
}
134134

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

140-
private static SeaTunnelDataType<?> parseArrayType(String columnStr) {
140+
private static SeaTunnelDataType<?> parseArrayType(String field, String columnStr) {
141141
String genericType = getGenericType(columnStr);
142-
SeaTunnelDataType<?> dataType = deserializeSeaTunnelDataType(genericType);
142+
SeaTunnelDataType<?> dataType = deserializeSeaTunnelDataType(field, genericType);
143143
switch (dataType.getSqlType()) {
144144
case STRING:
145145
return ArrayType.STRING_ARRAY_TYPE;
@@ -158,9 +158,7 @@ private static SeaTunnelDataType<?> parseArrayType(String columnStr) {
158158
case DOUBLE:
159159
return ArrayType.DOUBLE_ARRAY_TYPE;
160160
default:
161-
String errorMsg =
162-
String.format("Array type not support this genericType [%s]", genericType);
163-
throw new UnsupportedOperationException(errorMsg);
161+
throw CommonError.unsupportedDataType("SeaTunnel", columnStr, field);
164162
}
165163
}
166164

seatunnel-api/src/main/java/org/apache/seatunnel/api/table/catalog/schema/ReadonlyConfigParser.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public List<Column> parse(ReadonlyConfig schemaConfig) {
9494
String key = entry.getKey();
9595
String value = entry.getValue();
9696
SeaTunnelDataType<?> dataType =
97-
SeaTunnelDataTypeConvertorUtil.deserializeSeaTunnelDataType(value);
97+
SeaTunnelDataTypeConvertorUtil.deserializeSeaTunnelDataType(key, value);
9898
PhysicalColumn column = PhysicalColumn.of(key, dataType, 0, true, null, null);
9999
columns.add(column);
100100
}
@@ -121,8 +121,10 @@ public List<Column> parse(ReadonlyConfig schemaConfig) {
121121
columnConfig
122122
.getOptional(TableSchemaOptions.ColumnOptions.TYPE)
123123
.map(
124-
SeaTunnelDataTypeConvertorUtil
125-
::deserializeSeaTunnelDataType)
124+
column ->
125+
SeaTunnelDataTypeConvertorUtil
126+
.deserializeSeaTunnelDataType(
127+
name, column))
126128
.orElseThrow(
127129
() ->
128130
new IllegalArgumentException(

seatunnel-api/src/test/java/org/apache/seatunnel/api/table/catalog/SeaTunnelDataTypeConvertorUtilTest.java

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,62 @@
1717

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

20+
import org.apache.seatunnel.common.exception.SeaTunnelRuntimeException;
21+
2022
import org.junit.jupiter.api.Assertions;
2123
import org.junit.jupiter.api.Test;
2224

2325
public class SeaTunnelDataTypeConvertorUtilTest {
2426

2527
@Test
2628
void testParseWithUnsupportedType() {
29+
SeaTunnelRuntimeException exception =
30+
Assertions.assertThrows(
31+
SeaTunnelRuntimeException.class,
32+
() ->
33+
SeaTunnelDataTypeConvertorUtil.deserializeSeaTunnelDataType(
34+
"test", "MULTIPLE_ROW"));
35+
Assertions.assertEquals(
36+
"ErrorCode:[COMMON-07], ErrorDescription:['SeaTunnel' unsupported data type 'MULTIPLE_ROW' of 'test']",
37+
exception.getMessage());
38+
39+
SeaTunnelRuntimeException exception2 =
40+
Assertions.assertThrows(
41+
SeaTunnelRuntimeException.class,
42+
() ->
43+
SeaTunnelDataTypeConvertorUtil.deserializeSeaTunnelDataType(
44+
"test", "map<string, MULTIPLE_ROW>"));
45+
Assertions.assertEquals(
46+
"ErrorCode:[COMMON-07], ErrorDescription:['SeaTunnel' unsupported data type 'MULTIPLE_ROW' of 'test']",
47+
exception2.getMessage());
2748

28-
UnsupportedOperationException exception =
49+
SeaTunnelRuntimeException exception3 =
2950
Assertions.assertThrows(
30-
UnsupportedOperationException.class,
31-
() -> SeaTunnelDataTypeConvertorUtil.deserializeSeaTunnelDataType("uuid"));
32-
Assertions.assertEquals("the type[uuid] is not support", exception.getMessage());
51+
SeaTunnelRuntimeException.class,
52+
() ->
53+
SeaTunnelDataTypeConvertorUtil.deserializeSeaTunnelDataType(
54+
"test", "array<MULTIPLE_ROW>"));
55+
Assertions.assertEquals(
56+
"ErrorCode:[COMMON-07], ErrorDescription:['SeaTunnel' unsupported data type 'MULTIPLE_ROW' of 'test']",
57+
exception3.getMessage());
58+
59+
SeaTunnelRuntimeException exception4 =
60+
Assertions.assertThrows(
61+
SeaTunnelRuntimeException.class,
62+
() ->
63+
SeaTunnelDataTypeConvertorUtil.deserializeSeaTunnelDataType(
64+
"test", "uuid"));
65+
Assertions.assertEquals(
66+
"ErrorCode:[COMMON-07], ErrorDescription:['SeaTunnel' unsupported data type 'uuid' of 'test']",
67+
exception4.getMessage());
3368

34-
RuntimeException exception2 =
69+
RuntimeException exception5 =
3570
Assertions.assertThrows(
3671
RuntimeException.class,
3772
() ->
3873
SeaTunnelDataTypeConvertorUtil.deserializeSeaTunnelDataType(
39-
"{uuid}"));
74+
"test", "{uuid}"));
4075
Assertions.assertEquals(
41-
"String json deserialization exception.{uuid}", exception2.getMessage());
76+
"String json deserialization exception.{uuid}", exception5.getMessage());
4277
}
4378
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.seatunnel.common.exception;
19+
20+
import org.apache.seatunnel.common.constants.PluginType;
21+
22+
import java.util.HashMap;
23+
import java.util.Map;
24+
25+
import static org.apache.seatunnel.common.exception.CommonErrorCode.CONVERT_TO_CONNECTOR_TYPE_ERROR;
26+
import static org.apache.seatunnel.common.exception.CommonErrorCode.CONVERT_TO_CONNECTOR_TYPE_ERROR_SIMPLE;
27+
import static org.apache.seatunnel.common.exception.CommonErrorCode.CONVERT_TO_SEATUNNEL_TYPE_ERROR;
28+
import static org.apache.seatunnel.common.exception.CommonErrorCode.CONVERT_TO_SEATUNNEL_TYPE_ERROR_SIMPLE;
29+
import static org.apache.seatunnel.common.exception.CommonErrorCode.UNSUPPORTED_DATA_TYPE;
30+
31+
/**
32+
* The common error of SeaTunnel. This is an alternative to {@link CommonErrorCodeDeprecated} and is
33+
* used to define non-bug errors or expected errors for all connectors and engines. We need to
34+
* define a corresponding enumeration type in {@link CommonErrorCode} to determine the output error
35+
* message format and content. Then define the corresponding method in {@link CommonError} to
36+
* construct the corresponding error instance.
37+
*/
38+
public class CommonError {
39+
40+
public static SeaTunnelRuntimeException unsupportedDataType(
41+
String identifier, String dataType, String field) {
42+
Map<String, String> params = new HashMap<>();
43+
params.put("identifier", identifier);
44+
params.put("dataType", dataType);
45+
params.put("field", field);
46+
return new SeaTunnelRuntimeException(UNSUPPORTED_DATA_TYPE, params);
47+
}
48+
49+
public static SeaTunnelRuntimeException convertToSeaTunnelTypeError(
50+
String connector, PluginType pluginType, String dataType, String field) {
51+
Map<String, String> params = new HashMap<>();
52+
params.put("connector", connector);
53+
params.put("type", pluginType.getType());
54+
params.put("dataType", dataType);
55+
params.put("field", field);
56+
return new SeaTunnelRuntimeException(CONVERT_TO_SEATUNNEL_TYPE_ERROR, params);
57+
}
58+
59+
public static SeaTunnelRuntimeException convertToSeaTunnelTypeError(
60+
String identifier, String dataType, String field) {
61+
Map<String, String> params = new HashMap<>();
62+
params.put("identifier", identifier);
63+
params.put("dataType", dataType);
64+
params.put("field", field);
65+
return new SeaTunnelRuntimeException(CONVERT_TO_SEATUNNEL_TYPE_ERROR_SIMPLE, params);
66+
}
67+
68+
public static SeaTunnelRuntimeException convertToConnectorTypeError(
69+
String connector, PluginType pluginType, String dataType, String field) {
70+
Map<String, String> params = new HashMap<>();
71+
params.put("connector", connector);
72+
params.put("type", pluginType.getType());
73+
params.put("dataType", dataType);
74+
params.put("field", field);
75+
return new SeaTunnelRuntimeException(CONVERT_TO_CONNECTOR_TYPE_ERROR, params);
76+
}
77+
78+
public static SeaTunnelRuntimeException convertToConnectorTypeError(
79+
String identifier, String dataType, String field) {
80+
Map<String, String> params = new HashMap<>();
81+
params.put("identifier", identifier);
82+
params.put("dataType", dataType);
83+
params.put("field", field);
84+
return new SeaTunnelRuntimeException(CONVERT_TO_CONNECTOR_TYPE_ERROR_SIMPLE, params);
85+
}
86+
}

seatunnel-common/src/main/java/org/apache/seatunnel/common/exception/CommonErrorCode.java

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,22 @@
1717

1818
package org.apache.seatunnel.common.exception;
1919

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

4237
private final String code;
4338
private final String description;

0 commit comments

Comments
 (0)