你好,
我目前在两份工作之间,时间多得很,正好用来被一个 100 行的程序搞糊涂。
ledger-import 读取分号分隔的 CSV,把清洗后的行写入暂存文件。除非整个文件都解析成功,否则暂存里不允许留下任何东西。我的测试文件 data/march.csv 第 4 行有个损坏的金额(-12,5x)。我预期看到 import rejected: line 4: bad amount ...,退出码 2。实际得到的是下面这个,退出码是 3。
SEVERE: staging problem: staging file build/staging.tmp closed with 2 uncommitted rows
行号和错误的值哪里都没出现。我在 parse() 的 catch 块里临时加了 System.err.println,它确实会打印,所以 ImportFailure 确实被创建了,只是一直没到达 Main。把第 4 行改对之后导入就正常了。
我最先怀疑的是 Main 里的 catch 顺序和 logging.properties 的配置。把两个 catch 块调换顺序没有任何有用的变化。相关部分如下。
// Importer.run()
StagingWriter writer = new StagingWriter(staging);
try {
...
writer.add(parse(lineNo, line));
...
writer.commit();
return writer.count();
} finally {
writer.close();
}
// StagingWriter.close()
out.close();
if (!committed && rows > 0) {
Files.deleteIfExists(path);
throw new IOException("staging file " + path + " closed with " + rows + " uncommitted rows");
}
Main 先捕获 ImportFailure(退出码 2),再捕获 IOException(退出码 3)。
会不会日志根本就不是问题?我的异常到底去哪了?
从英语翻译·查看原文Hello,
I am between jobs, so I have all the time in the world to be confused by a 100-line program.
ledger-import reads a semicolon CSV and writes cleaned rows to a staging file. Nothing is allowed to stay in staging unless the whole file parses. My test file data/march.csv has a broken amount on line 4 (-12,5x). I expected import rejected: line 4: bad amount ... and exit code 2. What I get is this, with exit code 3:
SEVERE: staging problem: staging file build/staging.tmp closed with 2 uncommitted rows
The line number and the bad value appear nowhere. With a temporary System.err.println in the catch block of parse(), it does print, so the ImportFailure really is created. It just never arrives in Main. When I fix line 4 the import works fine.
My first suspects were the catch order in Main and the logging.properties setup. Swapping the two catch blocks changed nothing useful. The relevant parts:
// Importer.run()
StagingWriter writer = new StagingWriter(staging);
try {
...
writer.add(parse(lineNo, line));
...
writer.commit();
return writer.count();
} finally {
writer.close();
}
// StagingWriter.close()
out.close();
if (!committed && rows > 0) {
Files.deleteIfExists(path);
throw new IOException("staging file " + path + " closed with " + rows + " uncommitted rows");
}
Main catches ImportFailure first (exit 2), then IOException (exit 3).
Is there a possibility that the logging is not the problem at all? Where does my exception go?