[Haskell-cafe] error handling (esp. IO/exceptions)

Lennart Augustsson lennart at augustsson.net
Tue Apr 14 16:13:24 EDT 2009


There's no way to make all your tests atomically, e.g, between the
test that p exists and executing p, some other process could removce
p.
So the right way to do this (like opening a file), is to try executing
it and let the OS tell you if it failed.

On Tue, Apr 14, 2009 at 9:29 PM,  <brian at lorf.org> wrote:
> I'm finding it hard to write robust code that isn't really ugly. Suppose
> I want to write
>
>> execute :: FilePath -> [String] -> IO (Either ExecuteError ExitCode)
>
> where 'ExecuteError' is a data type representing all the ways 'execute'
> could fail and that 'execute p args' is supposed to
>
> * ensure p exists
> * get p's permissions
> * ensure p is readable
> * ensure p is executable
> * execute p and return its exit code
>
> So 'ExecuteError' would have constructors corresponding to these ways to
> fail:
> * could not determine whether p exists
> * p does not exist
> * could not get p's permissions
> * p is not readable
> * p is not executable
> * could not execute p for some other reason
>
> So if I start to code it, I 'tryJust' 'doesFileExist'. If an exception
> is thrown, I convert it to Left $AppropriateExecuteError. If not, we
> know whether p exists. If it doesn't, convert it to Left. Otherwise,
> 'tryJust' 'getPermissions', etc. It's starting to staircase.
>
> I'm needing it to be easier to stop my computation and return specific
> things when exceptions are thrown or when certain requirements aren't
> met. Thanks for any help.
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list