[Better error handling for draft loading. prb@mult.ifario.us**20080617070928] { hunk ./src/Blog/BackEnd/IoOperations.hs 19 +import qualified Control.Exception as CE hunk ./src/Blog/BackEnd/IoOperations.hs 35 - ; return $ B.build_model (map (uncurry EP.item_from_string) (zip all content)) } + ; let loaded = map (uncurry EP.item_from_string) $ zip all content + ; let ok_items = map unr $ filter isr loaded + ; mapM (uncurry loading_error) $ map unl $ filter isl loaded + ; return $ B.build_model ok_items } + +loading_error :: String -> String -> IO () +loading_error = loading_error_ log_handle + +loading_error_ :: String -> String -> String -> IO () +loading_error_ lh path reason = errorM lh $ "Error parsing entry file " ++ path ++ ": " ++ reason + +unr :: Either a b -> b +unr (Right r) = r +unr (Left _) = error "Can't unright a left." + +unl :: Either a b -> a +unl (Right _) = error "Can't unleft a right." +unl (Left l) = l + +isr :: Either a b -> Bool +isr (Right _) = True +isr (Left _) = False + +isl :: Either a b -> Bool +isl (Right _) = False +isl (Left _) = True hunk ./src/Blog/BackEnd/IoOperations.hs 105 -load_content f = load log_handle$ C.content_storage_dir f "content.ppp" +load_content = load_with log_handle EP.item_from_string (\g -> C.content_storage_dir g "content.ppp") + +load_with :: String -> (String -> String -> Either (String,String) B.Item) -> (String -> String) -> FilePath -> IO (Maybe B.Item) +load_with lh parser base f = do { file <- CE.try $ readFile' $ base f + ; case file of + Right content -> + do { let d = parser f $! content + ; case d of + Right i -> + return $ Just i + Left (p,e) -> + loading_error_ lh p e >> return Nothing } + Left ex -> + do { errorM lh $ "Exception loading " ++ f ++ ": " ++ (show ex) + ; return Nothing } + } hunk ./src/Blog/BackEnd/IoOperations.hs 123 -load_draft f = do { content <- readFile' $ C.draft_dir f - ; return $ Just . (EP.draft_from_string f) $! content } - `SIE.catch` - \e -> do { errorM log_handle $ "Error loading draft content from " - ++ f ++ "; exception was: " ++ ( show e ) - ; return Nothing } +load_draft = load_with log_handle EP.draft_from_string (\f -> C.draft_dir f) hunk ./src/Blog/BackEnd/IoOperations.hs 126 -load_comment f = do { content <- readFile' $ C.comment_dir f - ; return $ Just . EP.item_from_string f $! content } - `SIE.catch` - \e -> do { errorM log_handle $ "Error loading comment content from " - ++ f ++ "; exception was: " ++ ( show e ) - ; return Nothing } +load_comment = load_with log_handle EP.item_from_string (\f -> C.comment_dir f) hunk ./src/Blog/BackEnd/IoOperations.hs 128 -load :: String -> FilePath -> IO (Maybe B.Item) -load lh f = do { content <- readFile' f - ; return $ Just . EP.item_from_string f $! content } - `SIE.catch` - \e -> do { errorM lh $ "Error loading content from " - ++ f ++ "; exception was: " ++ ( show e ) - ; return Nothing } +load :: String -> FilePath -> IO (Maybe B.Item) +load lh = load_with lh EP.item_from_string id hunk ./src/Blog/Model/EntryParser.hs 6 -item_from_string :: String -> String -> B.Item +item_from_string :: String -> String -> Either (String,String) B.Item hunk ./src/Blog/Model/EntryParser.hs 9 -draft_from_string :: String -> String -> B.Item +draft_from_string :: String -> String -> Either (String,String) B.Item hunk ./src/Blog/Model/EntryParser.hs 12 -fromString :: Parser B.Item -> String -> String -> B.Item +fromString :: Parser B.Item -> String -> String -> Either (String,String) B.Item hunk ./src/Blog/Model/EntryParser.hs 14 - Left err -> error $ show err - Right item -> item + Left err -> Left (path, show err) + Right item -> Right item hunk ./src/Blog/Model/EntryParser.hs 64 - ; manyTill anyChar (try $ string $ concat ["\n--- END ", s, " ---\n"]) } + ; manyTill anyChar (try $ end_block s) } + +end_block :: String -> Parser () +end_block s = do { string $ concat [ "\n--- END ", s, " ---" ] + ; many $ char '\n' + ; return () } }