Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling WebExceptions properly?

I have the following F# program that retrieves a webpage from the internet:

open System.Net

[<EntryPoint>]
let main argv = 
    let mutable pageData : byte[] = [| |]
    let fullURI = "http://www.badaddress.xyz"
    let wc = new WebClient()
    try
        pageData  <- wc.DownloadData(fullURI)
        ()
    with
    | :? System.Net.WebException as err -> printfn "Web error: \n%s" err.Message
    |  exn -> printfn "Unknown exception:\n%s" exn.Message

    0 // return an integer exit code

This works fine if the URI is valid and the machine has an internet connection and the web server responds properly etc. In an ideal functional programming world the results of a function would not depend on external variables not passed as arguments (side effects).

What I would like to know is what is the appropriate F# design pattern to deal with operations which might require the function to deal with recoverable external errors. For example if the website is down one might want to wait 5 minutes and try again. Should parameters like how many times to retry and delays between retries be passed explicitly or is it OK to embed these variables in the function?

like image 765
JonnyBoats Avatar asked Jun 29 '13 15:06

JonnyBoats


People also ask

How do you handle exception handling?

The try-catch is the simplest method of handling exceptions. Put the code you want to run in the try block, and any Java exceptions that the code throws are caught by one or more catch blocks. This method will catch any type of Java exceptions that get thrown. This is the simplest mechanism for handling exceptions.

What is exceptions handling with example?

Examples include a user providing abnormal input, a file system error being encountered when trying to read or write a file, or a program attempting to divide by zero. Exception handling attempts to gracefully handle these situations so that a program (or worse, an entire system) does not crash.


1 Answers

In F#, when you want to handle recoverable errors you almost universally want to use the option or the Choice<_,_> type. In practice the only difference between them is that Choice allows you to return some information about the error while option does not. In other words, option is best when it doesn't matter how or why something failed (only that it did fail); Choice<_,_> is used when having information about how or why something failed is important. For example, you might want to write the error information to a log; or perhaps you want to handle an error situation differently based on why something failed -- a great use case for this is providing accurate error messages to help users diagnose a problem.

With that in mind, here's how I'd refactor your code to handle failures in a clean, functional style:

open System
open System.Net

/// Retrieves the content at the given URI.
let retrievePage (client : WebClient) (uri : Uri) =
    // Preconditions
    checkNonNull "uri" uri
    if not <| uri.IsAbsoluteUri then
        invalidArg "uri" "The URI must be an absolute URI."

    try
        // If the data is retrieved successfully, return it.
        client.DownloadData uri
        |> Choice1Of2
    with
    | :? System.Net.WebException as webExn ->
        // Return the URI and WebException so they can be used to diagnose the problem.
        Choice2Of2 (uri, webExn)
    | _ ->
        // Reraise any other exceptions -- we don't want to handle them here.
       reraise ()

/// Retrieves the content at the given URI.
/// If a WebException is raised when retrieving the content, the request
/// will be retried up to a specified number of times.
let rec retrievePageRetry (retryWaitTime : TimeSpan) remainingRetries (client : WebClient) (uri : Uri) =
    // Preconditions
    checkNonNull "uri" uri
    if not <| uri.IsAbsoluteUri then
        invalidArg "uri" "The URI must be an absolute URI."
    elif remainingRetries = 0u then
        invalidArg "remainingRetries" "The number of retries must be greater than zero (0)."

    // Try to retrieve the page.
    match retrievePage client uri with
    | Choice1Of2 _ as result ->
        // Successfully retrieved the page. Return the result.
        result
    | Choice2Of2 _ as error ->
        // Decrement the number of retries.
        let retries = remainingRetries - 1u

        // If there are no retries left, return the error along with the URI
        // for diagnostic purposes; otherwise, wait a bit and try again.
        if retries = 0u then error
        else
            // NOTE : If this is modified to use 'async', you MUST
            // change this to use 'Async.Sleep' here instead!
            System.Threading.Thread.Sleep retryWaitTime

            // Try retrieving the page again.
            retrievePageRetry retryWaitTime retries client uri

[<EntryPoint>]
let main argv =
    /// WebClient used for retrieving content.
    use wc = new WebClient ()

    /// The amount of time to wait before re-attempting to fetch a page.
    let retryWaitTime = TimeSpan.FromSeconds 2.0

    /// The maximum number of times we'll try to fetch each page.
    let maxPageRetries = 3u

    /// The URI to fetch.
    let fullURI = Uri ("http://www.badaddress.xyz", UriKind.Absolute)

    // Fetch the page data.
    match retrievePageRetry retryWaitTime maxPageRetries wc fullURI with
    | Choice1Of2 pageData ->
        printfn "Retrieved %u bytes from: %O" (Array.length pageData) fullURI

        0   // Success
    | Choice2Of2 (uri, error) ->
        printfn "Unable to retrieve the content from: %O" uri
        printfn "HTTP Status: (%i) %O" (int error.Status) error.Status
        printfn "Message: %s" error.Message

        1   // Failure

Basically, I split your code out into two functions, plus the original main:

  • One function that attempts to retrieve the content from a specified URI.
  • One function containing the logic for retrying attempts; this 'wraps' the first function which performs the actual requests.
  • The original main function now only handles 'settings' (which you could easily pull from an app.config or web.config) and printing the final results. In other words, it's oblivious to the retrying logic -- you could modify the single line of code with the match statement and use the non-retrying request function instead if you wanted.

If you want to pull content from multiple URIs AND wait for a significant amount of time (e.g., 5 minutes) between retries, you should modify the retrying logic to use a priority queue or something instead of using Thread.Sleep or Async.Sleep.

Shameless plug: my ExtCore library contains some things to make your life significantly easier when building something like this, especially if you want to make it all asynchronous. Most importantly, it provides an asyncChoice workflow and collections functions designed to work with it.

As for your question about passing in parameters (like the retry timeout and number of retries) -- I don't think there's a hard-and-fast rule for deciding whether to pass them in or hard-code them within the function. In most cases, I prefer to pass them in, though if you have more than a few parameters to pass in, you're better off creating a record to hold them all and passing that instead. Another approach I've used is to make the parameters option values, where the defaults are pulled from a configuration file (though you'll want to pull them from the file once and assign them to some private field to avoid re-parsing the configuration file each time your function is called); this makes it easy to modify the default values you've used in your code, but also gives you the flexibility of overriding them when necessary.

like image 198
Jack P. Avatar answered Nov 15 '22 08:11

Jack P.