Skip to content

Commit

Permalink
checksum: Add Req.ChecksumMismatchError
Browse files Browse the repository at this point in the history
Closes #254
  • Loading branch information
wojtekmach committed Nov 29, 2023
1 parent e94d744 commit 899bb7e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
11 changes: 11 additions & 0 deletions lib/req/checksum_mismatch_error.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule Req.ChecksumMismatchError do
defexception [:expected, :actual]

def message(%{expected: expected, actual: actual}) do
"""
checksum mismatch
expected: #{expected}
actual: #{actual}\
"""
end
end
25 changes: 11 additions & 14 deletions lib/req/steps.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1250,21 +1250,18 @@ defmodule Req.Steps do
|> :crypto.hash_final()
|> Base.encode16(case: :lower, padding: false))

if expected != actual do
raise """
checksum mismatch
expected: #{expected}
actual: #{actual}\
"""
end

request =
update_in(
request.private,
&Map.drop(&1, [:req_checksum_hash, :req_checksum_expected, :req_checksum_type])
)
if expected == actual do
request =
update_in(
request.private,
&Map.drop(&1, [:req_checksum_hash, :req_checksum_expected, :req_checksum_type])
)

{request, response}
{request, response}
else
exception = Req.ChecksumMismatchError.exception(expected: expected, actual: actual)
{request, exception}
end
else
{request, response}
end
Expand Down
6 changes: 3 additions & 3 deletions test/req/steps_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ defmodule Req.StepsTest do
resp = Req.get!(req, checksum: @foo_sha256)
assert resp.body == "foo"

assert_raise RuntimeError,
assert_raise Req.ChecksumMismatchError,
"""
checksum mismatch
expected: sha1:bad
Expand Down Expand Up @@ -323,7 +323,7 @@ defmodule Req.StepsTest do
resp = Req.get!(req, checksum: @foo_sha256)
assert resp.body == "foo"

assert_raise RuntimeError,
assert_raise Req.ChecksumMismatchError,
"""
checksum mismatch
expected: sha1:bad
Expand Down Expand Up @@ -351,7 +351,7 @@ defmodule Req.StepsTest do
resp = Req.get!(req, checksum: @foo_sha256)
assert resp.body == ["foo"]

assert_raise RuntimeError,
assert_raise Req.ChecksumMismatchError,
"""
checksum mismatch
expected: sha1:bad
Expand Down

0 comments on commit 899bb7e

Please sign in to comment.