-
Notifications
You must be signed in to change notification settings - Fork 730
/
Copy pathError.hs
63 lines (47 loc) · 1.68 KB
/
Error.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE GADTSyntax #-}
-- | Class of errors used in the Api.
--
module Cardano.Api.Error
( Error(..)
, throwErrorAsException
, ErrorAsException(..)
, FileError(..)
) where
import Control.Exception (Exception (..), IOException, throwIO)
import System.IO (Handle)
class Show e => Error e where
displayError :: e -> String
instance Error () where
displayError () = ""
-- | The preferred approach is to use 'Except' or 'ExceptT', but you can if
-- necessary use IO exceptions.
--
throwErrorAsException :: Error e => e -> IO a
throwErrorAsException e = throwIO (ErrorAsException e)
data ErrorAsException where
ErrorAsException :: Error e => e -> ErrorAsException
instance Show ErrorAsException where
show (ErrorAsException e) = show e
instance Exception ErrorAsException where
displayException (ErrorAsException e) = displayError e
data FileError e = FileError FilePath e
| FileErrorTempFile
FilePath
-- ^ Target path
FilePath
-- ^ Temporary path
Handle
| FileIOError FilePath IOException
deriving (Show, Eq)
instance Error e => Error (FileError e) where
displayError (FileErrorTempFile targetPath tempPath h)=
"Error creating temporary file at: " ++ tempPath ++
"/n" ++ "Target path: " ++ targetPath ++
"/n" ++ "Handle: " ++ show h
displayError (FileIOError path ioe) =
path ++ ": " ++ displayException ioe
displayError (FileError path e) =
path ++ ": " ++ displayError e
instance Error IOException where
displayError ioEx = show ioEx