Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create ZstdError error type. Test known, stable error codes. #54

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/CodecZstd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export
ZstdCompressorStream,
ZstdFrameCompressor,
ZstdDecompressor,
ZstdDecompressorStream
ZstdDecompressorStream,
ZstdError

import TranscodingStreams:
TranscodingStreams,
Expand Down
9 changes: 4 additions & 5 deletions src/compression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
function TranscodingStreams.initialize(codec::ZstdCompressor)
code = initialize!(codec.cstream, codec.level)
if iserror(code)
zstderror(codec.cstream, code)
throw(ZstdError(code))

Check warning on line 84 in src/compression.jl

View check run for this annotation

Codecov / codecov/patch

src/compression.jl#L84

Added line #L84 was not covered by tests
end
reset!(codec.cstream.ibuffer)
reset!(codec.cstream.obuffer)
Expand All @@ -92,7 +92,7 @@
if codec.cstream.ptr != C_NULL
code = free!(codec.cstream)
if iserror(code)
zstderror(codec.cstream, code)
throw(ZstdError(code))

Check warning on line 95 in src/compression.jl

View check run for this annotation

Codecov / codecov/patch

src/compression.jl#L95

Added line #L95 was not covered by tests
end
codec.cstream.ptr = C_NULL
end
Expand All @@ -104,7 +104,7 @@
function TranscodingStreams.startproc(codec::ZstdCompressor, mode::Symbol, error::Error)
code = reset!(codec.cstream, 0 #=unknown source size=#)
if iserror(code)
error[] = ErrorException("zstd error")
error[] = ZstdError(code)

Check warning on line 107 in src/compression.jl

View check run for this annotation

Codecov / codecov/patch

src/compression.jl#L107

Added line #L107 was not covered by tests
return :error
end
return :ok
Expand Down Expand Up @@ -144,8 +144,7 @@
Δin = Int(cstream.ibuffer.pos - ibuffer_starting_pos)
Δout = Int(cstream.obuffer.pos)
if iserror(code)
ptr = LibZstd.ZSTD_getErrorName(code)
error[] = ErrorException("zstd error: " * unsafe_string(ptr))
error[] = ZstdError(code)

Check warning on line 147 in src/compression.jl

View check run for this annotation

Codecov / codecov/patch

src/compression.jl#L147

Added line #L147 was not covered by tests
return Δin, Δout, :error
else
return Δin, Δout, input.size == 0 && code == 0 ? :end : :ok
Expand Down
8 changes: 4 additions & 4 deletions src/decompression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
function TranscodingStreams.initialize(codec::ZstdDecompressor)
code = initialize!(codec.dstream)
if iserror(code)
zstderror(codec.dstream, code)
throw(ZstdError(code))

Check warning on line 39 in src/decompression.jl

View check run for this annotation

Codecov / codecov/patch

src/decompression.jl#L39

Added line #L39 was not covered by tests
end
reset!(codec.dstream.ibuffer)
reset!(codec.dstream.obuffer)
Expand All @@ -47,7 +47,7 @@
if codec.dstream.ptr != C_NULL
code = free!(codec.dstream)
if iserror(code)
zstderror(codec.dstream, code)
throw(ZstdError(code))

Check warning on line 50 in src/decompression.jl

View check run for this annotation

Codecov / codecov/patch

src/decompression.jl#L50

Added line #L50 was not covered by tests
end
codec.dstream.ptr = C_NULL
end
Expand All @@ -59,7 +59,7 @@
function TranscodingStreams.startproc(codec::ZstdDecompressor, mode::Symbol, error::Error)
code = reset!(codec.dstream)
if iserror(code)
error[] = ErrorException("zstd error")
error[] = ZstdError(code)

Check warning on line 62 in src/decompression.jl

View check run for this annotation

Codecov / codecov/patch

src/decompression.jl#L62

Added line #L62 was not covered by tests
return :error
end
return :ok
Expand All @@ -77,7 +77,7 @@
Δin = Int(dstream.ibuffer.pos)
Δout = Int(dstream.obuffer.pos)
if iserror(code)
error[] = ErrorException("zstd error")
error[] = ZstdError(code)

Check warning on line 80 in src/decompression.jl

View check run for this annotation

Codecov / codecov/patch

src/decompression.jl#L80

Added line #L80 was not covered by tests
return Δin, Δout, :error
else
if code == 0
Expand Down
13 changes: 13 additions & 0 deletions src/libzstd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,24 @@
return LibZstd.ZSTD_isError(code) != 0
end

# deprecated error reporting
function zstderror(stream, code::Csize_t)
zstderror(code)

Check warning on line 10 in src/libzstd.jl

View check run for this annotation

Codecov / codecov/patch

src/libzstd.jl#L10

Added line #L10 was not covered by tests
end
function zstderror(code::Integer)

Check warning on line 12 in src/libzstd.jl

View check run for this annotation

Codecov / codecov/patch

src/libzstd.jl#L12

Added line #L12 was not covered by tests
ptr = LibZstd.ZSTD_getErrorName(code)
error("zstd error: ", unsafe_string(ptr))
end

# new error reporting
struct ZstdError <: Exception
code::Csize_t
end
ZstdError() = ZstdError(typemax(Csize_t))

Check warning on line 21 in src/libzstd.jl

View check run for this annotation

Codecov / codecov/patch

src/libzstd.jl#L21

Added line #L21 was not covered by tests
function Base.show(io::IO, e::ZstdError)
print(io, "ZstdError: ", unsafe_string(LibZstd.ZSTD_getErrorName(e.code)))
end

function max_clevel()
return LibZstd.ZSTD_maxCLevel()
end
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,5 @@ Random.seed!(1234)
end

include("compress_endOp.jl")
include("zstderror.jl")
end
47 changes: 47 additions & 0 deletions test/zstderror.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Test
using CodecZstd: ZstdError

@static if VERSION ≥ v"1.8"
@testset "ZSTD Errors" begin
@test_throws "ZstdError: No error detected" throw(ZstdError(0))
@test_throws "ZstdError: Error (generic)" throw(ZstdError(18446744073709551615))
@test_throws "ZstdError: Unknown frame descriptor" throw(ZstdError(18446744073709551606))
@test_throws "ZstdError: Version not supported" throw(ZstdError(18446744073709551604))
@test_throws "ZstdError: Unsupported frame parameter" throw(ZstdError(18446744073709551602))
@test_throws "ZstdError: Frame requires too much memory for decoding" throw(ZstdError(18446744073709551600))
@test_throws "ZstdError: Data corruption detected" throw(ZstdError(18446744073709551596))
@test_throws "ZstdError: Restored data doesn't match checksum" throw(ZstdError(18446744073709551594))
@test_throws "ZstdError: Header of Literals' block doesn't respect format specification" throw(ZstdError(18446744073709551592))
@test_throws "ZstdError: Dictionary is corrupted" throw(ZstdError(18446744073709551586))
@test_throws "ZstdError: Dictionary mismatch" throw(ZstdError(18446744073709551584))
@test_throws "ZstdError: Cannot create Dictionary from provided samples" throw(ZstdError(18446744073709551582))
@test_throws "ZstdError: Unsupported parameter" throw(ZstdError(18446744073709551576))
@test_throws "ZstdError: Unsupported combination of parameters" throw(ZstdError(18446744073709551575))
@test_throws "ZstdError: Parameter is out of bound" throw(ZstdError(18446744073709551574))
@test_throws "ZstdError: tableLog requires too much memory : unsupported" throw(ZstdError(18446744073709551572))
@test_throws "ZstdError: Unsupported max Symbol Value : too large" throw(ZstdError(18446744073709551570))
@test_throws "ZstdError: Specified maxSymbolValue is too small" throw(ZstdError(18446744073709551568))
@test_throws "ZstdError: pledged buffer stability condition is not respected" throw(ZstdError(18446744073709551566))
@test_throws "ZstdError: Operation not authorized at current processing stage" throw(ZstdError(18446744073709551556))
@test_throws "ZstdError: Context should be init first" throw(ZstdError(18446744073709551554))
@test_throws "ZstdError: Allocation error : not enough memory" throw(ZstdError(18446744073709551552))
@test_throws "ZstdError: workSpace buffer is not large enough" throw(ZstdError(18446744073709551550))
@test_throws "ZstdError: Destination buffer is too small" throw(ZstdError(18446744073709551546))
@test_throws "ZstdError: Src size is incorrect" throw(ZstdError(18446744073709551544))
@test_throws "ZstdError: Operation on NULL destination buffer" throw(ZstdError(18446744073709551542))
@test_throws "ZstdError: Operation made no progress over multiple calls, due to output buffer being full" throw(ZstdError(18446744073709551536))
@test_throws "ZstdError: Operation made no progress over multiple calls, due to input being empty" throw(ZstdError(18446744073709551534))
end
end

# Use the following function to print the tests above
function print_error_tests()
err_codes = [0, 1, 10, 12, 14, 16, 20, 22, 24, 30, 32, 34, 40, 41, 42, 44, 46, 48, 50, 60, 62, 64, 66, 70, 72, 74, 80, 82];
println("@testset \"ZSTD Errors\" begin")
for err_code in err_codes
code = typemax(Csize_t)-err_code+1
println(" @test_throws \"" * string(CodecZstd.ZstdError(typemax(Csize_t) - err_code + 1)) * "\" throw(ZstdError($code))")
end
println("end")
end

Loading