Skip to content

Commit

Permalink
Merge #613
Browse files Browse the repository at this point in the history
613: Fix AWSException construction for JSON-encoded responses with invalid bodies r=mattBrzezinski a=Octogonapus

This PR fixes a bug I ran into when receiving a JSON-encoded response with an invalid JSON body.
I received this response from a custom API GW endpoint, not from an official AWS endpoint.


Co-authored-by: Octogonapus <[email protected]>
Co-authored-by: mattBrzezinski <[email protected]>
  • Loading branch information
3 people authored Mar 31, 2023
2 parents f956a1b + fc6cdda commit 7b09ec0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "AWS"
uuid = "fbe9abb3-538b-5e4e-ba9e-bc94f4f92ebc"
license = "MIT"
version = "1.84.0"
version = "1.84.1"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
17 changes: 10 additions & 7 deletions src/AWSExceptions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,18 @@ function AWSException(e::HTTP.StatusError, body::AbstractString)
end
end

# There are times when Errors or Error are returned back
info = get(info, "Errors", info)
info = get(info, "Error", info)
# Sometimes info is a string, in which case there is nothing else to do
if info isa AbstractDict
# There are times when Errors or Error are returned back
info = get(info, "Errors", info)
info = get(info, "Error", info)

code = get(info, "Code", code)
code = get(info, "Code", code)

# There are also times when the response back is (M|m)essage
message = get(info, "Message", message)
message = get(info, "message", message)
# There are also times when the response back is (M|m)essage
message = get(info, "Message", message)
message = get(info, "message", message)
end

streamed_body = !HTTP.isbytes(e.response.body) ? body : nothing

Expand Down
28 changes: 28 additions & 0 deletions test/AWSExceptions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,32 @@
_test_exception(ex, expected, "$msg")
@test ex.info["__type"] == expected["code"]
end

@testset "JSON requests can have invalid bodies" begin
expected = Dict(
"code" => "400",
"message" => "AWSException",
"headers" => ["Content-Type" => "application/json"],
"status_code" => 400,
)

expected["body"] = IOBuffer()
expected["streamed_body"] = "\"foo\""

# This does not actually send a request, just creates the object to test with
req = HTTP.Request("GET", "https://aws.com", expected["headers"], expected["body"])
resp = HTTP.Response(
expected["status_code"], expected["headers"]; body=expected["body"], request=req
)
status_error = AWS.statuserror(expected["status_code"], resp)
ex = AWSException(status_error, expected["streamed_body"])

@test ex.code == expected["code"]
@test ex.info == "foo" # nothing better we can do than just forward the invalid body
@test ex.message == expected["message"]
@test ex.cause.response.body == expected["body"]
@test ex.cause.status == expected["status_code"]
@test ex.cause.response.headers == expected["headers"]
@test ex.streamed_body == expected["streamed_body"]
end
end

2 comments on commit 7b09ec0

@mattBrzezinski
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/80759

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.84.1 -m "<description of version>" 7b09ec01ab2a06471ce7a0101535c60472659634
git push origin v1.84.1

Please sign in to comment.