Skip to content

Commit 3de6a7e

Browse files
committed
Merge pull request #4 from purescript-contrib/exceptions
Use new purescript-exceptions
2 parents bf46294 + 812550d commit 3de6a7e

File tree

6 files changed

+144
-156
lines changed

6 files changed

+144
-156
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 (fs :: FS | eff) (Either Error Unit)
118+
appendFile :: forall eff. FilePath -> Buffer -> Eff (err :: Exception, fs :: FS | eff) Unit
119119

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

bower.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"purescript-foreign": ">=0.1.1",
2424
"purescript-node-buffer": "*",
2525
"purescript-node-path": ">=0.1.0",
26-
"purescript-globals": ">=0.1.4",
27-
"purescript-datetime": "*"
26+
"purescript-datetime": "*",
27+
"purescript-exceptions": "~0.2.0"
2828
}
2929
}

examples/Test.purs

Lines changed: 52 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,90 +4,86 @@ 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
78
import Data.Either
89
import Debug.Trace
910
import Node.Encoding
1011

11-
trace' x = trace x *> return unit
12-
1312
main = do
1413

1514
file <- S.readTextFile UTF8 "examples\\Test.purs"
16-
trace' "\n\nreadTextFile sync result:"
17-
case file of
18-
Right file' -> trace' $ file'
15+
trace "\n\nreadTextFile sync result:"
16+
trace $ file
1917

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

2422
S.rename "tmp\\Test.js" "tmp\\Test1.js"
2523

2624
S.truncate "tmp\\Test1.js" 1000
2725

2826
stats <- S.stat "tmp\\Test1.js"
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'
27+
trace "\n\nS.stat:"
28+
trace "isFile:"
29+
trace $ show $ isFile stats
30+
trace "isDirectory:"
31+
trace $ show $ isDirectory stats
32+
trace "isBlockDevice:"
33+
trace $ show $ isBlockDevice stats
34+
trace "isCharacterDevice:"
35+
trace $ show $ isCharacterDevice stats
36+
trace "isFIFO:"
37+
trace $ show $ isFIFO stats
38+
trace "isSocket:"
39+
trace $ show $ isSocket stats
40+
trace "isSymbolicLink:"
41+
trace $ show $ isSymbolicLink stats
42+
trace "modifiedTime:"
43+
trace $ show $ modifiedTime stats
44+
trace "accessedTime:"
45+
trace $ show $ accessedTime stats
46+
trace "statusChangedTime:"
47+
trace $ show $ statusChangedTime stats
5248

5349
A.rename "tmp\\Test1.js" "tmp\\Test.js" $ \x -> do
5450
trace "\n\nrename result:"
55-
either (trace' <<< show) (trace' <<< show) x
51+
either (trace <<< show) (trace <<< show) x
5652

5753
A.truncate "tmp\\Test.js" 10 $ \x -> do
5854
trace "\n\ntruncate result:"
59-
either (trace' <<< show) (trace' <<< show) x
55+
either (trace <<< show) (trace <<< show) x
6056

6157
A.readFile "examples\\Test.purs" $ \x -> do
6258
trace "\n\nreadFile result:"
63-
either (trace' <<< show) (trace' <<< show) x
59+
either (trace <<< show) (trace <<< show) x
6460

6561
A.readTextFile UTF8 "examples\\Test.purs" $ \x -> do
6662
trace "\n\nreadTextFile result:"
67-
either (trace' <<< show) trace' x
63+
either (trace <<< show) trace x
6864

6965
A.stat "examples\\Test.purs" $ \x -> do
7066
trace "\n\nstat:"
7167
case x of
72-
Left err -> trace' $ "Error:" ++ show err
68+
Left err -> trace $ "Error:" ++ show err
7369
Right x' -> do
74-
trace' "isFile:"
75-
trace' $ show $ isFile x'
76-
trace' "isDirectory:"
77-
trace' $ show $ isDirectory x'
78-
trace' "isBlockDevice:"
79-
trace' $ show $ isBlockDevice x'
80-
trace' "isCharacterDevice:"
81-
trace' $ show $ isCharacterDevice x'
82-
trace' "isFIFO:"
83-
trace' $ show $ isFIFO x'
84-
trace' "isSocket:"
85-
trace' $ show $ isSocket x'
86-
trace' "isSymbolicLink:"
87-
trace' $ show $ isSymbolicLink x'
88-
trace' "modifiedTime:"
89-
trace' $ show $ modifiedTime x'
90-
trace' "accessedTime:"
91-
trace' $ show $ accessedTime x'
92-
trace' "statusChangedTime:"
93-
trace' $ show $ statusChangedTime x'
70+
trace "isFile:"
71+
trace $ show $ isFile x'
72+
trace "isDirectory:"
73+
trace $ show $ isDirectory x'
74+
trace "isBlockDevice:"
75+
trace $ show $ isBlockDevice x'
76+
trace "isCharacterDevice:"
77+
trace $ show $ isCharacterDevice x'
78+
trace "isFIFO:"
79+
trace $ show $ isFIFO x'
80+
trace "isSocket:"
81+
trace $ show $ isSocket x'
82+
trace "isSymbolicLink:"
83+
trace $ show $ isSymbolicLink x'
84+
trace "modifiedTime:"
85+
trace $ show $ modifiedTime x'
86+
trace "accessedTime:"
87+
trace $ show $ accessedTime x'
88+
trace "statusChangedTime:"
89+
trace $ show $ statusChangedTime x'

src/Node/FS/Async.purs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,34 @@ module Node.FS.Async
2525
) where
2626

2727
import Control.Monad.Eff
28+
import Control.Monad.Eff.Exception
2829
import Data.Date
2930
import Data.Either
30-
import Data.Foreign
3131
import Data.Function
3232
import Data.Maybe
3333
import Node.Buffer (Buffer(..))
3434
import Node.Encoding
3535
import Node.FS
3636
import Node.FS.Stats
3737
import Node.Path (FilePath())
38-
import Global (Error(), error)
3938

40-
type JSCallback a = Fn2 Foreign a Unit
39+
foreign import data Nullable :: * -> *
4140

42-
foreign import runCallbackEff
43-
"function runCallbackEff (f) {\
44-
\ return f(); \
45-
\}" :: forall eff a. Eff eff a -> a
41+
type JSCallback a = Fn2 (Nullable Error) a Unit
42+
43+
foreign import handleCallbackImpl
44+
"function handleCallbackImpl(left, right, f) {\
45+
\ return function(err, value) {\
46+
\ if (err) f(left(err))();\
47+
\ else f(right(value))();\
48+
\ };\
49+
\}" :: forall eff a. Fn3 (Error -> Either Error a)
50+
(a -> Either Error a)
51+
(Callback eff a)
52+
(JSCallback a)
4653

4754
handleCallback :: forall eff a b. (Callback eff a) -> JSCallback a
48-
handleCallback f = mkFn2 $ \err x -> runCallbackEff $ f case parseForeign read err of
49-
Left err -> Left $ error $ "handleCallback failed: " ++ show err
50-
Right (Just err') -> Left err'
51-
Right Nothing -> Right x
55+
handleCallback cb = runFn3 handleCallbackImpl Left Right cb
5256

5357
foreign import fs "var fs = require('fs');" ::
5458
{ rename :: Fn3 FilePath FilePath (JSCallback Unit) Unit

src/Node/FS/Stats.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type StatsObj =
3939

4040
-- |
4141
-- Stats wrapper to provide a usable interface to the underlying properties and methods.
42-
--
42+
--
4343
data Stats = Stats StatsObj
4444

4545
foreign import showStatsObj
@@ -50,11 +50,11 @@ foreign import showStatsObj
5050
instance showStats :: Show Stats where
5151
show (Stats o) = "Stats " ++ showStatsObj o
5252

53-
foreign import statsMethod
53+
foreign import statsMethod
5454
"function statsMethod(m, s) {\
5555
\ return s[m]();\
5656
\}" :: Fn2 String StatsObj Boolean
57-
57+
5858
isFile :: Stats -> Boolean
5959
isFile (Stats s) = runFn2 statsMethod "isFile" s
6060

0 commit comments

Comments
 (0)