Browse Source

implemented error catching, (not done yet: error throwing)

Roberto Saccon 17 years ago
parent
commit
e2a19abdb4
2 changed files with 35 additions and 44 deletions
  1. 16 32
      src/demo/erlydtl_demo.erl
  2. 19 12
      src/erlydtl/erlydtl_compiler.erl

+ 16 - 32
src/demo/erlydtl_demo.erl

@@ -34,7 +34,7 @@
 -author('rsaccon@gmail.com').
 
 %% API
--export([compile_all/0, compile/1, compile/2, render_all/0, render/1, preset/1]).
+-export([compile_all/0, compile/1, compile/2, render_all/0, render/1, render/2, preset/1]).
 
 %%====================================================================
 %% API
@@ -227,7 +227,20 @@ render(Name) ->
 %%--------------------------------------------------------------------
 render(Name, Args) ->
     OutDir = filename:join([filename:dirname(code:which(?MODULE)),"..", "demo", "out"]),
-    render2(OutDir, list_to_atom("test_" ++ Name), Args).
+    Module = list_to_atom("test_" ++ Name),
+    case catch Module:render(Args) of
+        {ok, Val} -> 
+            case file:open(filename:join([OutDir, lists:concat([Module, ".", Module:file_extension()])]), [write]) of
+                {ok, IoDev} ->
+                    file:write(IoDev, Val),
+                    file:close(IoDev),
+                    io:format("render success: ~p~n",[Module]);    
+                _ ->
+                    io:format("file writing failure: ~p~n",[Module])
+            end;
+        {error, Err} ->
+            io:format("render failure: ~p ~p~n",[Module, Err])
+    end.    
             
 
 %%--------------------------------------------------------------------
@@ -253,33 +266,4 @@ preset(test_for_records_preset) ->
               
 %%====================================================================
 %% Internal functions
-%%====================================================================
-
-render2(OutDir, Module, Arg) ->
-    case catch Module:render(Arg) of
-        {ok, Val, Warnings} -> 
-            write_file(OutDir, Module, Val, Warnings);
-        {error, Err, Warnings} ->
-            io:format("TRACE ~p:~p Errors: ~p~n",[?MODULE, ?LINE, Err]),
-            io:format("TRACE ~p:~p Warnings: ~p~n",[?MODULE, ?LINE, Warnings]);
-        {'EXIT', Reason} -> 
-            io:format("TRACE ~p:~p ~p: render failure: ~n",[?MODULE, ?LINE, Reason]);
-        Val -> %% only temporarly
-            write_file(OutDir, Module, Val, [])
-    end.
-    
-    
-write_file(OutDir, Module, Val, Warnings) ->
-    case file:open(filename:join([OutDir, lists:concat([Module, ".", Module:file_extension()])]), [write]) of
-        {ok, IoDev} ->
-            file:write(IoDev, Val),
-            file:close(IoDev),
-            case Warnings of
-                [] ->
-                    io:format("render success: ~p~n",[Module]);    
-                _ -> 
-                    io:format("render success: ~p - Warnings: ~p~n",[Module, Warnings])
-            end;
-        _ ->
-            io:format("render failure: ~p~n",[Module])
-    end.
+%%====================================================================

+ 19 - 12
src/erlydtl/erlydtl_compiler.erl

@@ -55,27 +55,34 @@ compile(File, DocRoot, Module, Function) ->
 
 compile(File, DocRoot, Module, Function, OutDir) ->
     case parse(File) of
-        {ok, DjangoAst} ->
-            Render1FunctionAst = erl_syntax:function(
-                erl_syntax:atom(Function), 
+        {ok, DjangoAst} ->                        
+            Function2 = erl_syntax:application(erl_syntax:atom(Module), erl_syntax:atom(Function ++ "2"),
+                [erl_syntax:variable("Variables")]),  
+            ClauseOk = erl_syntax:clause([erl_syntax:variable("Val")], none,
+                [erl_syntax:tuple([erl_syntax:atom(ok), erl_syntax:variable("Val")])]),     
+            ClauseCatch = erl_syntax:clause([erl_syntax:tuple([erl_syntax:atom(throw), 
+                erl_syntax:variable("'_'"), erl_syntax:variable("'_'")])], none,
+                    [erl_syntax:tuple([erl_syntax:atom(error), erl_syntax:string("error description")])]),                                              
+            Render1FunctionAst = erl_syntax:function(erl_syntax:atom(Function),
                 [erl_syntax:clause([erl_syntax:variable("Variables")], none, 
+                    [erl_syntax:try_expr([Function2], [ClauseOk], [ClauseCatch])])]),
+            Render0FunctionAst = erl_syntax:function(erl_syntax:atom(Function),
+                [erl_syntax:clause([], none, [erl_syntax:application(erl_syntax:atom(Module), 
+                    erl_syntax:atom(Function), [erl_syntax:list([])])])]),
+            RenderInternalFunctionAst = erl_syntax:function(
+                erl_syntax:atom(Function ++ "2"), 
+                    [erl_syntax:clause([erl_syntax:variable("Variables")], none, 
                         [body_ast(DjangoAst, #dtl_context{doc_root = DocRoot, parse_trail = [File]})])]),
-            Render0FunctionAst = erl_syntax:function(
-                erl_syntax:atom(Function),
-                [erl_syntax:clause([], none, 
-                        [erl_syntax:application(erl_syntax:atom(Module), erl_syntax:atom(Function),
-                                [erl_syntax:list([])])]
-                    )]),
             ExtensionFunctionAst = erl_syntax:function(
                 erl_syntax:atom(file_extension),
                 [erl_syntax:clause([], none, [erl_syntax:string(file_extension(File))])]),
             ModuleAst  = erl_syntax:attribute(erl_syntax:atom(module), [erl_syntax:atom(Module)]),
-            CompileAst = erl_syntax:attribute(erl_syntax:atom(compile), [erl_syntax:atom("export_all")]),
+            CompileAst = erl_syntax:attribute(erl_syntax:atom(compile), [erl_syntax:atom("export_all")]), % TODO: export only render/0, render/1
 
             Forms = [erl_syntax:revert(X) || X <- [ModuleAst, CompileAst, ExtensionFunctionAst, 
-                    Render0FunctionAst, Render1FunctionAst]],
+                Render0FunctionAst, Render1FunctionAst, RenderInternalFunctionAst]],
 
-            case compile:forms(Forms) of
+            case compile:forms(Forms, []) of
                 {ok, Module1, Bin} ->       
                     Path = filename:join([OutDir, atom_to_list(Module1) ++ ".beam"]),
                     case file:write_file(Path, Bin) of