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

brian at lorf.org brian at lorf.org
Tue Apr 14 15:29:06 EDT 2009


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.


More information about the Haskell-Cafe mailing list