-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathError.hs
More file actions
375 lines (321 loc) · 15.9 KB
/
Error.hs
File metadata and controls
375 lines (321 loc) · 15.9 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
{-# LANGUAGE CPP #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE TemplateHaskell #-}
module Data.API.Error
( -- * Representation of JSON parsing errors
JSONError(..)
, JSONWarning
, Expected(..)
, FormatExpected(..)
, Position
, Step(..)
, inField
, prettyJSONErrorPositions
, prettyJSONError
, prettyStep
-- * JSON parse error construction
, expectedArray
, expectedSet
, expectedBool
, expectedInt
, expectedObject
, expectedString
, badFormat
-- * Validation and migration errors
, ValueError(..)
, ValidateFailure(..)
, ValidateWarning
, ApplyFailure(..)
, TypeKind(..)
, MigrateFailure(..)
, MigrateWarning
, prettyMigrateFailure
, prettyValidateFailure
, prettyValueError
, prettyValueErrorPosition
) where
import Data.API.Changes.Types
import Data.API.PP
import Data.API.NormalForm
import Data.API.Types
import Data.API.Utils
import qualified Data.Aeson as JS
import Data.Aeson.TH
import qualified Data.Graph as Graph
import Data.List
import Data.Map ( Map )
import qualified Data.Map as Map
import qualified Data.SafeCopy as SC
import Data.Set ( Set )
import qualified Data.Set as Set
import qualified Data.Text as T
import Data.Time
----------------------------------------------------------
-- Representation of JSON parsing errors and positions
--
-- | Represents an error that can be encountered while parsing
data JSONError = Expected Expected String JS.Value
| BadFormat FormatExpected String T.Text
| MissingField
| MissingAlt [String]
| UnexpectedField
| UnexpectedEnumVal [T.Text] T.Text
| IntRangeError String Int IntRange
| UTCRangeError String UTCTime UTCRange
| RegexError String T.Text RegEx
| SyntaxError String
deriving (Eq, Show)
-- | At present, we do not distinguish between errors and warnings
type JSONWarning = JSONError
-- | JSON type expected at a particular position, when a value of a
-- different type was encountered
data Expected = ExpArray
| ExpBool
| ExpInt
| ExpObject
| ExpString
deriving (Eq, Show)
-- | Special format expected of a string
data FormatExpected = FmtBinary
| FmtUTC
| FmtOther
deriving (Eq, Show)
expectedArray, expectedSet, expectedBool, expectedInt, expectedObject, expectedString
:: JS.Value -> JSONError
expectedArray = Expected ExpArray "Array"
expectedBool = Expected ExpBool "Bool"
expectedInt = Expected ExpInt "Int"
expectedObject = Expected ExpObject "Object"
expectedString = Expected ExpString "String"
expectedSet = Expected ExpObject "Set"
badFormat :: String -> T.Text -> JSONError
badFormat = BadFormat FmtOther
-- | Human-readable description of a JSON parse error
prettyJSONError :: JSONError -> String
prettyJSONError (Expected _ s v) = "When expecting " ++ s ++ ", encountered "
++ x ++ " instead"
where
x = case v of
JS.Object _ -> "Object"
JS.Array _ -> "Array"
JS.String _ -> "String"
JS.Number _ -> "Number"
JS.Bool _ -> "Boolean"
JS.Null -> "Null"
prettyJSONError (BadFormat _ s t) = "Could not parse as " ++ s ++ " the string " ++ show t
prettyJSONError MissingField = "Field missing from Object"
prettyJSONError (MissingAlt xs) = "Missing alternative, expecting one of: "
++ intercalate ", " xs
prettyJSONError UnexpectedField = "Unexpected field in Object"
prettyJSONError (UnexpectedEnumVal xs t) = "Unexpected enum value " ++ show t
++ ", expecting one of: "
++ T.unpack (T.intercalate ", " xs)
prettyJSONError (IntRangeError s i r) = s ++ ": " ++ show i ++ " not in range " ++ show r
prettyJSONError (UTCRangeError s u r) = s ++ ": " ++ show u ++ " not in range " ++ show r
prettyJSONError (RegexError s _ t) = s ++ ": failed to match RE: " ++ show t
prettyJSONError (SyntaxError e) = "JSON syntax error: " ++ e
-- | A position inside a JSON value is a list of steps, ordered
-- innermost first (so going inside an object prepends a step).
type Position = [Step]
-- | Each step may be into a field of an object, or a specific element
-- of an array.
data Step = InField T.Text | InElem Int
deriving (Eq, Show)
inField :: FieldName -> Step
inField fn = InField (_FieldName fn)
-- | Human-readable description of a single step in a position
prettyStep :: Step -> String
prettyStep (InField f) = " in the field " ++ show f
prettyStep (InElem i) = " in array index " ++ show i
instance PPLines Step where
ppLines s = [prettyStep s]
-- | Human-readable presentation of a list of parse errors with their
-- positions
prettyJSONErrorPositions :: [(JSONError, Position)] -> String
prettyJSONErrorPositions xs = unlines $ concatMap help xs
where
help (e, pos) = prettyJSONError e : map prettyStep pos
----------------------------------------------------------
-- Validation and data migration errors
--
-- | Errors that can be discovered when migrating data values
data ValueError
= JSONError JSONError -- ^ Data doesn't match schema
| CustomMigrationError String JS.Value -- ^ Error generated during custom migration
| InvalidAPI ApplyFailure -- ^ An API change was invalid
deriving (Eq, Show)
-- | Errors that may be discovered when validating a changelog
data ValidateFailure
-- | the changelog must be in descending order of versions
= ChangelogOutOfOrder { vfLaterVersion :: VersionExtra
, vfEarlierVersion :: VersionExtra }
-- | forbid migrating from one version to an earlier version
| CannotDowngrade { vfFromVersion :: VersionExtra
, vfToVersion :: VersionExtra }
-- | an API uses types that are not declared
| ApiInvalid { vfInvalidVersion :: VersionExtra
, vfMissingDeclarations :: Set TypeName }
-- | changelog entry does not apply
| ChangelogEntryInvalid { vfSuccessfullyApplied :: [APITableChange]
, vfFailedToApply :: APIChange
, vfApplyFailure :: ApplyFailure }
-- | changelog is incomplete
-- (ie all entries apply ok but result isn't the target api)
| ChangelogIncomplete { vfChangelogVersion :: VersionExtra
, vfTargetVersion :: VersionExtra
, vfDifferences :: Map TypeName (MergeResult NormTypeDecl NormTypeDecl) }
deriving (Eq, Show)
data ValidateWarning = ValidateWarning -- add warnings about bits we cannot check (opaque custom)
deriving Show
-- | Errors that may occur applying a single API change
data ApplyFailure
= TypeExists { afExistingType :: TypeName } -- ^ for adding or renaming type
| TypeDoesNotExist { afMissingType :: TypeName } -- ^ for deleting or renaming a type
| TypeWrongKind { afTypeName :: TypeName
, afExpectedKind :: TypeKind } -- ^ e.g. it's not a record type
| TypeInUse { afTypeName :: TypeName } -- ^ cannot delete/modify types that are still used
| TypeMalformed { afType :: APIType
, afMissingTypes :: Set TypeName } -- ^ type refers to a non-existent type
| DeclMalformed { afTypeName :: TypeName
, afDecl :: NormTypeDecl
, afMissingTypes :: Set TypeName } -- ^ decl refers to a non-existent type
| FieldExists { afTypeName :: TypeName
, afTypeKind :: TypeKind
, afExistingField :: FieldName } -- ^ for adding or renaming a field
| FieldDoesNotExist { afTypeName :: TypeName
, afTypeKind :: TypeKind
, afMissingField :: FieldName } -- ^ for deleting or renaming a field
| FieldBadDefaultValue { afTypeName :: TypeName
, afFieldName :: FieldName
, afFieldType :: APIType
, afBadDefault :: DefaultValue } -- ^ for adding a field, must be a default
-- value compatible with the type
| DefaultMissing { afTypeName :: TypeName
, afFieldName :: FieldName } -- ^ for adding a field to a table
| TableChangeError { afCustomMessage :: String } -- ^ custom error in tableChange
deriving (Eq, Show)
data TypeKind = TKRecord | TKUnion | TKEnum | TKNewtype | TKTypeSynonym
deriving (Eq, Show)
data MigrateFailure
= ValidateFailure ValidateFailure
| ValueError ValueError Position
deriving (Eq, Show)
type MigrateWarning = ValidateWarning
-------------------------------------
-- Pretty-printing
--
prettyMigrateFailure :: MigrateFailure -> String
prettyMigrateFailure = unlines . ppLines
prettyValidateFailure :: ValidateFailure -> String
prettyValidateFailure = unlines . ppLines
prettyValueError :: ValueError -> String
prettyValueError = unlines . ppLines
prettyValueErrorPosition :: (ValueError, Position) -> String
prettyValueErrorPosition = unlines . ppLines
instance PP TypeKind where
pp TKRecord = "record"
pp TKUnion = "union"
pp TKEnum = "enum"
pp TKNewtype = "newtype"
pp TKTypeSynonym = "type"
ppATypeKind :: TypeKind -> String
ppATypeKind TKRecord = "a record"
ppATypeKind TKUnion = "a union"
ppATypeKind TKEnum = "an enum"
ppATypeKind TKNewtype = "a newtype"
ppATypeKind TKTypeSynonym = "a type synonym"
ppMemberWord :: TypeKind -> String
ppMemberWord TKRecord = "field"
ppMemberWord TKUnion = "alternative"
ppMemberWord TKEnum = "value"
ppMemberWord TKNewtype = "member"
ppMemberWord TKTypeSynonym = "member"
instance PPLines MigrateFailure where
ppLines (ValidateFailure x) = ppLines x
ppLines (ValueError x ps) = ppLines x ++ map prettyStep ps
instance PPLines ValidateFailure where
ppLines (ChangelogOutOfOrder later earlier) =
["Changelog out of order: version " ++ pp later
++ " appears after version " ++ pp earlier]
ppLines (CannotDowngrade from to) =
["Cannot downgrade from version " ++ pp from
++ " to version " ++ pp to]
ppLines (ApiInvalid ver missing) =
["Missing declarations in API version " ++ pp ver ++ ": " ++ pp missing]
ppLines (ChangelogEntryInvalid succs change af) =
ppLines af ++ ("when applying the change" : indent (ppLines change))
++ if not (null succs)
then "after successfully applying the changes:"
: indent (ppLines succs)
else []
ppLines (ChangelogIncomplete ver ver' diffs) =
("Changelog incomplete! Differences between log version ("
++ showVersionExtra ver ++ ") and latest version (" ++ showVersionExtra ver' ++ "):")
: indent (ppDiffs diffs)
ppDiffs :: Map TypeName (MergeResult NormTypeDecl NormTypeDecl) -> [String]
ppDiffs = concatMap (uncurry ppDiff) . sortDiffs . Map.toList
-- | Perform a topological sort of the differences, so that the
-- pretty-printed form can be copied directly into the changelog.
sortDiffs :: [(TypeName, MergeResult NormTypeDecl NormTypeDecl)]
-> [(TypeName, MergeResult NormTypeDecl NormTypeDecl)]
sortDiffs = reverse . Graph.flattenSCCs . Graph.stronglyConnComp . map f
where
f (tn, mr) = ((tn, mr), tn, Set.toList (mergeResultFreeVars mr))
mergeResultFreeVars :: MergeResult NormTypeDecl NormTypeDecl -> Set TypeName
mergeResultFreeVars (OnlyInLeft x) = typeDeclFreeVars x
mergeResultFreeVars (OnlyInRight x) = typeDeclFreeVars x
mergeResultFreeVars (InBoth x y) = typeDeclFreeVars x `Set.union` typeDeclFreeVars y
ppDiff :: TypeName -> MergeResult NormTypeDecl NormTypeDecl -> [String]
ppDiff t (OnlyInLeft _) = ["removed " ++ pp t]
ppDiff t (OnlyInRight d) = ("added " ++ pp t ++ " ") `inFrontOf` ppLines d
ppDiff t (InBoth (NRecordType flds) (NRecordType flds')) =
("changed record " ++ pp t)
: (concatMap (uncurry (ppDiffFields "field")) $ Map.toList $ diffMaps flds flds')
ppDiff t (InBoth (NUnionType alts) (NUnionType alts')) =
("changed union " ++ pp t)
: (concatMap (uncurry (ppDiffFields "alternative")) $ Map.toList $ diffMaps alts alts')
ppDiff t (InBoth (NEnumType vals) (NEnumType vals')) =
("changed enum " ++ pp t)
: (map (\ x -> " alternative removed " ++ pp x) $ Set.toList $ vals Set.\\ vals')
++ (map (\ x -> " alternative added " ++ pp x) $ Set.toList $ vals' Set.\\ vals)
ppDiff t (InBoth _ _) = ["incompatible definitions of " ++ pp t]
ppDiffFields :: String -> FieldName -> MergeResult APIType APIType -> [String]
ppDiffFields s f (OnlyInLeft _) = [" " ++ s ++ " removed " ++ pp f]
ppDiffFields s f (OnlyInRight ty) = [" " ++ s ++ " added " ++ pp f ++ " :: " ++ pp ty]
ppDiffFields s f (InBoth ty ty') = [ " incompatible types for " ++ s ++ " " ++ pp f
, " changelog type: " ++ pp ty
, " latest version type: " ++ pp ty' ]
instance PPLines ApplyFailure where
ppLines (TypeExists t) = ["Type " ++ pp t ++ " already exists"]
ppLines (TypeDoesNotExist t) = ["Type " ++ pp t ++ " does not exist"]
ppLines (TypeWrongKind t k) = ["Type " ++ pp t ++ " is not " ++ ppATypeKind k]
ppLines (TypeInUse t) = ["Type " ++ pp t ++ " is in use, so it cannot be modified"]
ppLines (TypeMalformed ty xs) = ["Type " ++ pp ty
++ " is malformed, missing declarations:"
, " " ++ pp xs]
ppLines (DeclMalformed t _ xs) = [ "Declaration of " ++ pp t
++ " is malformed, missing declarations:"
, " " ++ pp xs]
ppLines (FieldExists t k f) = ["Type " ++ pp t ++ " already has the "
++ ppMemberWord k ++ " " ++ pp f]
ppLines (FieldDoesNotExist t k f) = ["Type " ++ pp t ++ " does not have the "
++ ppMemberWord k ++ " " ++ pp f]
ppLines (FieldBadDefaultValue _ _ ty v) = ["Default value " ++ pp v
++ " is not compatible with the type " ++ pp ty]
ppLines (DefaultMissing t f) = ["Field " ++ pp f ++ " does not have a default value, but "
++ pp t ++ " occurs in the database"]
ppLines (TableChangeError s) = ["Error when detecting changed tables:", " " ++ s]
instance PPLines ValueError where
ppLines (JSONError e) = [prettyJSONError e]
ppLines (CustomMigrationError e v) = [ "Custom migration error:", " " ++ e
, "when migrating value"] ++ indent (ppLines v)
ppLines (InvalidAPI af) = "Invalid API detected during value migration:"
: indent (ppLines af)
$(deriveJSON defaultOptions ''Expected)
$(deriveJSON defaultOptions ''FormatExpected)
$(deriveJSON defaultOptions ''Step)
$(deriveJSON defaultOptions ''JSONError)
$(SC.deriveSafeCopy 1 'SC.base ''Step)