0 | module System.Zlib.Error
 1 |
 2 | import Derive.Prelude
 3 | import System.Posix.Timer
 4 | import System.Posix.Timer.Prim
 5 |
 6 | %default total
 7 | %language ElabReflection
 8 |
 9 | ||| Represents the possible errors reported by the zlib compression or decompression API.
10 | |||
11 | public export
12 | data ZlibErr
13 |   = StreamError
14 |   | DataError
15 |   | MemoryError
16 |   | BufferError
17 |   | VersionError
18 |   | NeedDictionary
19 |   | ErrnoError
20 |   | UnknownError Int
21 |   | MiscError String
22 |
23 | %runElab derive "ZlibErr" [Show]  
24 |
25 | ||| Converts a zlib return code to the corresponding typed error.
26 | |||
27 | ||| Returns `Nothing` for return codes that do not represent a zlib error, including `Z_OK` and `Z_STREAM_END`.
28 | |||
29 | export
30 | zlibError :  Int
31 |           -> Maybe ZlibErr
32 | zlibError 2    = Just NeedDictionary
33 | zlibError (-1) = Just ErrnoError
34 | zlibError (-2) = Just StreamError
35 | zlibError (-3) = Just DataError
36 | zlibError (-4) = Just MemoryError
37 | zlibError (-5) = Just BufferError
38 | zlibError (-6) = Just VersionError
39 | zlibError _    = Nothing
40 |
41 | ||| A wrapper around `ZlibErr`.
42 | |||
43 | ||| Fields:
44 | ||| - `fnname`                : The function the error originated from
45 | ||| - `errormessage`          : The error formatted as a String
46 | ||| - `errormessagetimestamp` : The timestamp the error occurred at
47 | |||
48 | public export
49 | record ZlibError where
50 |   constructor MkZlibError
51 |   fnname                : String
52 |   error                 : ZlibErr
53 |   errormessagetimestamp : Maybe (IClock CLOCK_REALTIME)
54 |
55 | public export
56 | Show ZlibError where
57 |   show (MkZlibError fnname error (Just errormessagetimestamp)) =
58 |     "MkZlibError " ++
59 |     fnname         ++
60 |     " "            ++
61 |     (show error)   ++
62 |     " "            ++
63 |     (asctime $ fromUTC errormessagetimestamp)
64 |   show (MkZlibError fnname error Nothing)                      =
65 |     "MkZlibError " ++
66 |     fnname         ++
67 |     " "            ++
68 |     (show error)   ++
69 |     " "            ++
70 |     "IClock CLOCK_REALTIME"
71 |