~zinigor/cardano-node/trunk

« back to all changes in this revision

Viewing changes to cardano-api/src/Cardano/Api/Error.hs

  • Committer: Igor Zinovyev
  • Date: 2021-08-13 19:12:27 UTC
  • Revision ID: zinigor@gmail.com-20210813191227-stlnsj3mc5ypwn0c
Tags: upstream-1.27.0
ImportĀ upstreamĀ versionĀ 1.27.0

Show diffs side-by-side

added added

removed removed

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