Skip to content

Commit

Permalink
ADD: Error handling for UnicodeError (#347)
Browse files Browse the repository at this point in the history
Adds Unicode Error message that informs user only UTF-8 and ASCII files
are supported, and to re-encode file into a supported format.

Adds unit test

Closes #328
  • Loading branch information
pseudocubic authored and ccoffrin committed Aug 3, 2018
1 parent e93b7b1 commit 6ef816f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/io/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@ PowerModels data structure. All fields from PTI files will be imported if
`import_all` is true (Default: false).
"""
function parse_file(file::String; import_all=false)
if endswith(file, ".m")
pm_data = PowerModels.parse_matpower(file)
elseif endswith(lowercase(file), ".raw")
info(LOGGER, "The PSS(R)E parser currently supports buses, loads, shunts, generators, branches, transformers, and dc lines")
pm_data = PowerModels.parse_psse(file; import_all=import_all)
else
pm_data = parse_json(file)
end
try
if endswith(file, ".m")
pm_data = PowerModels.parse_matpower(file)
elseif endswith(lowercase(file), ".raw")
info(LOGGER, "The PSS(R)E parser currently supports buses, loads, shunts, generators, branches, transformers, and dc lines")
pm_data = PowerModels.parse_psse(file; import_all=import_all)
else
pm_data = parse_json(file)
end

return pm_data
return pm_data
catch e
if isa(e, UnicodeError)
error(LOGGER, "UnicodeError: PowerModels can only load UTF-8 or ASCII encoded files, re-encode \"$file\" to supported encoding")
end
end
end


Expand Down
6 changes: 6 additions & 0 deletions test/data/pti/parser_test_j.raw
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
0, 100.00, 33, 0, 1, 60.00


1002,, 345.0000, 2, 101, 201, 301,1.00000000, 0.000000, 1.10000, 0.90000, 1.10000, 0.90000
1005,' � ', 87.0000, 2, 101, 201, 301,1.00000000, 3.000000, 1.10000, 0.90000, 1.10000, 0.90000
Q
1 change: 1 addition & 0 deletions test/pti.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ TESTLOG = getlogger(PowerModels)
PowerModels.parse_pti("../test/data/pti/parser_test_c.raw"))
@test_throws(TESTLOG, ErrorException, PowerModels.parse_pti("../test/data/pti/parser_test_d.raw"))
@test_warn(TESTLOG, "GNE DEVICE parsing is not supported.", PowerModels.parse_pti("../test/data/pti/parser_test_h.raw"))
@test_throws(TESTLOG, ErrorException, PowerModels.parse_file("../test/data/pti/parser_test_j.raw"))

setlevel!(TESTLOG, "error")
end
Expand Down

0 comments on commit 6ef816f

Please sign in to comment.