Skip to content

Commit 7b09ec0

Browse files
bors[bot]OctogonapusmattBrzezinski
authored
Merge #613
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]>
2 parents f956a1b + fc6cdda commit 7b09ec0

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "AWS"
22
uuid = "fbe9abb3-538b-5e4e-ba9e-bc94f4f92ebc"
33
license = "MIT"
4-
version = "1.84.0"
4+
version = "1.84.1"
55

66
[deps]
77
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

src/AWSExceptions.jl

+10-7
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,18 @@ function AWSException(e::HTTP.StatusError, body::AbstractString)
109109
end
110110
end
111111

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

116-
code = get(info, "Code", code)
118+
code = get(info, "Code", code)
117119

118-
# There are also times when the response back is (M|m)essage
119-
message = get(info, "Message", message)
120-
message = get(info, "message", message)
120+
# There are also times when the response back is (M|m)essage
121+
message = get(info, "Message", message)
122+
message = get(info, "message", message)
123+
end
121124

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

test/AWSExceptions.jl

+28
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,32 @@
9393
_test_exception(ex, expected, "$msg")
9494
@test ex.info["__type"] == expected["code"]
9595
end
96+
97+
@testset "JSON requests can have invalid bodies" begin
98+
expected = Dict(
99+
"code" => "400",
100+
"message" => "AWSException",
101+
"headers" => ["Content-Type" => "application/json"],
102+
"status_code" => 400,
103+
)
104+
105+
expected["body"] = IOBuffer()
106+
expected["streamed_body"] = "\"foo\""
107+
108+
# This does not actually send a request, just creates the object to test with
109+
req = HTTP.Request("GET", "https://aws.com", expected["headers"], expected["body"])
110+
resp = HTTP.Response(
111+
expected["status_code"], expected["headers"]; body=expected["body"], request=req
112+
)
113+
status_error = AWS.statuserror(expected["status_code"], resp)
114+
ex = AWSException(status_error, expected["streamed_body"])
115+
116+
@test ex.code == expected["code"]
117+
@test ex.info == "foo" # nothing better we can do than just forward the invalid body
118+
@test ex.message == expected["message"]
119+
@test ex.cause.response.body == expected["body"]
120+
@test ex.cause.status == expected["status_code"]
121+
@test ex.cause.response.headers == expected["headers"]
122+
@test ex.streamed_body == expected["streamed_body"]
123+
end
96124
end

0 commit comments

Comments
 (0)