Skip to content

Commit 9b46982

Browse files
committed
use OpenMLAPIError
1 parent 3ce9148 commit 9b46982

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

src/data.jl

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,36 @@
11
const API_URL = "https://www.openml.org/api/v1/json"
22

3-
# Data API
4-
# The structures are based on these descriptions
5-
# https://github.com/openml/OpenML/tree/master/openml_OS/views/pages/api_new/v1/xsd
6-
# https://www.openml.org/api_docs#!/data/get_data_id
3+
struct OpenMLAPIError <: Exception
4+
msg::String
5+
end
6+
function Base.showerror(io::IO, e::OpenMLAPIError)
7+
print(io, e.msg)
8+
end
79

810

9-
function get(query; extra_error_message = "")
11+
# Data API. See REST API on https://www.openml.org/apis
12+
function get(query)
1013
try
1114
r = HTTP.request("GET", string(API_URL, query))
1215
return JSON.parse(String(r.body))
1316
catch e
1417
if isa(e, HTTP.StatusError) && e.status == 412
15-
try
16-
err = JSON.parse(String(e.response.body))["error"]
17-
msg = err["message"]
18-
code = err["code"]
19-
additional_msg = haskey(err, "additional_message") ? err["additional_message"] : ""
20-
@error msg * " " * additional_msg * "(error code $code)"
18+
error_string = String(e.response.body)
19+
err = try
20+
JSON.parse(error_string)["error"]
2121
catch
22-
@error e
22+
@error(error_string)
23+
throw(OpenMLAPIError("Malformed query \"$query\"."))
2324
end
24-
extra_error_message != "" && println(extra_error_message)
25+
msg = err["message"]
26+
code = err["code"]
27+
additional_msg = haskey(err, "additional_message") ? err["additional_message"] : ""
28+
if code == "111"
29+
additional_msg *= "Check if there is a dataset with id $(last(split(query, '/'))).\nSee e.g. OpenML.list_datasets(). "
30+
end
31+
throw(OpenMLAPIError(msg * ". " * additional_msg * "(error code $code)"))
2532
else
26-
throw(e)
33+
rethrow()
2734
end
2835
end
2936
return nothing
@@ -35,10 +42,7 @@ end
3542
Returns information about a dataset. The information includes the name,
3643
information about the creator, URL to download it and more.
3744
"""
38-
function load_Dataset_Description(id::Int)
39-
get("/data/$id",
40-
extra_error_message = "Check if there is a dataset with id $id.\nSee e.g. OpenML.list_datasets()\n")
41-
end
45+
load_Dataset_Description(id::Int) = get("/data/$id")
4246

4347
"""
4448
OpenML.load(id; maxbytes = nothing)

0 commit comments

Comments
 (0)