PageRenderTime 33ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/haskell-extras/System/IO/Error.hs

https://github.com/akakcolin/ajhc
Haskell | 100 lines | 48 code | 18 blank | 34 comment | 1 complexity | dfeb206fbdbbf323a38ed0513448150c MD5 | raw file
Possible License(s): GPL-2.0
  1. module System.IO.Error (
  2. IOError(), userError, mkIOError, annotateIOError, isAlreadyExistsError,
  3. isDoesNotExistError, isAlreadyInUseError, isFullError, isEOFError,
  4. isIllegalOperation, isPermissionError, isUserError, ioeGetErrorString,
  5. ioeGetHandle, ioeGetFileName, IOErrorType(), alreadyExistsErrorType,
  6. doesNotExistErrorType, alreadyInUseErrorType, fullErrorType,
  7. eofErrorType, illegalOperationErrorType, permissionErrorType,
  8. userErrorType, ioError, catch, try
  9. ) where
  10. import Jhc.Type.Handle
  11. -- | The construct 'try' @comp@ exposes IO errors which occur within a
  12. -- computation, and which are not fully handled.
  13. --
  14. -- Non-I\/O exceptions are not caught by this variant; to catch all
  15. -- exceptions, use 'Control.Exception.try' from "Control.Exception".
  16. try :: IO a -> IO (Either IOError a)
  17. try f = catch (do r <- f
  18. return (Right r))
  19. (return . Left)
  20. -- -----------------------------------------------------------------------------
  21. -- Constructing an IOError
  22. -- | Construct an 'IOError' of the given type where the second argument
  23. -- describes the error location and the third and fourth argument
  24. -- contain the file handle and file path of the file involved in the
  25. -- error if applicable.
  26. mkIOError :: IOErrorType -> String -> Maybe Handle -> Maybe FilePath -> IOError
  27. mkIOError = IOError
  28. -- TODO(john): fix
  29. annotateIOError :: IOError -> String -> Maybe Handle -> Maybe FilePath -> IOError
  30. annotateIOError ioe _s _mh _mfp = ioe
  31. check :: IOErrorType -> IOError -> Bool
  32. check errorType ioe = ioeGetErrorType ioe == errorType
  33. -- | An error indicating that an 'IO' operation failed because
  34. -- one of its arguments already exists.
  35. isAlreadyExistsError :: IOError -> Bool
  36. isAlreadyExistsError = check AlreadyExists
  37. -- | An error indicating that an 'IO' operation failed because
  38. -- one of its arguments does not exist.
  39. isDoesNotExistError :: IOError -> Bool
  40. isDoesNotExistError = check DoesNotExist
  41. -- | An error indicating that an 'IO' operation failed because
  42. -- one of its arguments is a single-use resource, which is already
  43. -- being used (for example, opening the same file twice for writing
  44. -- might give this error).
  45. isAlreadyInUseError :: IOError -> Bool
  46. isAlreadyInUseError = check AlreadyInUse
  47. -- | An error indicating that an 'IO' operation failed because
  48. -- the device is full.
  49. isFullError :: IOError -> Bool
  50. isFullError = check Full
  51. -- | An error indicating that an 'IO' operation failed because
  52. -- the end of file has been reached.
  53. isEOFError :: IOError -> Bool
  54. isEOFError = check EOF
  55. -- | An error indicating that an 'IO' operation failed because
  56. -- the operation was not possible.
  57. -- Any computation which returns an 'IO' result may fail with
  58. -- 'isIllegalOperation'. In some cases, an implementation will not be
  59. -- able to distinguish between the possible error causes. In this case
  60. -- it should fail with 'isIllegalOperation'.
  61. isIllegalOperation :: IOError -> Bool
  62. isIllegalOperation = check IllegalOperation
  63. -- | An error indicating that an 'IO' operation failed because
  64. -- the user does not have sufficient operating system privilege
  65. -- to perform that operation.
  66. isPermissionError :: IOError -> Bool
  67. isPermissionError = check Permission
  68. -- | A programmer-defined error value constructed using 'userError'.
  69. isUserError :: IOError -> Bool
  70. isUserError = check User
  71. deriving instance Eq IOErrorType
  72. deriving instance Ord IOErrorType
  73. alreadyExistsErrorType = AlreadyExists
  74. doesNotExistErrorType = DoesNotExist
  75. alreadyInUseErrorType = AlreadyInUse
  76. fullErrorType = Full
  77. eofErrorType = EOF
  78. illegalOperationErrorType = IllegalOperation
  79. permissionErrorType = Permission
  80. userErrorType = User
  81. instance Show IOError where
  82. showsPrec _ s = (ioeGetErrorString s ++)