Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Pass detailed error message along to user if possible (closes #7) #12

Merged
merged 2 commits into from
Jan 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Imports:
crul (>= 0.5.2),
jsonlite (>= 1.5),
data.table,
tibble
tibble,
httpcode (>= 0.2.0)
Suggests:
testthat,
vcr (>= 0.2.0.9114)
Expand Down
30 changes: 29 additions & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ma_HTTP <- function(path, args, key, method = "GET", body = list(),
GET = cli$get(path, query = args, ...),
POST = cli$post(path, query = args, body = body, encode = encode, ...)
)
res$raise_for_status()
raise_for_status2(res)
txt <- res$parse("UTF-8")
jsonlite::fromJSON(txt, flatten = TRUE)
}
Expand Down Expand Up @@ -49,3 +49,31 @@ assert <- function(x, y) {
}
}
}

raise_for_status2 <- function(res) {
if (res$status_code >= 300) {
er_lst <- tryCatch(
parse_error_msg(res),
error = function(e) httpcode::http_code(res$status_code)
)
er_msg <- sprintf(
"%s (HTTP %s)\nExplanation: %s",
res$status_code, er_lst$message, er_lst$explanation
)
stop(er_msg, call. = FALSE)
}
}

parse_error_msg <- function(res) {
txt <- res$parse("UTF-8")
er_vec <- unlist(jsonlite::fromJSON(txt))
# er_vec names used to be capitilized (e.g., "Error.Code", not "error.code").
# convert to lowercase just in case API goes back to using caps.
names(er_vec) <- tolower(names(er_vec))
list(
# the error.code element refers to a short error message, not the HTTP
# status code
message = er_vec[["error.code"]],
explanation = er_vec[["error.message"]]
)
}
30 changes: 30 additions & 0 deletions tests/fixtures/ma_error.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
http_interactions:
- request:
method: get
uri: https://api.labs.cognitive.microsoft.com/academic/v1.0/evaluate?expr=hi-there&count=10&offset=0&attributes=Id%2CAA.AuN%2CJ.JN%2CTi%2CY%2CE%2CCC&model=latest
body:
encoding: ''
string: ''
headers:
User-Agent: libcurl/7.54.0 r-curl/3.2 crul/0.6.0
Accept-Encoding: gzip, deflate
Accept: application/json, text/xml, application/xml, */*
Ocp-Apim-Subscription-Key: <<<microdemic_api_key>>>
response:
status:
status_code: '400'
message: Bad Request
explanation: Bad request syntax or unsupported method
headers:
status: HTTP/1.1 400 Bad Request
content-length: '100'
content-type: application/json
access-control-allow-origin: '*'
request-context: appId=cid-v1:7cd06f0e-6d73-46a5-9c10-4ebe681d0156
x-powered-by: ASP.NET
date: Sat, 12 Jan 2019 03:18:16 GMT
body:
encoding: ASCII-8BIT
string: eyJFcnJvciI6eyJDb2RlIjoiQmFkIEFyZ3VtZW50IiwiTWVzc2FnZSI6IkludmFsaWQgcXVlcnkgZXhwcmVzc2lvblxyXG5QYXJhbWV0ZXIgbmFtZTogZXhwcmVzc2lvbiJ9fQ==
recorded_at: 2019-01-12 03:18:16 GMT
recorded_with: vcr/0.1.0, webmockr/0.2.6, crul/0.6.0
12 changes: 12 additions & 0 deletions tests/testthat/test-error-msgs.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
context("ma_error")

test_that("Detailed error messages are provided when possible", {
skip_on_cran()

vcr::use_cassette("ma_error", {
expect_error(
ma_search(query = "hi-there"),
"query"
)
}, preserve_exact_body_bytes = TRUE)
})