Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Errors.hs @ ae858516

History | View | Annotate | Download (6.4 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 Text.JSON hiding (Result, Ok)
44
import System.Exit
45

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

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

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

    
114
instance JSON GanetiException where
115
  showJSON = saveGanetiException
116
  readJSON = loadGanetiException
117

    
118
instance FromString GanetiException where
119
  mkFromString = GenericError
120

    
121
-- | Error monad using 'GanetiException' type alias.
122
type ErrorResult = GenericResult GanetiException
123

    
124
$(genStrOfOp ''GanetiException "excName")
125

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

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

    
177
-- | Convert from an 'ErrorResult' to a standard 'Result'.
178
errToResult :: ErrorResult a -> Result a
179
errToResult (Ok a)  = Ok a
180
errToResult (Bad e) = Bad $ formatError e
181

    
182
-- | Convert from a 'Maybe' to a an 'ErrorResult'.
183
maybeToError :: String -> Maybe a -> ErrorResult a
184
maybeToError _ (Just a) = Ok a
185
maybeToError m  Nothing = Bad $ GenericError m