How to handle isAlreadyExistsError

I was trying to call createDirectory and catching isAlreadyExistError in order to do nothing in this case. I realized then that there is createDirectoryIfMissing and use it. But still I’m puzzled by the documentation for createDirectory, which says that:

`isAlreadyExistsError` The operand refers to a directory that already exists. [EEXIST]

Which leads me to believe that I can catch an IOException and do an if isAlreadyExistsError e .... But that doesn’t seem possible, because the predicate is in a private module.

How is this supposed to be used?

Ah, the documentation points to a strange location. I think you can just use the one from System.IO.Error.

It’s definitely System.IO.Error. The directory package Internal.Prelude re-exports definitions and under haddock that makes it seem like the function is definied within the directory package. I found this behaviour useful in a personal project, but I can see it very easily becoming confusing.

Confusing indeed. I click on ìsAlreadyExistError` and get to a (for me) relatively random module and then have to Hoogle search my way to the right function?

Marking directory's internal prelude not-home for haddock should make the link target System.IO.Error.

Since I don’t know how to do that, I opened an issue:

Let’s see if somebody fixes it.

1 Like

If you’d like to contribute directly on that change. This haddock manual section should be of interest Documentation and Markup — Haddock 1.0 documentation

Would require a patch with the following options pragma included in that module source code {-# OPTIONS_HADDOCK not-home #-}

1 Like

I use something like this:

hideError :: (MonadIO m, MonadCatch m) => IOErrorType -> m () -> m ()
hideError err = handleIO (\e -> if err == ioeGetErrorType e then pure () else liftIO . ioError $ e)

hideErrorDef :: (MonadIO m, MonadCatch m) => IOErrorType -> a -> m a -> m a
hideErrorDef err def =
  handleIO (\e -> if err == ioeGetErrorType e then pure def else liftIO $ ioError e)

(can also expand it to hideErrors :: [IOErrorType] -> ...)

Then:

  hideError alreadyExistsErrorType $ createDirectory "foo"