Skip to content

Commit bfd1a15

Browse files
committed
Add LoaderError and WriterError exceptions
When throwing these errors in library code, FileIO will emit an info message and then try the next registered loader / writer. These are useful for marking specific failed assertions. If the file does not conform to what the loader expects, it can bail out and allow the next reader to attempt to load the file. At the same time, legitimate programming errors will halt execution with an error message.
1 parent 3596268 commit bfd1a15

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ os:
33
- linux
44
- osx
55
julia:
6-
- release
6+
- 0.3
7+
- 0.4
78
- nightly
89
notifications:
910
email: false

src/FileIO.jl

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,35 @@ export DataFormat,
3636
stream,
3737
unknown
3838

39+
import Base.showerror
40+
3941
include("query.jl")
4042
include("loadsave.jl")
4143
include("registry.jl")
4244

45+
@doc """
46+
`LoaderError` should be thrown when loader library code fails, and other libraries should
47+
be given the chance to recover from the error. Reports the library name and an error message:
48+
LoaderError("ImageMagick", "Foo not available")
49+
""" ->
50+
type LoaderError <: Exception
51+
library::UTF8String
52+
msg::UTF8String
53+
end
54+
Base.showerror(io::IO, e::LoaderError) = println(io, e.library, " load error: ",
55+
msg, "\n Will try next loader.")
4356

57+
@doc """
58+
`WriterError` should be thrown when writer library code fails, and other libraries should
59+
be given the chance to recover from the error. Reports the library name and an error message:
60+
WriterError("ImageMagick", "Foo not available")
61+
""" ->
62+
type WriterError <: Exception
63+
library::UTF8String
64+
msg::UTF8String
65+
end
66+
Base.showerror(io::IO, e::WriterError) = println(io, e.library, " writer error: ",
67+
msg, "\n Will try next writer.")
4468

4569
@doc """
4670
- `load(filename)` loads the contents of a formatted file, trying to infer
@@ -59,10 +83,14 @@ function load(s::@compat(Union{AbstractString,IO}), args...; options...)
5983
Library = check_loader(library)
6084
return Library.load(q, args...; options...)
6185
catch e
62-
last_exception = e
86+
if isa(e, LoaderError)
87+
info(e.library, " failed. ", e.msg)
88+
info(" Will try next loader, if available")
89+
else
90+
rethrow(e)
91+
end
6392
end
6493
end
65-
rethrow(last_exception)
6694
end
6795

6896
@doc """
@@ -80,10 +108,14 @@ function save(s::@compat(Union{AbstractString,IO}), data...; options...)
80108
Library = check_saver(library)
81109
return Library.save(q, data...; options...)
82110
catch e
83-
last_exception = e #TODO, better and standardized exception propagation system
111+
if isa(e, WriterError)
112+
info(e.library, " failed. ", e.msg)
113+
info(" Will try next writer, if available")
114+
else
115+
rethrow(e)
116+
end
84117
end
85118
end
86-
rethrow(last_exception)
87119
end
88120

89121
# Forced format
@@ -108,10 +140,14 @@ function Base.writemime(io::IO, mime::MIME, x)
108140
check_mime(pkg)
109141
return writemime(Stream(DataFormat{pkg}, io), mime, x)
110142
catch e
111-
last_exception = e
143+
if isa(e, WriterError)
144+
info(e.library, " failed. ", e.msg)
145+
info(" Will try next writer, if available")
146+
else
147+
rethrow(e)
148+
end
112149
end
113150
end
114-
rethrow(last_exception)
115151
end
116152

117153
# Fallbacks

0 commit comments

Comments
 (0)