I'm writing code that attempts to fetch a page from a web server, and
needs to be able to handle failures gracefully.  Specifically, if the
URL in hand is bogus, I want to be able to log that fact, and not have
my script blow up.

Being used to "try" from other languages, I can do the following:

    REBOL [ Author: "Joel Neely" ]

    gotpage?: func [ pagename  /local result ][
        either error? try [read pagename]
        [ print "Bletch!" return false ]
        [ print "Hooray!" return true  ]
    ]

which behaves as follows:

    >> system/version
    == 2.1.2.3.1
    >> gotpage? http://www.fedex.com/
    Hooray!
    == true
    >> gotpage? http://nohost.org/nopage.html
    Bletch!
    == false

Based on the REBOL Dictionary entries for try, error!, and error?, I
assumed that try would return a data value/object that I could use
subsequently.  Thinking that I'd like to save the error message for
logging, I attempted the following technique:

    REBOL [ Author: "Joel Neely" ]

    gotpage?: func [ pagename  /local result ][
        result: try [read pagename]
        either error? result
        [ print "Bletch!" return false ]
        [ print "Hooray!" return true  ]
    ]

with the following results (lines wrapped to fit email)

    >> gotpage? http://www.fedex.com/
    Hooray!
    == true
    >> gotpage? http://nohost.org/nopage.html
    ** User Error: Error.  Target url: http://nohost.org/nopage.html
       could not be retrieved.
       Server response: HTTP/1.0 503 Service Unavailable.
    ** Where: read pagename
    >>

Can error values not be stored in a variable?  How can I retain the
error message for future reference without my entire script crashing?

Thanks in advance for any hints!

-jn-

Reply via email to