[racket] Handling errors from in-directory - and handling errors more generally

From: Peter Kiggins (peterk at palinurus.org)
Date: Tue Oct 12 09:48:49 EDT 2010

[I am completely new to both Scheme and racket, so if my questions are
easily answered by reading some documentation, please just point me at
it.]

Looking at traversal of a file tree led me to in-directory.  The
problem is that as soon as it hits a directory it cannot search
(permissions, whatever...) it drops out with an exception.  For
example:

(for ([e (in-directory "/Users/lucas")])
  (printf "~a\n" e))
/Users/lucas/.CFUserTextEncoding
/Users/lucas/.DS_Store
/Users/lucas/Desktop
directory-list: could not open "/Users/lucas/Desktop" (Permission
denied; errno=13)

I'd like to be able to catch the error, change the output, and
continue pulling names:

(for ([e (safe-in-directory "/Users/lucas")])
   (printf "~a\n" e))
/Users/lucas/.CFUserTextEncoding
/Users/lucas/.DS_Store
/Users/lucas/Desktop[permission denied]
/Users/lucas/morefiles
...
...

How is in-directory meant to be used, so that it re-starts after an
error?  I can catch the error by wrapping the code in with-handlers,
but I can't see ahow to re-start after the catch.

Or is there a different and preferred way of approaching re-starts in
this sort of situation?

In general, what is idiomatic for catching exceptions, re-trying after
an exception, running finalise code?  Is there an equivalent to Clisp
restarts?

To be clear, I'm not really interested in alternative methods of
traversing the file tree - I am interested in making in-directory
useful, and understanding the exception/restart model in racket.

Many thanks

Peter


Posted on the users mailing list.