From 17c4cac8d041a241b9dc8dafb8d1f77c8bcdf7d5 Mon Sep 17 00:00:00 2001 From: Felix Cremer Date: Thu, 27 Jul 2023 14:42:09 +0200 Subject: [PATCH] Catch error in write to remove broken file --- src/write.jl | 9 ++++++++- test/sources/ncdatasets.jl | 10 ++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/write.jl b/src/write.jl index 78d6d275..3d567c4b 100644 --- a/src/write.jl +++ b/src/write.jl @@ -8,7 +8,14 @@ Keyword arguments are passed to the `write` method for the backend. function Base.write( filename::AbstractString, A::AbstractRaster; source=_sourcetype(filename), kw... ) - write(filename, source, A; kw...) + try + write(filename, source, A; kw...) + catch e + if isfile(filename) + rm(filename) + end + rethrow(e) + end end Base.write(A::AbstractRaster; kw...) = write(filename(A), A; kw...) function Base.write( diff --git a/test/sources/ncdatasets.jl b/test/sources/ncdatasets.jl index ea2b73d6..18d45ac4 100644 --- a/test/sources/ncdatasets.jl +++ b/test/sources/ncdatasets.jl @@ -289,6 +289,16 @@ end @test size2 > size1*1.8 # two variable isfile(fn) && rm(fn) + # test erroring doesn't write file + fn = "test_broken.nc" + try + write(fn, r1; deflatelevel="a") + catch e + end + @test !isfile(fn) + isfile(fn) && rm(fn) + + @testset "non allowed values" begin # TODO return this test when the changes in NCDatasets.jl settle # @test_throws ArgumentError write(filename, convert.(Union{Missing,Float16}, geoA))