Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Errors.hs @ c92b4671

History | View | Annotate | Download (6.5 kB)

1
{-# LANGUAGE TemplateHaskell #-}
2

    
3
{-| Implementation of the Ganeti error types.
4

    
5
This module implements our error hierarchy. Currently we implement one
6
identical to the Python one; later we might one to have separate ones
7
for frontend (clients), master and backend code.
8

    
9
-}
10

    
11
{-
12

    
13
Copyright (C) 2012 Google Inc.
14

    
15
This program is free software; you can redistribute it and/or modify
16
it under the terms of the GNU General Public License as published by
17
the Free Software Foundation; either version 2 of the License, or
18
(at your option) any later version.
19

    
20
This program is distributed in the hope that it will be useful, but
21
WITHOUT ANY WARRANTY; without even the implied warranty of
22
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23
General Public License for more details.
24

    
25
You should have received a copy of the GNU General Public License
26
along with this program; if not, write to the Free Software
27
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28
02110-1301, USA.
29

    
30
-}
31

    
32
module Ganeti.Errors
33
  ( ErrorCode(..)
34
  , GanetiException(..)
35
  , ErrorResult
36
  , errToResult
37
  , errorExitCode
38
  , excName
39
  , formatError
40
  , maybeToError
41
  ) where
42

    
43
import Control.Monad.Error (Error(..))
44
import Text.JSON hiding (Result, Ok)
45
import System.Exit
46

    
47
import Ganeti.THH
48
import Ganeti.BasicTypes
49
import qualified Ganeti.Constants as C
50

    
51
-- | Error code types for 'OpPrereqError'.
52
$(declareSADT "ErrorCode"
53
  [ ("ECodeResolver",  'C.errorsEcodeResolver)
54
  , ("ECodeNoRes",     'C.errorsEcodeNores)
55
  , ("ECodeTempNoRes", 'C.errorsEcodeTempNores)
56
  , ("ECodeInval",     'C.errorsEcodeInval)
57
  , ("ECodeState",     'C.errorsEcodeState)
58
  , ("ECodeNoEnt",     'C.errorsEcodeNoent)
59
  , ("ECodeExists",    'C.errorsEcodeExists)
60
  , ("ECodeNotUnique", 'C.errorsEcodeNotunique)
61
  , ("ECodeFault",     'C.errorsEcodeFault)
62
  , ("ECodeEnviron",   'C.errorsEcodeEnviron)
63
  ])
64
$(makeJSONInstance ''ErrorCode)
65

    
66
$(genException "GanetiException"
67
  [ ("GenericError", [excErrMsg])
68
  , ("LockError", [excErrMsg])
69
  , ("PidFileLockError", [excErrMsg])
70
  , ("HypervisorError", [excErrMsg])
71
  , ("ProgrammerError", [excErrMsg])
72
  , ("BlockDeviceError", [excErrMsg])
73
  , ("ConfigurationError", [excErrMsg])
74
  , ("ConfigVersionMismatch", [ ("expVer", [t| Int |])
75
                              , ("actVer", [t| Int |])])
76
  , ("ReservationError", [excErrMsg])
77
  , ("RemoteError", [excErrMsg])
78
  , ("SignatureError", [excErrMsg])
79
  , ("ParameterError", [excErrMsg])
80
  , ("ResultValidationError", [excErrMsg])
81
  , ("OpPrereqError", [excErrMsg, ("errCode", [t| ErrorCode |])])
82
  , ("OpExecError", [excErrMsg])
83
  , ("OpResultError", [excErrMsg])
84
  , ("OpCodeUnknown", [excErrMsg])
85
  , ("JobLost", [excErrMsg])
86
  , ("JobFileCorrupted", [excErrMsg])
87
  , ("ResolverError", [ ("errHostname", [t| String |])
88
                      , ("errResolverCode", [t| Int |])
89
                      , ("errResolverMsg", [t| String |])])
90
  , ("HooksFailure", [excErrMsg])
91
  , ("HooksAbort", [("errs", [t| [(String, String, String)] |])])
92
  , ("UnitParseError", [excErrMsg])
93
  , ("ParseError", [excErrMsg])
94
  , ("TypeEnforcementError", [excErrMsg])
95
  , ("X509CertError", [ ("certFileName", [t| String |])
96
                      , excErrMsg ])
97
  , ("TagError", [excErrMsg])
98
  , ("CommandError", [excErrMsg])
99
  , ("StorageError", [excErrMsg])
100
  , ("InotifyError", [excErrMsg])
101
  , ("JobQueueError", [excErrMsg])
102
  , ("JobQueueDrainError", [excErrMsg])
103
  , ("JobQueueFull", [])
104
  , ("ConfdMagicError", [excErrMsg])
105
  , ("ConfdClientError", [excErrMsg])
106
  , ("UdpDataSizeError", [excErrMsg])
107
  , ("NoCtypesError", [excErrMsg])
108
  , ("IPAddressError", [excErrMsg])
109
  , ("LuxiError", [excErrMsg])
110
  , ("QueryFilterParseError", [excErrMsg]) -- not consistent with Python
111
  , ("RapiTestResult", [excErrMsg])
112
  , ("FileStoragePathError", [excErrMsg])
113
  ])
114

    
115
instance Error GanetiException where
116
  strMsg = mkFromString
117

    
118
instance JSON GanetiException where
119
  showJSON = saveGanetiException
120
  readJSON = loadGanetiException
121

    
122
instance FromString GanetiException where
123
  mkFromString = GenericError
124

    
125
-- | Error monad using 'GanetiException' type alias.
126
type ErrorResult = GenericResult GanetiException
127

    
128
$(genStrOfOp ''GanetiException "excName")
129

    
130
-- | Returns the exit code of a program that should be used if we got
131
-- back an exception from masterd.
132
errorExitCode :: GanetiException -> ExitCode
133
errorExitCode (ConfigurationError {}) = ExitFailure 2
134
errorExitCode _ = ExitFailure 1
135

    
136
-- | Formats an exception.
137
formatError :: GanetiException -> String
138
formatError (ConfigurationError msg) =
139
  "Corrup configuration file: " ++ msg ++ "\nAborting."
140
formatError (HooksAbort errs) =
141
  unlines $
142
  "Failure: hooks execution failed:":
143
  map (\(node, script, out) ->
144
         "  node: " ++ node ++ ", script: " ++ script ++
145
                    if null out
146
                      then " (no output)"
147
                      else ", output: " ++ out
148
      ) errs
149
formatError (HooksFailure msg) =
150
  "Failure: hooks general failure: " ++ msg
151
formatError (ResolverError host _ _) =
152
  -- FIXME: in Python, this uses the system hostname to format the
153
  -- error differently if we are failing to resolve our own hostname
154
  "Failure: can't resolve hostname " ++ host
155
formatError (OpPrereqError msg code) =
156
  "Failure: prerequisites not met for this" ++
157
  " operation:\nerror type: " ++ show code ++ ", error details:\n" ++ msg
158
formatError (OpExecError msg) =
159
  "Failure: command execution error:\n" ++ msg
160
formatError (TagError msg) =
161
  "Failure: invalid tag(s) given:\n" ++ msg
162
formatError (JobQueueDrainError _)=
163
  "Failure: the job queue is marked for drain and doesn't accept new requests"
164
formatError JobQueueFull =
165
  "Failure: the job queue is full and doesn't accept new" ++
166
  " job submissions until old jobs are archived"
167
formatError (TypeEnforcementError msg) =
168
  "Parameter Error: " ++ msg
169
formatError (ParameterError msg) =
170
  "Failure: unknown/wrong parameter name '" ++ msg ++ "'"
171
formatError (JobLost msg) =
172
  "Error checking job status: " ++ msg
173
formatError (QueryFilterParseError msg) =
174
  -- FIXME: in Python, this has a more complex error message
175
  "Error while parsing query filter: " ++ msg
176
formatError (GenericError msg) =
177
  "Unhandled Ganeti error: " ++ msg
178
formatError err =
179
  "Unhandled exception: " ++ show err
180

    
181
-- | Convert from an 'ErrorResult' to a standard 'Result'.
182
errToResult :: ErrorResult a -> Result a
183
errToResult (Ok a)  = Ok a
184
errToResult (Bad e) = Bad $ formatError e
185

    
186
-- | Convert from a 'Maybe' to a an 'ErrorResult'.
187
maybeToError :: String -> Maybe a -> ErrorResult a
188
maybeToError _ (Just a) = Ok a
189
maybeToError m  Nothing = Bad $ GenericError m