Skip to content

Commit

Permalink
way less spammy errors on bad requests. (#60)
Browse files Browse the repository at this point in the history
* way less spammy errors on bad requests.

* fix: better catch logic
fix: added tests

* invert range

Co-authored-by: Júlio Hoffimann <julio.hoffimann@gmail.com>

* Minor adjustments

---------

Co-authored-by: Júlio Hoffimann <julio.hoffimann@gmail.com>
  • Loading branch information
ghyatzo and juliohm authored Jan 16, 2025
1 parent 4b933de commit 5b5b932
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/CDSAPI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,28 @@ function retrieve(name, params, filename; wait=1.0)
end
end

response = HTTP.request("POST",
creds["url"] * "/retrieve/v1/processes/$name/execute",
["PRIVATE-TOKEN" => creds["key"]],
body=JSON.json(Dict("inputs" => params))
)
try
response = HTTP.request("POST",
creds["url"] * "/retrieve/v1/processes/$name/execute",
["PRIVATE-TOKEN" => creds["key"]],
body=JSON.json(Dict("inputs" => params))
)
catch e
if e isa HTTP.StatusError
if e.status == 404
throw(ArgumentError("""
The requested dataset $name was not found.
"""))
elseif 400 e.status < 500
throw(ArgumentError("""
The request is in a bad format:
$params
"""))
end
end
throw(e)
end

body = JSON.parse(String(response.body))
data = Dict("status" => "queued")

Expand Down
13 changes: 13 additions & 0 deletions test/retrieve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,17 @@
rm(filepath)
rm(ewq_file)
end

@testset "Bad requests errors are catched" begin
goodname = "reanalysis-era5-single-levels"
badname = "bad-dataset"
badrequest = """{
"this": "is",
"a": "bad",
"re": ["quest"]
}"""

@test_throws ArgumentError CDSAPI.retrieve(goodname, badrequest, "unreachable")
@test_throws ArgumentError CDSAPI.retrieve(badname, badrequest, "unreachable")
end
end

0 comments on commit 5b5b932

Please sign in to comment.