Skip to content

Commit b3dc0bb

Browse files
authored
[Improve][Common] Adapt FILE_OPERATION_FAILED to CommonError (#5928)
1 parent 43787a3 commit b3dc0bb

File tree

27 files changed

+353
-127
lines changed

27 files changed

+353
-127
lines changed

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@
2929
import static org.apache.seatunnel.common.exception.CommonErrorCode.CONVERT_TO_CONNECTOR_TYPE_ERROR_SIMPLE;
3030
import static org.apache.seatunnel.common.exception.CommonErrorCode.CONVERT_TO_SEATUNNEL_TYPE_ERROR;
3131
import static org.apache.seatunnel.common.exception.CommonErrorCode.CONVERT_TO_SEATUNNEL_TYPE_ERROR_SIMPLE;
32+
import static org.apache.seatunnel.common.exception.CommonErrorCode.FILE_NOT_EXISTED;
33+
import static org.apache.seatunnel.common.exception.CommonErrorCode.FILE_OPERATION_FAILED;
3234
import static org.apache.seatunnel.common.exception.CommonErrorCode.GET_CATALOG_TABLES_WITH_UNSUPPORTED_TYPE_ERROR;
3335
import static org.apache.seatunnel.common.exception.CommonErrorCode.GET_CATALOG_TABLE_WITH_UNSUPPORTED_TYPE_ERROR;
3436
import static org.apache.seatunnel.common.exception.CommonErrorCode.JSON_OPERATION_FAILED;
3537
import static org.apache.seatunnel.common.exception.CommonErrorCode.UNSUPPORTED_DATA_TYPE;
38+
import static org.apache.seatunnel.common.exception.CommonErrorCode.WRITE_SEATUNNEL_ROW_ERROR;
3639

3740
/**
3841
* The common error of SeaTunnel. This is an alternative to {@link CommonErrorCodeDeprecated} and is
@@ -45,6 +48,41 @@ public class CommonError {
4548

4649
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
4750

51+
public static SeaTunnelRuntimeException fileOperationFailed(
52+
String identifier, String operation, String fileName, Throwable cause) {
53+
Map<String, String> params = new HashMap<>();
54+
params.put("identifier", identifier);
55+
params.put("operation", operation);
56+
params.put("fileName", fileName);
57+
return new SeaTunnelRuntimeException(FILE_OPERATION_FAILED, params, cause);
58+
}
59+
60+
public static SeaTunnelRuntimeException fileOperationFailed(
61+
String identifier, String operation, String fileName) {
62+
Map<String, String> params = new HashMap<>();
63+
params.put("identifier", identifier);
64+
params.put("operation", operation);
65+
params.put("fileName", fileName);
66+
return new SeaTunnelRuntimeException(FILE_OPERATION_FAILED, params);
67+
}
68+
69+
public static SeaTunnelRuntimeException fileNotExistFailed(
70+
String identifier, String operation, String fileName) {
71+
Map<String, String> params = new HashMap<>();
72+
params.put("identifier", identifier);
73+
params.put("operation", operation);
74+
params.put("fileName", fileName);
75+
return new SeaTunnelRuntimeException(FILE_NOT_EXISTED, params);
76+
}
77+
78+
public static SeaTunnelRuntimeException writeSeaTunnelRowFailed(
79+
String connector, String row, Throwable cause) {
80+
Map<String, String> params = new HashMap<>();
81+
params.put("connector", connector);
82+
params.put("seaTunnelRow", row);
83+
return new SeaTunnelRuntimeException(WRITE_SEATUNNEL_ROW_ERROR, params, cause);
84+
}
85+
4886
public static SeaTunnelRuntimeException unsupportedDataType(
4987
String identifier, String dataType, String field) {
5088
Map<String, String> params = new HashMap<>();

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
/** SeaTunnel connector error code interface */
2121
public enum CommonErrorCode implements SeaTunnelErrorCode {
22+
FILE_OPERATION_FAILED("COMMON-01", "<identifier> <operation> file '<fileName>' failed."),
2223
JSON_OPERATION_FAILED(
2324
"COMMON-02", "<identifier> JSON convert/parse '<payload>' operation failed."),
24-
2525
UNSUPPORTED_DATA_TYPE(
2626
"COMMON-07", "'<identifier>' unsupported data type '<dataType>' of '<field>'"),
2727
CONVERT_TO_SEATUNNEL_TYPE_ERROR(
@@ -41,7 +41,13 @@ public enum CommonErrorCode implements SeaTunnelErrorCode {
4141
"'<catalogName>' table '<tableName>' unsupported get catalog table with field data types '<fieldWithDataTypes>'"),
4242
GET_CATALOG_TABLES_WITH_UNSUPPORTED_TYPE_ERROR(
4343
"COMMON-21",
44-
"'<catalogName>' tables unsupported get catalog table,the corresponding field types in the following tables are not supported: '<tableUnsupportedTypes>'");
44+
"'<catalogName>' tables unsupported get catalog table,the corresponding field types in the following tables are not supported: '<tableUnsupportedTypes>'"),
45+
FILE_NOT_EXISTED(
46+
"COMMON-22",
47+
"<identifier> <operation> file '<fileName>' failed, because it not existed."),
48+
WRITE_SEATUNNEL_ROW_ERROR(
49+
"COMMON-23",
50+
"<connector> write SeaTunnelRow failed, the SeaTunnelRow value is '<seaTunnelRow>'.");
4551

4652
private final String code;
4753
private final String description;

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
*/
2525
@Deprecated
2626
public enum CommonErrorCodeDeprecated implements SeaTunnelErrorCode {
27-
FILE_OPERATION_FAILED(
28-
"COMMON-01", "File operation failed, such as (read,list,write,move,copy,sync) etc..."),
2927
REFLECT_CLASS_OPERATION_FAILED("COMMON-03", "Reflect class operation failed"),
3028
SERIALIZE_OPERATION_FAILED("COMMON-04", "Serialize class operation failed"),
3129
UNSUPPORTED_OPERATION("COMMON-05", "Unsupported operation"),

seatunnel-common/src/main/java/org/apache/seatunnel/common/utils/FileUtils.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

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

20+
import org.apache.seatunnel.common.exception.CommonError;
2021
import org.apache.seatunnel.common.exception.CommonErrorCodeDeprecated;
2122
import org.apache.seatunnel.common.exception.SeaTunnelRuntimeException;
2223

@@ -65,8 +66,7 @@ public static String readFileToStr(Path path) {
6566
byte[] bytes = Files.readAllBytes(path);
6667
return new String(bytes);
6768
} catch (IOException e) {
68-
throw new SeaTunnelRuntimeException(
69-
CommonErrorCodeDeprecated.FILE_OPERATION_FAILED, ExceptionUtils.getMessage(e));
69+
throw CommonError.fileOperationFailed("SeaTunnel", "read", path.toString(), e);
7070
}
7171
}
7272

@@ -77,10 +77,7 @@ public static void writeStringToFile(String filePath, String str) {
7777
ps = new PrintStream(new FileOutputStream(file));
7878
ps.println(str);
7979
} catch (FileNotFoundException e) {
80-
throw new SeaTunnelRuntimeException(
81-
CommonErrorCodeDeprecated.FILE_OPERATION_FAILED,
82-
ExceptionUtils.getMessage(e),
83-
e);
80+
throw CommonError.fileNotExistFailed("SeaTunnel", "write", filePath);
8481
} finally {
8582
if (ps != null) {
8683
ps.close();
@@ -123,10 +120,7 @@ public static Long getFileLineNumber(@NonNull String filePath) {
123120
try (Stream<String> lines = Files.lines(Paths.get(filePath))) {
124121
return lines.count();
125122
} catch (IOException e) {
126-
throw new SeaTunnelRuntimeException(
127-
CommonErrorCodeDeprecated.FILE_OPERATION_FAILED,
128-
String.format("get file[%s] line error", filePath),
129-
e);
123+
throw CommonError.fileOperationFailed("SeaTunnel", "read", filePath, e);
130124
}
131125
}
132126

@@ -197,9 +191,7 @@ private static void deleteFiles(@NonNull File file) {
197191
file.delete();
198192

199193
} catch (Exception e) {
200-
String errorMsg = String.format("Delete file [%s] failed", file.getPath());
201-
throw new SeaTunnelRuntimeException(
202-
CommonErrorCodeDeprecated.FILE_OPERATION_FAILED, errorMsg, e);
194+
throw CommonError.fileOperationFailed("SeaTunnel", "delete", file.toString(), e);
203195
}
204196
}
205197
}

seatunnel-common/src/test/java/org/apache/seatunnel/common/utils/FileUtilsTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.apache.seatunnel.common.utils;
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

@@ -26,6 +28,7 @@
2628
import java.io.File;
2729
import java.io.FileWriter;
2830
import java.io.IOException;
31+
import java.nio.file.NoSuchFileException;
2932
import java.nio.file.Path;
3033
import java.nio.file.Paths;
3134

@@ -70,6 +73,45 @@ public void testGetFileLineNumberFromDir() throws Exception {
7073
Assertions.assertEquals(100 * 4, lines);
7174
}
7275

76+
@Test
77+
void throwExpectedException() {
78+
String root = System.getProperty("java.io.tmpdir");
79+
Path path = Paths.get(root, "not", "existed", "path");
80+
SeaTunnelRuntimeException exception =
81+
Assertions.assertThrows(
82+
SeaTunnelRuntimeException.class,
83+
() -> FileUtils.writeStringToFile(path.toString(), ""));
84+
Assertions.assertEquals(
85+
"ErrorCode:[COMMON-22], ErrorDescription:[SeaTunnel write file '"
86+
+ path
87+
+ "' failed, because it not existed.]",
88+
exception.getMessage());
89+
90+
SeaTunnelRuntimeException exception2 =
91+
Assertions.assertThrows(
92+
SeaTunnelRuntimeException.class, () -> FileUtils.readFileToStr(path));
93+
Assertions.assertEquals(
94+
"ErrorCode:[COMMON-01], ErrorDescription:[SeaTunnel read file '"
95+
+ path
96+
+ "' failed.]",
97+
exception2.getMessage());
98+
Assertions.assertInstanceOf(NoSuchFileException.class, exception2.getCause());
99+
Assertions.assertEquals(path.toString(), exception2.getCause().getMessage());
100+
101+
Path path2 = Paths.get(root, "not", "existed", "path2");
102+
SeaTunnelRuntimeException exception3 =
103+
Assertions.assertThrows(
104+
SeaTunnelRuntimeException.class,
105+
() -> FileUtils.getFileLineNumber(path2.toString()));
106+
Assertions.assertEquals(
107+
"ErrorCode:[COMMON-01], ErrorDescription:[SeaTunnel read file '"
108+
+ path2
109+
+ "' failed.]",
110+
exception3.getMessage());
111+
Assertions.assertInstanceOf(NoSuchFileException.class, exception3.getCause());
112+
Assertions.assertEquals(path2.toString(), exception3.getCause().getMessage());
113+
}
114+
73115
public void writeTestDataToFile(@NonNull String filePath) throws IOException {
74116
FileUtils.createNewFile(filePath);
75117

seatunnel-connectors-v2/connector-clickhouse/src/main/java/org/apache/seatunnel/connectors/seatunnel/clickhouse/sink/file/ClickhouseFileSinkWriter.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.apache.seatunnel.api.sink.SinkWriter;
2121
import org.apache.seatunnel.api.table.type.SeaTunnelRow;
2222
import org.apache.seatunnel.common.config.Common;
23+
import org.apache.seatunnel.common.exception.CommonError;
2324
import org.apache.seatunnel.common.exception.CommonErrorCodeDeprecated;
2425
import org.apache.seatunnel.connectors.seatunnel.clickhouse.config.FileReaderOption;
2526
import org.apache.seatunnel.connectors.seatunnel.clickhouse.exception.ClickhouseConnectorErrorCode;
@@ -119,15 +120,14 @@ public void write(SeaTunnelRow element) throws IOException {
119120
rowCache.computeIfAbsent(
120121
shard,
121122
k -> {
123+
String uuid =
124+
UUID.randomUUID()
125+
.toString()
126+
.substring(0, UUID_LENGTH)
127+
.replaceAll("-", "_");
128+
String clickhouseLocalFile =
129+
String.format("%s/%s", readerOption.getFileTempPath(), uuid);
122130
try {
123-
String uuid =
124-
UUID.randomUUID()
125-
.toString()
126-
.substring(0, UUID_LENGTH)
127-
.replaceAll("-", "_");
128-
String clickhouseLocalFile =
129-
String.format(
130-
"%s/%s", readerOption.getFileTempPath(), uuid);
131131
FileUtils.forceMkdir(new File(clickhouseLocalFile));
132132
String clickhouseLocalFileTmpFile =
133133
clickhouseLocalFile + CLICKHOUSE_LOCAL_FILE_SUFFIX;
@@ -138,10 +138,8 @@ public void write(SeaTunnelRow element) throws IOException {
138138
StandardOpenOption.READ,
139139
StandardOpenOption.CREATE_NEW);
140140
} catch (IOException e) {
141-
throw new ClickhouseConnectorException(
142-
CommonErrorCodeDeprecated.FILE_OPERATION_FAILED,
143-
"can't create new file to save tmp data",
144-
e);
141+
throw CommonError.fileOperationFailed(
142+
"ClickhouseFile", "write", clickhouseLocalFile, e);
145143
}
146144
});
147145
saveDataToFile(channel, element, shard);
@@ -240,10 +238,8 @@ private void saveDataToFile(FileChannel fileChannel, SeaTunnelRow row, Shard sha
240238
return fileChannel.map(
241239
FileChannel.MapMode.READ_WRITE, 0, bufferSize);
242240
} catch (IOException e) {
243-
throw new ClickhouseConnectorException(
244-
CommonErrorCodeDeprecated.FILE_OPERATION_FAILED,
245-
"data_local file write failed",
246-
e);
241+
throw CommonError.fileOperationFailed(
242+
"ClickhouseFile", "write", "UNKNOWN", e);
247243
}
248244
});
249245
byte[] byteData = data.getBytes(StandardCharsets.UTF_8);
@@ -312,9 +308,8 @@ private List<String> generateClickhouseLocalFiles(String clickhouseLocalFileTmpF
312308
try (FileWriter writer = new FileWriter(ckLocalConfigPath)) {
313309
writer.write(String.format(CK_LOCAL_CONFIG_TEMPLATE, clickhouseLocalFile));
314310
} catch (IOException e) {
315-
throw new ClickhouseConnectorException(
316-
CommonErrorCodeDeprecated.FILE_OPERATION_FAILED,
317-
"Error occurs when create ck local config");
311+
throw CommonError.fileOperationFailed(
312+
"ClickhouseFile", "write", clickhouseLocalFile, e);
318313
}
319314
command.add("--config-file");
320315
command.add("\"" + ckLocalConfigPath + "\"");

seatunnel-connectors-v2/connector-clickhouse/src/main/java/org/apache/seatunnel/connectors/seatunnel/clickhouse/sink/file/RsyncFileTransfer.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.apache.seatunnel.connectors.seatunnel.clickhouse.sink.file;
1919

20+
import org.apache.seatunnel.common.exception.CommonError;
2021
import org.apache.seatunnel.common.exception.CommonErrorCodeDeprecated;
2122
import org.apache.seatunnel.connectors.seatunnel.clickhouse.exception.ClickhouseConnectorErrorCode;
2223
import org.apache.seatunnel.connectors.seatunnel.clickhouse.exception.ClickhouseConnectorException;
@@ -112,10 +113,8 @@ public void transferAndChown(String sourcePath, String targetPath) {
112113
}
113114
start.waitFor();
114115
} catch (IOException | InterruptedException ex) {
115-
throw new ClickhouseConnectorException(
116-
CommonErrorCodeDeprecated.FILE_OPERATION_FAILED,
117-
"Rsync failed to transfer file: " + sourcePath + " to: " + targetPath,
118-
ex);
116+
throw CommonError.fileOperationFailed(
117+
"ClickhouseFile", "transfer", sourcePath + " -> " + targetPath, ex);
119118
}
120119
// remote exec command to change file owner. Only file owner equal with server's clickhouse
121120
// user can

seatunnel-connectors-v2/connector-clickhouse/src/main/java/org/apache/seatunnel/connectors/seatunnel/clickhouse/sink/file/ScpFileTransfer.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.apache.seatunnel.connectors.seatunnel.clickhouse.sink.file;
1919

20+
import org.apache.seatunnel.common.exception.CommonError;
2021
import org.apache.seatunnel.common.exception.CommonErrorCodeDeprecated;
2122
import org.apache.seatunnel.connectors.seatunnel.clickhouse.exception.ClickhouseConnectorErrorCode;
2223
import org.apache.seatunnel.connectors.seatunnel.clickhouse.exception.ClickhouseConnectorException;
@@ -86,10 +87,8 @@ public void transferAndChown(String sourcePath, String targetPath) {
8687
ScpClient.Option.TargetIsDirectory,
8788
ScpClient.Option.PreserveAttributes);
8889
} catch (IOException e) {
89-
throw new ClickhouseConnectorException(
90-
CommonErrorCodeDeprecated.FILE_OPERATION_FAILED,
91-
"Scp failed to transfer file: " + sourcePath + " to: " + targetPath,
92-
e);
90+
throw CommonError.fileOperationFailed(
91+
"ClickhouseFile", "transfer", sourcePath + " -> " + targetPath, e);
9392
}
9493
// remote exec command to change file owner. Only file owner equal with server's clickhouse
9594
// user can

seatunnel-connectors-v2/connector-email/src/main/java/org/apache/seatunnel/connectors/seatunnel/email/sink/EmailSinkWriter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import org.apache.seatunnel.api.table.type.SeaTunnelRow;
2323
import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
24-
import org.apache.seatunnel.common.exception.CommonErrorCodeDeprecated;
24+
import org.apache.seatunnel.common.exception.CommonError;
2525
import org.apache.seatunnel.connectors.seatunnel.common.sink.AbstractSinkWriter;
2626
import org.apache.seatunnel.connectors.seatunnel.email.config.EmailSinkConfig;
2727
import org.apache.seatunnel.connectors.seatunnel.email.exception.EmailConnectorErrorCode;
@@ -143,9 +143,10 @@ protected PasswordAuthentication getPasswordAuthentication() {
143143
}
144144

145145
public void createFile() {
146+
String fileName = "emailsink.csv";
146147
try {
147148
String data = stringBuffer.toString();
148-
File file = new File("emailsink.csv");
149+
File file = new File(fileName);
149150
// if file doesn't exist, then create it
150151
if (!file.exists()) {
151152
file.createNewFile();
@@ -155,8 +156,7 @@ public void createFile() {
155156
fileWriter.close();
156157
log.info("Create File successfully....");
157158
} catch (IOException e) {
158-
throw new EmailConnectorException(
159-
CommonErrorCodeDeprecated.FILE_OPERATION_FAILED, "Create file failed", e);
159+
throw CommonError.fileOperationFailed("Email", "create", fileName, e);
160160
}
161161
}
162162
}

0 commit comments

Comments
 (0)