Skip to content

Commit

Permalink
XLS vs XLSX errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jdunkerley committed May 6, 2022
1 parent 9adbd33 commit 24d6520
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
25 changes: 12 additions & 13 deletions distribution/lib/Standard/Table/0.0.0-dev/src/Io/Excel.enso
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from Standard.Base import Integer, Text, Nothing, Boolean, Illegal_Argument_Error, Any, Error, Panic, File, Vector, False
from Standard.Base import Integer, Text, Nothing, Boolean, Illegal_Argument_Error, Any, Error, Panic, File, Vector, False, IO
from Standard.Base.Error.Problem_Behavior as Problem_Behavior_Module import Problem_Behavior

import Standard.Table.Data.Table
from Standard.Table.Error as Error_Module import Invalid_Location

polyglot java import org.enso.table.format.xlsx.Range as Java_Range
polyglot java import org.enso.table.format.xlsx.Reader

polyglot java import java.lang.IllegalArgumentException
polyglot java import java.io.IOException

polyglot java import org.apache.poi.UnsupportedFileFormatException

## Specified the part of an Excel Workbook to Read
type Excel_Section
Expand Down Expand Up @@ -69,17 +70,15 @@ type Excel_Range

Arguments:
- column: 1-based index to check
- limit: maximum valid index, defaults to Excel 2007+ limit of 16,384
is_valid_column : Integer -> Integer -> Boolean
is_valid_column column (limit=16384) = (column > 0) && (column <= limit)
is_valid_column : Integer -> Boolean
is_valid_column column = (column > 0) && (column <= 16384)

## Validates if a row index (1-based) is within the valid range for Excel.

Arguments:
- row: 1-based index to check
- limit: maximum valid index, defaults to Excel 2007+ limit of 1,048,576
is_valid_row : Integer -> Integer -> Boolean
is_valid_row row (limit=1048576) = (row > 0) && (row <= limit)
is_valid_row : Integer -> Boolean
is_valid_row row = (row > 0) && (row <= 1048576)

## Given a column name parse to the index (1-based) or return index unchanged.
column_index : (Text|Integer) -> Integer
Expand Down Expand Up @@ -171,11 +170,11 @@ read_excel file section _ xls_format=False =
Excel_Range _ -> Reader.readRange stream address.java_range skip_rows row_limit xls_format
Text -> Reader.readRangeByName stream address skip_rows row_limit xls_format

file_failure caught_panic = File.wrap_io_exception file caught_panic.payload.cause.getCause
catch_file_failure = Panic.catch IOException handler=file_failure

bad_argument caught_panic = Error.throw (Invalid_Location caught_panic.payload.cause.getCause)
catch_bad_argument = Panic.catch IllegalArgumentException handler=bad_argument
handle_bad_argument = Panic.catch IllegalArgumentException handler=bad_argument

bad_format caught_panic = Error.throw (File.Io_Error file caught_panic.payload.cause.getMessage)
handle_bad_format = Panic.catch UnsupportedFileFormatException handler=bad_format

catch_file_failure <| catch_bad_argument <|
File.handle_java_exceptions file <| handle_bad_argument <| handle_bad_format <|
file.with_input_stream [File.Option.Read] stream->(stream.with_java_stream reader)
12 changes: 11 additions & 1 deletion test/Table_Tests/src/Excel_Spec.enso
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from Standard.Base import Nothing, File, Illegal_Argument_Error
from Standard.Base import Nothing, File, Illegal_Argument_Error, True, False

import Standard.Base.Data.Time.Date

Expand Down Expand Up @@ -198,4 +198,14 @@ spec =
table_4 = xlsx_sheet.read (File_Format.Excel (Sheet "Sheet1" row_limit=6))
table_4.length . should_equal 6

Test.group "Problems" <|
Test.specify "should handle non-existing file gracefully" <|
bad_file = Enso_Project.data / "DoesNotExists.xlsx"
bad_file.read (File_Format.Excel (Range "Sheet1!A:C")) . should_fail_with File.File_Not_Found

Test.specify "should handle wrong xls_format gracefully" <|
xlsx_sheet.read (File_Format.Excel (Range "Sheet1!A:C") True) . should_fail_with File.Io_Error
xls_sheet.read (File_Format.Excel (Range "Sheet1!A:C") False) . should_fail_with File.Io_Error


main = Test.Suite.run_main here.spec

0 comments on commit 24d6520

Please sign in to comment.