Skip to content

Commit

Permalink
(core) Show proper message on empty Excel import, rather than a code …
Browse files Browse the repository at this point in the history
…error

Summary:
- Previously showed "UnboundLocalError". Now will show:
    Import failed: Failed to parse Excel file.
    Error: No tables found (1 empty tables skipped)
- Also fix logging for import code

Test Plan: Added a test case

Reviewers: georgegevoian

Reviewed By: georgegevoian

Differential Revision: https://phab.getgrist.com/D3396
  • Loading branch information
dsagal committed Apr 27, 2022
1 parent dcafa96 commit e59dcc1
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/client/components/PluginScreen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ const cssModalBody = styled('div', `
padding: 16px 0;
overflow-y: auto;
max-width: 470px;
white-space: pre-line;
`);

const cssModalHeader = styled('div', `
Expand Down
2 changes: 1 addition & 1 deletion app/server/lib/DocPluginManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export class DocPluginManager {
'.csv' : 'CSV',
};
const fileType = extToType[path.extname(fileName)] || path.extname(fileName);
throw new Error(`Failed to parse ${fileType} file. Error: ${messages.join("; ")}`);
throw new Error(`Failed to parse ${fileType} file.\nError: ${messages.join("; ")}`);
}
throw new Error(`File format is not supported.`);
}
Expand Down
10 changes: 8 additions & 2 deletions sandbox/grist/imports/import_xls.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def parse_file(file_path, orig_name, parse_options=None, table_name_hint=None, n
return parse_open_file(f, orig_name, table_name_hint=table_name_hint)
except Exception as e:
# Log the full error, but simplify the thrown error to omit the unhelpful extra args.
log.info("import_xls parse_file failed: %s", e)
log.warn("import_xls parse_file failed: %s", e)
if six.PY2 and e.args and isinstance(e.args[0], six.string_types):
raise Exception(e.args[0])
raise
Expand All @@ -58,6 +58,7 @@ def parse_open_file(file_obj, orig_name, table_name_hint=None):
if table_set.encoding == 'ascii':
table_set.encoding = 'utf8'

skipped_tables = 0
export_list = []
# A table set is a collection of tables:
for row_set in table_set.tables:
Expand Down Expand Up @@ -101,6 +102,7 @@ def parse_open_file(file_obj, orig_name, table_name_hint=None):

if not table_data:
# Don't add tables with no columns.
skipped_tables += 1
continue

log.info("Output table %r with %d columns", table_name, len(column_metadata))
Expand All @@ -112,8 +114,12 @@ def parse_open_file(file_obj, orig_name, table_name_hint=None):
"table_data": table_data
})

parse_options = {}
if not export_list:
if skipped_tables:
raise Exception("No tables found ({} empty tables skipped)".format(skipped_tables))
raise Exception("No tables found")

parse_options = {}
return parse_options, export_list


Expand Down
4 changes: 4 additions & 0 deletions sandbox/grist/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
sys.path.append('thirdparty')
# pylint: disable=wrong-import-position

import logging
import marshal
import functools

Expand All @@ -25,6 +26,9 @@
import logger
log = logger.Logger(__name__, logger.INFO)

# Configure logging module to behave similarly to logger. (It may be OK to get rid of logger.)
logging.basicConfig(format="[%(levelname)s] [%(name)s] %(message)s")

def table_data_from_db(table_name, table_data_repr):
if table_data_repr is None:
return actions.TableData(table_name, [], {})
Expand Down

0 comments on commit e59dcc1

Please sign in to comment.