Skip to content

Commit

Permalink
refactor: change error type to ArgumentError when field is too short/…
Browse files Browse the repository at this point in the history
…long or length is unknown
  • Loading branch information
mchitre committed Oct 3, 2023
1 parent 76ba394 commit 1e5943b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/ProtocolDataUnits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,14 @@ function Base.write(io::IO, pdu::T; hooks=true) where {T<:AbstractPDU}
if n === WireEncoded()
varwrite(io, v)
elseif n === nothing
throw(ErrorException("Length of field $(f) is unknown"))
throw(ArgumentError("Length of field $(f) is unknown"))
else
if n === missing
write(io, v)
else
length(v) > n && throw(ErrorException("Value too long for field $(f) (expected $n, actual $(length(n)))"))
length(v) > n && throw(ArgumentError("Value too long for field $(f) (expected $n, actual $(length(n)))"))
if length(v) < n
autopad || throw(ErrorException("Value too short for field $(f) (expected $n, actual $(length(n)))"))
autopad || throw(ArgumentError("Value too short for field $(f) (expected $n, actual $(length(n)))"))
v = vcat(v, zeros(eltype(v), n - length(v)))
end
write(io, v)
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ end

pdu = MyLessSimplePDU(1, "hello world! how are you?")

@test_throws ErrorException PDU.encode(pdu)
@test_throws ArgumentError PDU.encode(pdu)

Base.length(::Type{MyLessSimplePDU}, ::Val{:b}, info) = info.length - 2

Expand Down

0 comments on commit 1e5943b

Please sign in to comment.