Skip to contents

Handle http request errors

Usage

vcr_last_error()

Details

How this error class is used: If record="once" we trigger this.

Users can use vcr in the context of both use_cassette() and insert_cassette()

For the former, all requests go through the call_block But for the latter, requests go through webmockr.

Where is one place where we can put UnhandledHTTPRequestError that will handle both use_cassette and insert_cassette?

Error situations where this is invoked

  • record=once AND there's a new request that doesn't match the one in the cassette on disk

    • in webmockr: if no stub found and there are recorded interactions on the cassette, and record = once, then error with UnhandledHTTPRequestError

      • but if record != once, then allow it, unless record == none

  • others?

Public fields

request

a Request object

cassette

a cassette name

Methods


Method new()

Create a new UnhandledHTTPRequestError object

Usage

Arguments

request

(Request) a Request object

Returns

A new UnhandledHTTPRequestError object


Method run()

Run unhandled request handling

Usage

UnhandledHTTPRequestError$run()

Returns

various


Method construct_message()

Construct and execute stop message for why request failed

Usage

UnhandledHTTPRequestError$construct_message()

Returns

a stop message


Method request_description()

construct request description

Usage

UnhandledHTTPRequestError$request_description()

Returns

character


Method current_matchers()

get current request matchers

Usage

UnhandledHTTPRequestError$current_matchers()

Returns

character


Method match_request_on_headers()

are headers included in current matchers?

Usage

UnhandledHTTPRequestError$match_request_on_headers()

Returns

logical


Method match_request_on_body()

is body includled in current matchers?

Usage

UnhandledHTTPRequestError$match_request_on_body()

Returns

logical


Method formatted_headers()

get request headers

Usage

UnhandledHTTPRequestError$formatted_headers()

Returns

character


Method cassettes_description()

construct description of current or lack thereof cassettes

Usage

UnhandledHTTPRequestError$cassettes_description()

Returns

character


Method cassettes_list()

cassette details

Usage

UnhandledHTTPRequestError$cassettes_list()

Returns

character


Method get_help()

get help message for non-verbose error

Usage

UnhandledHTTPRequestError$get_help()

Returns

character


Method formatted_suggestions()

make suggestions for what to do

Usage

UnhandledHTTPRequestError$formatted_suggestions()

Returns

character


Method format_bullet_point()

add bullet point to beginning of a line

Usage

UnhandledHTTPRequestError$format_bullet_point(lines, index)

Arguments

lines

(character) vector of strings

index

(integer) a number

Returns

character


Method format_foot_note()

make a foot note

Usage

UnhandledHTTPRequestError$format_foot_note(url, index)

Arguments

url

(character) a url

index

(integer) a number

Returns

character


Method suggestion_for()

get a suggestion by key

Usage

UnhandledHTTPRequestError$suggestion_for(key)

Arguments

key

(character) a character string

Returns

character


Method suggestions()

get all suggestions

Usage

UnhandledHTTPRequestError$suggestions()

Returns

list


Method no_cassette_suggestions()

get all no cassette suggestions

Usage

UnhandledHTTPRequestError$no_cassette_suggestions()

Returns

list


Method record_mode_suggestion()

get the appropriate record mode suggestion

Usage

UnhandledHTTPRequestError$record_mode_suggestion()

Returns

character


Method has_used_interaction_matching()

are there any used interactions

Usage

UnhandledHTTPRequestError$has_used_interaction_matching()

Returns

logical


Method match_requests_on_suggestion()

match requests on suggestion

Usage

UnhandledHTTPRequestError$match_requests_on_suggestion()

Returns

list


Method clone()

The objects of this class are cloneable with this method.

Usage

UnhandledHTTPRequestError$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

vcr_configure(dir = tempdir())
#> <vcr configuration>
#>   Cassette Dir: /tmp/RtmpSmwo6t
#>   Record: once
#>   Serialize with: yaml
#>   URI Parser: crul::url_parse
#>   Match Requests on: method, uri
#>   Preserve Bytes?: FALSE
#>   Logging?: FALSE
#>   ignored hosts: 
#>   ignore localhost?: FALSE
#>   Write disk path: 
cassettes()
#> $jane
#> list()
#> 
insert_cassette("turtle")
#> <vcr - Cassette> turtle
#>   Record method: once
#>   Serialize with: yaml
#>   Persist with: FileSystem
#>   Re-record interval (s): 
#>   Clean outdated interactions?: FALSE
#>   update_content_length_header: FALSE
#>   allow_playback_repeats: FALSE
#>   allow_unused_http_interactions: 
#>   exclusive: 
#>   preserve_exact_body_bytes: FALSE
request <- Request$new("post", 'https://eu.httpbin.org/post?a=5',
  "", list(foo = "bar"))

err <- UnhandledHTTPRequestError$new(request)
err$request_description()
#> [1] "POST https://eu.httpbin.org/post?a=5"
err$current_matchers()
#> [1] "method" "uri"   
err$match_request_on_headers()
#> [1] FALSE
err$match_request_on_body()
#> [1] FALSE
err$formatted_headers()
#> [1] "    foo: bar"
cat(err$formatted_headers(), "\n")
#>     foo: bar 
cat(err$cassettes_description(), "\n")
#> vcr is currently using the following cassettes:
#>   - /tmp/RtmpSmwo6t/turtle.yml
#>     - record_mode: once
#>     - match_requests_on: method, uri 
#> 
#> Under the current configuration vcr can not find a suitable HTTP interaction
#> to replay and is prevented from recording new requests. There are a few ways
#> you can deal with this:
#>  
cat(err$cassettes_list(), "\n")
#> vcr is currently using the following cassettes:
#>   - /tmp/RtmpSmwo6t/turtle.yml
#>     - record_mode: once
#>     - match_requests_on: method, uri 
err$formatted_suggestions()
#> [1] "  * If you're surprised vcr is raising this error\n    and want insight about how vcr attempted to handle the request,\n    you can use 'logging' to see more details [1].\n  * You can use the :new_episodes record mode to allow vcr to\n    record this new request to the existing cassette [2].\n  * If you want vcr to ignore this request (and others like it), you can\n    set an `ignore_request` function [3].\n\n\n[1] https://books.ropensci.org/http-testing/debugging-your-tests-that-use-vcr.html#logging-1\n[2] https://books.ropensci.org/http-testing/record-modes.html#new_episodes\n[3] https://books.ropensci.org/http-testing/vcr-configuration#config-ignore-requests\n"
cat(err$format_bullet_point('foo bar', 1), "\n")
#>   * foo bar [2]. 
err$suggestion_for("use_new_episodes")
#> $text
#> [1] "You can use the :new_episodes record mode to allow vcr to"
#> [2] "record this new request to the existing cassette"         
#> 
#> $url
#> [1] "https://books.ropensci.org/http-testing/record-modes.html#new_episodes"
#> 
err$suggestions()
#> [[1]]
#> [[1]]$text
#> [1] "If you're surprised vcr is raising this error"                  
#> [2] "and want insight about how vcr attempted to handle the request,"
#> [3] "you can use 'logging' to see more details"                      
#> 
#> [[1]]$url
#> [1] "https://books.ropensci.org/http-testing/debugging-your-tests-that-use-vcr.html#logging-1"
#> 
#> 
#> [[2]]
#> [[2]]$text
#> [1] "You can use the :new_episodes record mode to allow vcr to"
#> [2] "record this new request to the existing cassette"         
#> 
#> [[2]]$url
#> [1] "https://books.ropensci.org/http-testing/record-modes.html#new_episodes"
#> 
#> 
#> [[3]]
#> [[3]]$text
#> [1] "If you want vcr to ignore this request (and others like it), you can"
#> [2] "set an `ignore_request` function"                                    
#> 
#> [[3]]$url
#> [1] "https://books.ropensci.org/http-testing/vcr-configuration#config-ignore-requests"
#> 
#> 
err$no_cassette_suggestions()
#> [[1]]
#> [[1]]$text
#> [1] "If you're surprised vcr is raising this error"                  
#> [2] "and want insight about how vcr attempted to handle the request,"
#> [3] "you can use 'logging' to see more details"                      
#> 
#> [[1]]$url
#> [1] "https://books.ropensci.org/http-testing/debugging-your-tests-that-use-vcr.html#logging-1"
#> 
#> 
#> [[2]]
#> [[2]]$text
#> [1] "If you want vcr to record this request and play it back during future test"
#> [2] "runs, you should wrap your test (or this portion of your test) in a"       
#> [3] "`vcr::use_cassette` block"                                                 
#> 
#> [[2]]$url
#> [1] "https://books.ropensci.org/http-testing/intro"
#> 
#> 
#> [[3]]
#> [[3]]$text
#> [1] "If you only want vcr to handle requests made while a cassette is in use,"
#> [2] "configure `allow_http_connections_when_no_cassette = TRUE`. vcr will"    
#> [3] "ignore this request since it is made when there is no cassette"          
#> 
#> [[3]]$url
#> [1] "https://books.ropensci.org/http-testing/vcr-configuration#allow-http-connections-when-no-cassette"
#> 
#> 
#> [[4]]
#> [[4]]$text
#> [1] "If you want vcr to ignore this request (and others like it), you can"
#> [2] "set an `ignore_request` function"                                    
#> 
#> [[4]]$url
#> [1] "https://books.ropensci.org/http-testing/vcr-configuration#config-ignore-requests"
#> 
#> 
err$record_mode_suggestion()
#> NULL
err$has_used_interaction_matching()
#> [1] FALSE
err$match_requests_on_suggestion()
#> NULL

# err$construct_message()

# cleanup
eject_cassette("turtle")
#> Warning: Empty cassette (turtle) deleted; consider the following:
#>  - If an error occurred resolve that first, then check:
#>  - vcr only supports crul & httr; requests w/ curl, download.file, etc. are not supported
#>  - If you are using crul/httr, are you sure you made an HTTP request?
#> net connect disabled
#> <vcr - Cassette> turtle
#>   Record method: once
#>   Serialize with: yaml
#>   Persist with: FileSystem
#>   Re-record interval (s): 
#>   Clean outdated interactions?: FALSE
#>   update_content_length_header: FALSE
#>   allow_playback_repeats: FALSE
#>   allow_unused_http_interactions: 
#>   exclusive: 
#>   preserve_exact_body_bytes: FALSE
unlink(tempdir())
if (FALSE) {
# vcr_last_error()
}