mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
Replace GError usage in file_format and load_file with proper try catch blocks
This commit is contained in:
parent
b9e73d923e
commit
d64c66e68d
@ -256,18 +256,25 @@ csv_import_trans_load_settings (CsvImportTrans *info)
|
|||||||
// This Section deals with the separators
|
// This Section deals with the separators
|
||||||
if (info->settings_data->csv_format)
|
if (info->settings_data->csv_format)
|
||||||
{
|
{
|
||||||
info->parse_data->file_format (GncImpFileFormat::CSV, NULL);
|
try
|
||||||
for (i = 0; i < SEP_NUM_OF_TYPES; i++)
|
|
||||||
{
|
{
|
||||||
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(info->sep_buttons[i]), info->settings_data->separator[i]);
|
info->parse_data->file_format (GncImpFileFormat::CSV);
|
||||||
}
|
for (i = 0; i < SEP_NUM_OF_TYPES; i++)
|
||||||
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(info->custom_cbutton), info->settings_data->custom);
|
{
|
||||||
if (info->settings_data->custom)
|
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(info->sep_buttons[i]), info->settings_data->separator[i]);
|
||||||
gtk_entry_set_text (GTK_ENTRY(info->custom_entry), info->settings_data->custom_entry);
|
}
|
||||||
else
|
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(info->custom_cbutton), info->settings_data->custom);
|
||||||
gtk_entry_set_text (GTK_ENTRY(info->custom_entry), "");
|
if (info->settings_data->custom)
|
||||||
|
gtk_entry_set_text (GTK_ENTRY(info->custom_entry), info->settings_data->custom_entry);
|
||||||
|
else
|
||||||
|
gtk_entry_set_text (GTK_ENTRY(info->custom_entry), "");
|
||||||
|
|
||||||
sep_button_clicked (NULL, info);
|
sep_button_clicked (NULL, info);
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
// FIXME Handle file loading errors (possibly thrown by file_format above)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This section deals with the combo's and character encoding
|
// This section deals with the combo's and character encoding
|
||||||
@ -282,21 +289,28 @@ csv_import_trans_load_settings (CsvImportTrans *info)
|
|||||||
// This section deals with the column widths (which are only used for fixed width files)
|
// This section deals with the column widths (which are only used for fixed width files)
|
||||||
if (!info->settings_data->csv_format)
|
if (!info->settings_data->csv_format)
|
||||||
{
|
{
|
||||||
info->parse_data->file_format (GncImpFileFormat::FIXED_WIDTH, NULL);
|
try
|
||||||
GncFwTokenizer *fwtok = dynamic_cast<GncFwTokenizer*>(info->parse_data->tokenizer.get());
|
|
||||||
if (info->settings_data->column_widths, NULL)
|
|
||||||
fwtok->cols_from_string (std::string(info->settings_data->column_widths));
|
|
||||||
|
|
||||||
GError *error = NULL;
|
|
||||||
if (info->parse_data->parse (false, &error))
|
|
||||||
{
|
{
|
||||||
gnc_error_dialog (NULL, "%s", _("There was a problem with the column widths, please review."));
|
info->parse_data->file_format (GncImpFileFormat::FIXED_WIDTH);
|
||||||
g_error_free (error);
|
GncFwTokenizer *fwtok = dynamic_cast<GncFwTokenizer*>(info->parse_data->tokenizer.get());
|
||||||
g_free (group);
|
if (info->settings_data->column_widths, NULL)
|
||||||
g_free (name);
|
fwtok->cols_from_string (std::string(info->settings_data->column_widths));
|
||||||
return;
|
|
||||||
|
GError *error = NULL;
|
||||||
|
if (info->parse_data->parse (false, &error))
|
||||||
|
{
|
||||||
|
gnc_error_dialog (NULL, "%s", _("There was a problem with the column widths, please review."));
|
||||||
|
g_error_free (error);
|
||||||
|
g_free (group);
|
||||||
|
g_free (name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
gnc_csv_preview_update_assist (info);
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
// FIXME Handle file loading errors (possibly thrown by file_format above)
|
||||||
}
|
}
|
||||||
gnc_csv_preview_update_assist (info);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This section deals with the column types
|
// This section deals with the column types
|
||||||
@ -693,19 +707,17 @@ csv_import_trans_file_chooser_confirm_cb (GtkWidget *button, CsvImportTrans *inf
|
|||||||
/* Load the file into parse_data. */
|
/* Load the file into parse_data. */
|
||||||
auto parse_data = new GncTxImport;
|
auto parse_data = new GncTxImport;
|
||||||
/* Assume data is CSV. User can later override to Fixed Width if needed */
|
/* Assume data is CSV. User can later override to Fixed Width if needed */
|
||||||
parse_data->file_format (GncImpFileFormat::CSV, &error);
|
try
|
||||||
if (parse_data->load_file (info->file_name, &error))
|
{
|
||||||
|
parse_data->file_format (GncImpFileFormat::CSV);
|
||||||
|
parse_data->load_file (info->file_name);
|
||||||
|
}
|
||||||
|
catch (std::ifstream::failure& ios_err)
|
||||||
{
|
{
|
||||||
/* If we couldn't load the file ... */
|
/* If we couldn't load the file ... */
|
||||||
gnc_error_dialog (NULL, "%s", error->message);
|
gnc_error_dialog (NULL, "%s", ios_err.what());
|
||||||
if (error->code == GNC_CSV_IMP_ERROR_OPEN)
|
|
||||||
{
|
|
||||||
delete parse_data;
|
delete parse_data;
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
/* If we couldn't guess the encoding, we are content with just
|
|
||||||
* displaying an error message and move on with a blank
|
|
||||||
* display. */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Parse the data. */
|
/* Parse the data. */
|
||||||
@ -983,29 +995,44 @@ static void separated_or_fixed_selected (GtkToggleButton* csv_button, CsvImportT
|
|||||||
/* Set the parsing type correctly. */
|
/* Set the parsing type correctly. */
|
||||||
if (gtk_toggle_button_get_active (csv_button)) /* If we're in CSV mode ... */
|
if (gtk_toggle_button_get_active (csv_button)) /* If we're in CSV mode ... */
|
||||||
{
|
{
|
||||||
info->parse_data->file_format (GncImpFileFormat::CSV, NULL);
|
try
|
||||||
sep_button_clicked (NULL, info);
|
{
|
||||||
// Note: sep_button_clicked also handles reparsing the data, so we're done here
|
info->parse_data->file_format (GncImpFileFormat::CSV);
|
||||||
return;
|
sep_button_clicked (NULL, info);
|
||||||
|
// Note: sep_button_clicked also handles reparsing the data, so we're done here
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
// FIXME Handle file loading errors (possibly thrown by file_format above)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* So we're in fixed-width mode ... */
|
/* So we're in fixed-width mode ... */
|
||||||
info->parse_data->file_format (GncImpFileFormat::FIXED_WIDTH, NULL);
|
try
|
||||||
|
|
||||||
/* Reparse the data. */
|
|
||||||
GError* error = NULL;
|
|
||||||
if (info->parse_data->parse (false, &error))
|
|
||||||
{
|
{
|
||||||
/* Show an error dialog explaining the problem. */
|
info->parse_data->file_format (GncImpFileFormat::FIXED_WIDTH);
|
||||||
gnc_error_dialog (NULL, "%s", error->message);
|
|
||||||
return;
|
/* Reparse the data. */
|
||||||
|
GError* error = NULL;
|
||||||
|
if (info->parse_data->parse (false, &error))
|
||||||
|
{
|
||||||
|
/* Show an error dialog explaining the problem. */
|
||||||
|
gnc_error_dialog (NULL, "%s", error->message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Show the new data. */
|
||||||
|
gnc_csv_preview_update_assist (info);
|
||||||
|
|
||||||
|
/* Refresh the row highlighting */
|
||||||
|
row_selection_update (info);
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
// FIXME Handle file loading errors (possibly thrown by file_format above)
|
||||||
|
// or parse errors from parse above
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Show the new data. */
|
|
||||||
gnc_csv_preview_update_assist (info);
|
|
||||||
|
|
||||||
/* Refresh the row highlighting */
|
|
||||||
row_selection_update (info);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -220,11 +220,17 @@ GncTxImport::~GncTxImport()
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
int GncTxImport::file_format(GncImpFileFormat format,
|
/** Sets the file format for the file to import, which
|
||||||
GError** error)
|
* may cause the file to be reloaded as well if the
|
||||||
|
* previously set file format was different and a
|
||||||
|
* filename was already set.
|
||||||
|
* @param format the new format to set
|
||||||
|
* @exception the reloading of the file may throw std::ifstream::failure
|
||||||
|
*/
|
||||||
|
void GncTxImport::file_format(GncImpFileFormat format)
|
||||||
{
|
{
|
||||||
if (file_fmt == format)
|
if (file_fmt == format)
|
||||||
return 0;
|
return;
|
||||||
|
|
||||||
auto new_encoding = std::string("UTF-8");
|
auto new_encoding = std::string("UTF-8");
|
||||||
auto new_imp_file = std::string();
|
auto new_imp_file = std::string();
|
||||||
@ -242,7 +248,7 @@ int GncTxImport::file_format(GncImpFileFormat format,
|
|||||||
// Set up new tokenizer with common settings
|
// Set up new tokenizer with common settings
|
||||||
// recovered from old tokenizer
|
// recovered from old tokenizer
|
||||||
tokenizer->encoding(new_encoding);
|
tokenizer->encoding(new_encoding);
|
||||||
return load_file(new_imp_file, error);
|
load_file(new_imp_file);
|
||||||
}
|
}
|
||||||
GncImpFileFormat GncTxImport::file_format()
|
GncImpFileFormat GncTxImport::file_format()
|
||||||
{
|
{
|
||||||
@ -274,25 +280,23 @@ void GncTxImport::convert_encoding (const std::string& encoding)
|
|||||||
* @param parse_data Data that is being parsed
|
* @param parse_data Data that is being parsed
|
||||||
* @param filename Name of the file that should be opened
|
* @param filename Name of the file that should be opened
|
||||||
* @param error Will contain an error if there is a failure
|
* @param error Will contain an error if there is a failure
|
||||||
|
* @exception may throw std::ifstream::failure on any io error
|
||||||
* @return 0 on success, 1 on failure
|
* @return 0 on success, 1 on failure
|
||||||
*/
|
*/
|
||||||
int GncTxImport::load_file (const std::string& filename,
|
void GncTxImport::load_file (const std::string& filename)
|
||||||
GError** error)
|
|
||||||
{
|
{
|
||||||
|
|
||||||
/* Get the raw data first and handle an error if one occurs. */
|
/* Get the raw data first and handle an error if one occurs. */
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
tokenizer->load_file (filename);
|
tokenizer->load_file (filename);
|
||||||
return 0;
|
return;
|
||||||
}
|
}
|
||||||
catch (std::ifstream::failure& ios_err)
|
catch (std::ifstream::failure& ios_err)
|
||||||
{
|
{
|
||||||
/* TODO Handle file opening errors more specifically,
|
// Just log the error and pass it on the call stack for proper handling
|
||||||
* e.g. inexistent file versus no read permission. */
|
|
||||||
PWARN ("Error: %s", ios_err.what());
|
PWARN ("Error: %s", ios_err.what());
|
||||||
g_set_error (error, GNC_CSV_IMP_ERROR, GNC_CSV_IMP_ERROR_OPEN, "%s", _("File opening failed."));
|
throw;
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,10 +127,10 @@ public:
|
|||||||
GncTxImport(GncImpFileFormat format = GncImpFileFormat::UNKNOWN);
|
GncTxImport(GncImpFileFormat format = GncImpFileFormat::UNKNOWN);
|
||||||
~GncTxImport();
|
~GncTxImport();
|
||||||
|
|
||||||
int file_format(GncImpFileFormat format, GError** error);
|
void file_format(GncImpFileFormat format);
|
||||||
GncImpFileFormat file_format();
|
GncImpFileFormat file_format();
|
||||||
|
|
||||||
int load_file (const std::string& filename, GError** error);
|
void load_file (const std::string& filename);
|
||||||
void convert_encoding (const std::string& encoding);
|
void convert_encoding (const std::string& encoding);
|
||||||
|
|
||||||
int parse (bool guessColTypes, GError** error);
|
int parse (bool guessColTypes, GError** error);
|
||||||
|
Loading…
Reference in New Issue
Block a user