Merge pull request #499 from michaelficarra/GH-497
fixes #497: do not throw plain strings for failed pattern matches
commit711cbd314516a8cc7b651f40c3aff310c9559581
authorGary Burgess <gary.burgess@gmail.com>
dateWed 23 Jul 2014, 18:41 (12 years ago)
parent154098b1b75ac3
examples/passing/PartialFunction.pursnew file+17
@@ -0,0 +1,17 @@
1+module Main where
2+
3+foreign import testError
4+ "function testError(f) {\
5+ \ try {\
6+ \ return f();\
7+ \ } catch (e) {\
8+ \ if (e instanceof Error) return 'success';\
9+ \ throw new Error('Pattern match failure is not TypeError');\
10+ \ }\
11+ \}" :: (Unit -> Number) -> Number
12+
13+fn :: Number -> Number
14+fn 0 = 0
15+fn 1 = 2
16+
17+main = Debug.Trace.trace (show $ testError $ \_ -> fn 2)
src/Language/PureScript/CodeGen/JS.hs+1−1
@@ -302,7 +302,7 @@ bindersToJs opts m e binders vals = do
302302 jss <- forM binders $ \(CaseAlternative bs grd result) -> do
303303 ret <- valueToJs opts m e result
304304 go valNames [JSReturn ret] bs grd
305− return $ JSApp (JSFunction Nothing valNames (JSBlock (concat jss ++ [JSThrow (JSStringLiteral "Failed pattern match")])))
305+ return $ JSApp (JSFunction Nothing valNames (JSBlock (concat jss ++ [JSThrow $ JSApp (JSVar "Error") $ [(JSStringLiteral "Failed pattern match")]])))
306306 vals
307307 where
308308 go :: (Functor m, Applicative m, Monad m) => [String] -> [JS] -> [Binder] -> Maybe Guard -> SupplyT m [JS]