0 | module System.File.Error
 1 |
 2 | import System.Errno
 3 |
 4 | import System.File.Support
 5 | import public System.File.Types
 6 |
 7 | %default total
 8 |
 9 | %foreign supportC "idris2_fileError"
10 |          "node:lambda:x=>(x===1?1:0)"
11 | prim__error : FilePtr -> PrimIO Int
12 |
13 | %foreign supportC "idris2_fileErrno"
14 |          supportNode "fileErrno"
15 | prim__fileErrno : PrimIO Int
16 |
17 | ||| The types of errors that can occur during file operations.
18 | public export
19 | data FileError = ||| A generic error with an errno
20 |                  GenericFileError Int
21 |                | FileReadError
22 |                | FileWriteError
23 |                | FileNotFound
24 |                | PermissionDenied
25 |                | FileExists
26 |
27 | ||| Return the `FileError` corresponding to the errno that was set when the
28 | ||| function call before this one errored.
29 | export
30 | returnError : HasIO io => io (Either FileError a)
31 | returnError
32 |     = do err <- primIO prim__fileErrno
33 |          pure $ Left $
34 |            case err of
35 |               0 => FileReadError
36 |               1 => FileWriteError
37 |               2 => FileNotFound
38 |               3 => PermissionDenied
39 |               4 => FileExists
40 |               _ => GenericFileError (err-5)
41 |
42 | export
43 | Show FileError where
44 |   show (GenericFileError errno) = strerror errno
45 |   show FileReadError = "File Read Error"
46 |   show FileWriteError = "File Write Error"
47 |   show FileNotFound = "File Not Found"
48 |   show PermissionDenied = "Permission Denied"
49 |   show FileExists = "File Exists"
50 |
51 | ||| Check if the error indicator for the given file handle is set.
52 | export
53 | fileError : HasIO io => File -> io Bool
54 | fileError (FHandle f)
55 |     = do x <- primIO $ prim__error f
56 |          pure (x /= 0)
57 |