Skip to content

Commit

Permalink
GBIF throws 404 (not 410) for deleted resources (#156)
Browse files Browse the repository at this point in the history
* 🀒 GBIF throws 404 (not 410) for deleted resources

* πŸ› throws an error when the occurrence is not available

* βœ… 404 error on single occurrence is handled
  • Loading branch information
tpoisot committed Feb 18, 2023
1 parent 8f45557 commit 75d8063
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion GBIF/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "GBIF"
uuid = "ee291a33-5a6c-5552-a3c8-0f29a1181037"
authors = ["TimothΓ©e Poisot <timothee.poisot@umontreal.ca>"]
version = "0.4.0"
version = "0.4.1"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
29 changes: 22 additions & 7 deletions GBIF/src/occurrence.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,30 @@ function pairs_to_querystring(query::Pair...)
end

"""
occurrence(key::Union{String, Integer})
occurrence(key::String)
Returns a GBIF occurrence identified by a key. The key can be given as a string or as an integer.
Returns a GBIF occurrence identified by a key. The key can be given as a string
or as an integer (there is a second method for integer keys). In case the status
of the HTTP request is anything other than 200 (success), this function *will*
throw an error.
"""
function occurrence(key::Union{String, Integer})
occ_url = gbifurl * "occurrence/" * string(key)
occ_key_req = HTTP.get(occ_url)
result = JSON.parse(String(occ_key_req.body))
return GBIFRecord(result)
function occurrence(key::String)::GBIFRecord
occ_url = gbifurl * "occurrence/" * key
try
occ_key_req = HTTP.get(occ_url)
result = JSON.parse(String(occ_key_req.body))
return GBIFRecord(result)
catch err
if err isa HTTP.Exceptions.StatusError
throw("Occurrence $(key) (at $(occ_url)) cannot be accessed - error code: $(err.status)")
else
throw("Occurrence $(key) cannot be accessed")
end
end
end

function occurrence(key::Integer)::GBIFRecord
return occurrence(string(key))
end

function _internal_occurrences_getter(query::Pair...)
Expand Down
9 changes: 7 additions & 2 deletions GBIF/test/occurrence.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ module TestOccurrence
using GBIF
using Test

k = 1258202889
# This occurrence exists
k = 3986160931
o = occurrence(k)
@test typeof(o) == GBIFRecord
@test o.key == k

# Piece of shit uncorrectly formatted occurence
# This occurrence has been deleted, so this needs some wrapping
k = 1258202889
@test_throws "cannot be accessed - error code" occurrence(k)

# This occurence is incorrectly formatted for some reason
k = 1039645472
o = occurrence(k)
@test typeof(o) == GBIFRecord
Expand Down

0 comments on commit 75d8063

Please sign in to comment.