Skip to content

Commit

Permalink
Implemented Exceptions for Unloaded Backends (#467)
Browse files Browse the repository at this point in the history
* Updated error message when attempting to read a file without the appropriate backend.

The previous error message for reading rasters without an imported backend was confusing. The new message informs callers of the change and suggests the appropriate backend to use.

* Update sources.jl

* Updated error message when attempting to read a file without the appropriate backend.

The previous error message for reading rasters without an imported backend was confusing. The new message informs callers of the change and suggests the appropriate backend to use.

* Updated error message when attempting to read a file without the appropriate backend.

The previous error message for reading rasters without an imported backend was confusing. The new message informs callers of the change and suggests the appropriate backend to use.

The error now reads, "`Rasters.jl` requires backends to be loaded externally as of version 0.8. Run `import $packagename` to read $filename"

* Added warning to docs regarding extensions

* Added warning to docs regarding extensions

* Added warning to docs regarding extensions

* Implemented  to display a common error message when appropriate backend is not loaded

* Made BackendException non-mutable
  • Loading branch information
JoshuaBillson committed Jun 28, 2023
1 parent 119b441 commit 70941cd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ multi-layered stacks, and multi-file series of arrays and stacks.

_A RasterStack of EarthEnv HabitatHeterogeneity layers, trimmed to Australia and plotted with Plots.jl_

## Packages extensions and Rasters 0.8 and onwards
## :warning: Packages extensions and Rasters 0.8 and onwards

On Julia 1.9 we can put additional packages in extensions, so the code only loads when
you load a specific package. Rasters.jl was always intended to work like this,
Expand Down
4 changes: 2 additions & 2 deletions src/extensions.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# extensions

# stubs that need ArchGDAL
resample(args...; kw...) = error("Run `using ArchGDAL` to use `resample`")
warp(args...; kw...) = error("Run `using ArchGDAL` to use `warp`")
resample(args...; kw...) = throw(BackendException("ArchGDAL"))
warp(args...; kw...) = throw(BackendException("ArchGDAL"))

# Other shared stubs
function layerkeys end
Expand Down
12 changes: 11 additions & 1 deletion src/sources/sources.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ const EXT2SOURCE = Dict(
".h5" => SMAPsource
)

# exception to be raised when backend extension is not satisfied
struct BackendException <: Exception
backend
end

# error message to show when backend is not loaded
function Base.showerror(io::IO, e::BackendException)
print(io, "`Rasters.jl` requires backends to be loaded externally as of version 0.8. Run `import $(e.backend)` to fix this error.")
end

# Get the source backend for a file extension, falling back to GDALsource
_sourcetype(filename::AbstractString) = get(EXT2SOURCE, splitext(filename)[2], GDALsource)
_sourcetype(filenames::NamedTuple) = _sourcetype(first(filenames))
Expand All @@ -59,5 +69,5 @@ function _open(f, filename::AbstractString; source=_sourcetype(filename), kw...)
end
function _open(f, T::Type, filename::AbstractString; kw...)
packagename = SOURCE2PACAKGENAME[T]
error("`Rasters.jl` requires backends to be loaded externally as of version 0.8. Run `import $packagename` to read $filename")
throw(BackendException(packagename))
end

0 comments on commit 70941cd

Please sign in to comment.