Skip to content

Commit f75a358

Browse files
committed
Better error messages when CSV import fails due to an unexpected EOF
When the CSV import fails because of an unexpected end of file show a better error message. When the file ends but we are still expected more data to come, i.e. we are still in quote mode, no INSERT statement failed. So showing the last SQLite error message will never show you a proper error message. Instead tell the user that their settings might be wrong or the file malformed. See issue #2330.
1 parent 0572b76 commit f75a358

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

src/ImportCsvDialog.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -674,17 +674,19 @@ bool ImportCsvDialog::importCsv(const QString& fileName, const QString& name)
674674
// Some error occurred or the user cancelled the action
675675

676676
// Rollback the entire import. If the action was cancelled, don't show an error message. If it errored, show an error message.
677-
if(result == CSVParser::ParserResult::ParserResultCancelled)
677+
QString message;
678+
if(result == CSVParser::ParserResult::ParserResultError)
678679
{
679-
sqlite3_finalize(stmt);
680-
rollback(this, pdb, &pDb, restorepointName, 0, QString());
681-
return false;
682-
} else {
683680
QString error(sqlite3_errmsg(pDb.get()));
684-
sqlite3_finalize(stmt);
685-
rollback(this, pdb, &pDb, restorepointName, lastRowNum, tr("Inserting row failed: %1").arg(error));
686-
return false;
681+
message = tr("Inserting row failed: %1").arg(error);
682+
} else if(result == CSVParser::ParserResult::ParserResultUnexpectedEOF) {
683+
message = tr("Unexpected end of file. Please make sure that you have configured the correct quote characters and "
684+
"the file is not malformed.");
687685
}
686+
687+
sqlite3_finalize(stmt);
688+
rollback(this, pdb, &pDb, restorepointName, lastRowNum, message);
689+
return false;
688690
}
689691

690692
// Clean up prepared statement

src/csvparser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ CSVParser::ParserResult CSVParser::parse(csvRowFunction insertFunction, QTextStr
294294

295295
// Check if we are in StateNormal or StateEndQuote state. The first is what we should be in for unquoted data and all files which
296296
// end with a line break. The latter is what we are in for quoted data with no final line break.
297-
return (state == StateNormal || state == StateEndQuote) ? ParserResult::ParserResultSuccess : ParserResult::ParserResultError;
297+
return (state == StateNormal || state == StateEndQuote) ? ParserResult::ParserResultSuccess : ParserResult::ParserResultUnexpectedEOF;
298298
}
299299

300300
bool CSVParser::look_ahead(QTextStream& stream, QByteArray& sBuffer, const char** it, const char** sBufferEnd, char expected)

src/csvparser.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ class CSVParser
6060
{
6161
ParserResultSuccess,
6262
ParserResultCancelled,
63-
ParserResultError
63+
ParserResultError,
64+
ParserResultUnexpectedEOF
6465
};
6566

6667
/*!

0 commit comments

Comments
 (0)