Skip to content

Commit aadd264

Browse files
committed
Use Either for exception handling in Sync
1 parent 8acc0eb commit aadd264

File tree

4 files changed

+113
-100
lines changed

4 files changed

+113
-100
lines changed

README.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -115,46 +115,46 @@
115115

116116
### Values
117117

118-
appendFile :: forall eff. FilePath -> Buffer -> Eff (err :: Exception Error, fs :: FS | eff) Unit
118+
appendFile :: forall eff. FilePath -> Buffer -> Eff (fs :: FS | eff) (Either String Unit)
119119

120-
appendTextFile :: forall eff. Encoding -> FilePath -> String -> Eff (err :: Exception Error, fs :: FS | eff) Unit
120+
appendTextFile :: forall eff. Encoding -> FilePath -> String -> Eff (fs :: FS | eff) (Either String Unit)
121121

122-
chmod :: forall eff. FilePath -> Number -> Eff (err :: Exception Error, fs :: FS | eff) Unit
122+
chmod :: forall eff. FilePath -> Number -> Eff (fs :: FS | eff) (Either String Unit)
123123

124-
chown :: forall eff. FilePath -> Number -> Number -> Eff (err :: Exception Error, fs :: FS | eff) Unit
124+
chown :: forall eff. FilePath -> Number -> Number -> Eff (fs :: FS | eff) (Either String Unit)
125125

126-
link :: forall eff. FilePath -> FilePath -> Eff (err :: Exception Error, fs :: FS | eff) Unit
126+
link :: forall eff. FilePath -> FilePath -> Eff (fs :: FS | eff) (Either String Unit)
127127

128-
mkdir :: forall eff. FilePath -> Eff (err :: Exception Error, fs :: FS | eff) Unit
128+
mkdir :: forall eff. FilePath -> Eff (fs :: FS | eff) (Either String Unit)
129129

130-
mkdir' :: forall eff. FilePath -> Number -> Eff (err :: Exception Error, fs :: FS | eff) Unit
130+
mkdir' :: forall eff. FilePath -> Number -> Eff (fs :: FS | eff) (Either String Unit)
131131

132-
readFile :: forall eff. FilePath -> Eff (err :: Exception Error, fs :: FS | eff) Buffer
132+
readFile :: forall eff. FilePath -> Eff (fs :: FS | eff) (Either String Buffer)
133133

134-
readTextFile :: forall eff. Encoding -> FilePath -> Eff (err :: Exception Error, fs :: FS | eff) String
134+
readTextFile :: forall eff. Encoding -> FilePath -> Eff (fs :: FS | eff) (Either String String)
135135

136-
readdir :: forall eff. FilePath -> Eff (err :: Exception Error, fs :: FS | eff) [FilePath]
136+
readdir :: forall eff. FilePath -> Eff (fs :: FS | eff) (Either String [FilePath])
137137

138-
readlink :: forall eff. FilePath -> Eff (err :: Exception Error, fs :: FS | eff) FilePath
138+
readlink :: forall eff. FilePath -> Eff (fs :: FS | eff) (Either String FilePath)
139139

140-
realpath :: forall eff. FilePath -> Eff (err :: Exception Error, fs :: FS | eff) FilePath
140+
realpath :: forall eff. FilePath -> Eff (fs :: FS | eff) (Either String FilePath)
141141

142-
realpath' :: forall eff cache. FilePath -> { | cache } -> Eff (err :: Exception Error, fs :: FS | eff) FilePath
142+
realpath' :: forall eff cache. FilePath -> { | cache } -> Eff (fs :: FS | eff) (Either String FilePath)
143143

144-
rename :: forall eff. FilePath -> FilePath -> Eff (err :: Exception Error, fs :: FS | eff) Unit
144+
rename :: forall eff. FilePath -> FilePath -> Eff (fs :: FS | eff) (Either String Unit)
145145

146-
rmdir :: forall eff. FilePath -> Eff (err :: Exception Error, fs :: FS | eff) Unit
146+
rmdir :: forall eff. FilePath -> Eff (fs :: FS | eff) (Either String Unit)
147147

148-
stat :: forall eff. FilePath -> Eff (err :: Exception Error, fs :: FS | eff) Stats
148+
stat :: forall eff. FilePath -> Eff (fs :: FS | eff) (Either String Stats)
149149

150-
symlink :: forall eff. FilePath -> FilePath -> SymlinkType -> Eff (err :: Exception Error, fs :: FS | eff) Unit
150+
symlink :: forall eff. FilePath -> FilePath -> SymlinkType -> Eff (fs :: FS | eff) (Either String Unit)
151151

152-
truncate :: forall eff. FilePath -> Number -> Eff (err :: Exception Error, fs :: FS | eff) Unit
152+
truncate :: forall eff. FilePath -> Number -> Eff (fs :: FS | eff) (Either String Unit)
153153

154-
unlink :: forall eff. FilePath -> Eff (err :: Exception Error, fs :: FS | eff) Unit
154+
unlink :: forall eff. FilePath -> Eff (fs :: FS | eff) (Either String Unit)
155155

156-
utimes :: forall eff. FilePath -> Date -> Date -> Eff (err :: Exception Error, fs :: FS | eff) Unit
156+
utimes :: forall eff. FilePath -> Date -> Date -> Eff (fs :: FS | eff) (Either String Unit)
157157

158-
writeFile :: forall eff. FilePath -> Buffer -> Eff (err :: Exception Error, fs :: FS | eff) Unit
158+
writeFile :: forall eff. FilePath -> Buffer -> Eff (fs :: FS | eff) (Either String Unit)
159159

160-
writeTextFile :: forall eff. Encoding -> FilePath -> String -> Eff (err :: Exception Error, fs :: FS | eff) Unit
160+
writeTextFile :: forall eff. Encoding -> FilePath -> String -> Eff (fs :: FS | eff) (Either String Unit)

bower.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@
2222
"purescript-maybe": ">=0.1.3",
2323
"purescript-foreign": ">=0.1.1",
2424
"purescript-node-buffer": "*",
25-
"purescript-node-path": "*",
25+
"purescript-node-path": ">=0.1.0",
2626
"purescript-globals": ">=0.1.3",
27-
"purescript-datetime": "*",
28-
"purescript-exceptions": ">=0.1.0"
27+
"purescript-datetime": "*"
2928
}
3029
}

examples/Test.purs

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import qualified Node.FS.Async as A
44
import qualified Node.FS.Sync as S
55
import Node.FS.Stats
66
import Control.Apply ((*>))
7-
import Control.Monad.Eff.Exception
87
import Data.Either
98
import Debug.Trace
109
import Node.Encoding
@@ -15,39 +14,41 @@ main = do
1514

1615
file <- S.readTextFile UTF8 "examples\\Test.purs"
1716
trace' "\n\nreadTextFile sync result:"
18-
trace' file
17+
case file of
18+
Right file' -> trace' $ file'
1919

20-
flip catchException (S.readTextFile UTF8 "examples\\does not exist") $ \e -> do
21-
trace' "Caught readTextFile exception:"
22-
trace' $ show e
23-
return ""
20+
err <- S.readTextFile UTF8 "examples\\does not exist"
21+
case err of
22+
Left err' -> trace' $ "Caught readTextFile error:\n" ++ show err'
2423

2524
S.rename "tmp\\Test.js" "tmp\\Test1.js"
2625

2726
S.truncate "tmp\\Test1.js" 1000
2827

2928
stats <- S.stat "tmp\\Test1.js"
30-
trace "\n\nS.stat:"
31-
trace' "isFile:"
32-
trace' $ show $ isFile stats
33-
trace' "isDirectory:"
34-
trace' $ show $ isDirectory stats
35-
trace' "isBlockDevice:"
36-
trace' $ show $ isBlockDevice stats
37-
trace' "isCharacterDevice:"
38-
trace' $ show $ isCharacterDevice stats
39-
trace' "isFIFO:"
40-
trace' $ show $ isFIFO stats
41-
trace' "isSocket:"
42-
trace' $ show $ isSocket stats
43-
trace' "isSymbolicLink:"
44-
trace' $ show $ isSymbolicLink stats
45-
trace' "modifiedTime:"
46-
trace' $ show $ modifiedTime stats
47-
trace' "accessedTime:"
48-
trace' $ show $ accessedTime stats
49-
trace' "statusChangedTime:"
50-
trace' $ show $ statusChangedTime stats
29+
case stats of
30+
Right stats' -> do
31+
trace "\n\nS.stat:"
32+
trace' "isFile:"
33+
trace' $ show $ isFile stats'
34+
trace' "isDirectory:"
35+
trace' $ show $ isDirectory stats'
36+
trace' "isBlockDevice:"
37+
trace' $ show $ isBlockDevice stats'
38+
trace' "isCharacterDevice:"
39+
trace' $ show $ isCharacterDevice stats'
40+
trace' "isFIFO:"
41+
trace' $ show $ isFIFO stats'
42+
trace' "isSocket:"
43+
trace' $ show $ isSocket stats'
44+
trace' "isSymbolicLink:"
45+
trace' $ show $ isSymbolicLink stats'
46+
trace' "modifiedTime:"
47+
trace' $ show $ modifiedTime stats'
48+
trace' "accessedTime:"
49+
trace' $ show $ accessedTime stats'
50+
trace' "statusChangedTime:"
51+
trace' $ show $ statusChangedTime stats'
5152

5253
A.rename "tmp\\Test1.js" "tmp\\Test.js" $ \x -> do
5354
trace "\n\nrename result:"

0 commit comments

Comments
 (0)