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

Conversation

crew102
Copy link
Collaborator

@crew102 crew102 commented Jan 15, 2019

This PR attempts to parse the error message contained in the body of the API's response. If the message can be parsed, it's passed along to the user in a detailed error message. Otherwise, a generic error message corresponding to the HTTP status code is provided. Closes #7.

Examples

This is what error messages look like in the current version of microdemic (note, I have fauxpas installed, so raise_for_status() is calling fauxpas::http() for error handling - https://github.com/ropensci/crul/blob/dba96adc1685aa237b9465074adcf2e00f675ac5/R/response.R#L149-L154).

library(microdemic)

ma_evaluate(
  query = "bad-query",
  key = Sys.getenv("MICROSOFT_ACADEMIC_KEY"),
)
#> Error: Bad Request (HTTP 400)

ma_evaluate(
  query = "Y=[2010, 2012)",
  key = Sys.getenv("MICROSOFT_ACADEMIC_KEY"),
  offset = "bad-offset"
)
#> Error: Bad Request (HTTP 400)

ma_evaluate(
  query = "Y=[2010, 2012)",
  key = "bad_key"
)
#> Error: Unauthorized (HTTP 401)

Examples of error messages when using version of microdemic found in this PR:

library(microdemic)

# The detailed error message provided by the API is literally "Invalid query 
# expression\r\nParameter name: expression"
ma_evaluate(
  query = "bad-query",
  key = Sys.getenv("MICROSOFT_ACADEMIC_KEY"),
)
#> Error: 400 (HTTP Bad Argument)
#> Explanation: Invalid query expression
#> Parameter name: expression

ma_evaluate(
  query = "Y=[2010, 2012)",
  key = Sys.getenv("MICROSOFT_ACADEMIC_KEY"),
  offset = "bad-offset"
)
#> Error: 400 (HTTP Bad Argument)
#> Explanation: offset should be an integer

ma_evaluate(
  query = "Y=[2010, 2012)",
  key = "bad_key"
)
#> Error: 401 (HTTP Unspecified)
#> Explanation: Access denied due to invalid subscription key. Make sure you are subscribed to an API you are trying to call and provide the right key.

Examples of error messages if parsing of error message fails in parse_error_msg(). This is also what error messages look like in the current version of microdemic if the user doesn't have fauxpas installed.

library(microdemic)

ma_evaluate(
  query = "bad-query",
  key = Sys.getenv("MICROSOFT_ACADEMIC_KEY"),
)
#> Error: 400 (HTTP Bad Request)
#> Explanation: Bad request syntax or unsupported method

ma_evaluate(
  query = "Y=[2010, 2012)",
  key = Sys.getenv("MICROSOFT_ACADEMIC_KEY"),
  offset = "bad-offset"
)
#> Error: 400 (HTTP Bad Request
#> Explanation: Bad request syntax or unsupported method

ma_evaluate(
  query = "Y=[2010, 2012)",
  key = "bad_key"
)
#> Error: 401 (HTTP Unauthorized)
#> Explanation: No permission -- see authorization schemes

@codecov-io
Copy link

codecov-io commented Jan 15, 2019

Codecov Report

Merging #12 into master will increase coverage by 4.32%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #12      +/-   ##
==========================================
+ Coverage   73.56%   77.88%   +4.32%     
==========================================
  Files           8        8              
  Lines          87      104      +17     
==========================================
+ Hits           64       81      +17     
  Misses         23       23
Impacted Files Coverage Δ
R/zzz.R 89.58% <100%> (+5.71%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 39ea083...d9121ea. Read the comment docs.

@sckott
Copy link
Contributor

sckott commented Jan 16, 2019

thanks, having a look

@sckott sckott added this to the v0.5 milestone Jan 16, 2019
@sckott
Copy link
Contributor

sckott commented Jan 16, 2019

LGTM, thanks very much

@sckott sckott merged commit c8aef88 into ropensci-archive:master Jan 16, 2019
@sckott
Copy link
Contributor

sckott commented Jan 16, 2019

@crew102 want to be an author?

@crew102
Copy link
Collaborator Author

crew102 commented Jan 16, 2019

Sure, that would be nice, thanks. How about I start off as a contributor and if I work more on the package in the future than we can bump to author?

@crew102 crew102 mentioned this pull request Jan 16, 2019
@sckott sckott mentioned this pull request Jan 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants