#46 handling host-info errors

open
run-time (53)
5
2001-05-29
2001-05-29
No

From: brownb@ja.gs.com (Robert E. Brown)
To: scsh-news@zurich.ai.mit.edu
Subject: handling host-info errors
Date: 07 Feb 2000 12:29:35 -0500

I'm using scsh-0.5.2 on Linux.

When my scsh script calls the function socket-connect
with a bad host name,
I get the following error

Error: name->host-info: non-zero herrno ~s ~s
1
#f

since socket-connect calls host-info, which calls name-
>host-info, which
indicates a problem by calling the error function.

I'd like to handle this condition and print a
reasonable error message that
the users of my script will understand. I want some
syntax like
with-errno-handler, but it should handle herror/host-
not-found. I have not
yet located what I need in the manual. Am I looking
in the wrong spot?
What syntax should I be using?

bob

Discussion

  • Logged In: YES
    user_id=27364

    From: Eric Marsden <emarsden@mail.dotcom.fr>
    To: scsh-news@zurich.ai.mit.edu
    Subject: Re: handling host-info errors
    Date: 07 Feb 2000 20:40:32 +0100

    >>>>> "reb" == Robert E Brown <brownb@ja.gs.com> writes:

    reb> I'd like to handle this condition and print a
    reasonable error
    reb> message that the users of my script will understand.
    I want
    reb> some syntax like with-errno-handler, but it should
    handle
    reb> herror/host-not-found.

    I suppose that ideally there should be condition types
    instead of raw
    errors for many of these things, but there would be a huge
    number of
    them. Here is one way of intercepting specific error types:

    #!/usr/local/bin/scsh \ -dm -m whnf -e main -s
    !#

    (define-structure whnf
    (export main)
    (open scheme scsh handle)
    (begin

    (define (with-host-not-found* thunk)
    (call-with-current-continuation
    (lambda (k)
    (with-handler
    (lambda (condition next)
    (cond ((string-match "^name->host-info" (cadr
    condition))
    (display "No such host")
    (newline)
    (k))
    (else (next))))
    thunk))))

    (define-syntax with-host-not-found
    (syntax-rules ()
    ((with-host-not-found ?body ...)
    (with-host-not-found* (lambda () ?body ...)))))

    (define (main args)
    (with-host-not-found
    (host-info "foo.bar.com")))))

    --
    Eric Marsden

     
  • Logged In: YES
    user_id=27364

    From: brownb@ja.gs.com (Robert E. Brown)
    To: scsh-news@zurich.ai.mit.edu
    Subject: Re: handling host-info errors
    Date: 07 Feb 2000 17:51:14 -0500

    I was afraid I'd have to do something like this.

    One interesting thing I noticed is that the codes for host
    info errors are
    already defined and documented in scsh:

    herror/host-not-found
    herror/try-again
    herror/no-recovery
    herror/no-data
    herror/no-address

    I think it's fair for scsh routines to call "error" when
    they're passed
    illegal arguments. However, when I call socket-connect and
    pass in a
    perfectly reasonable host name string that DNS can't
    resolve, I should get
    an exception that's as easy to catch as the system call
    errors I might get.

    Thanks for your help!

    bob

    ====================