Browse Source

better error handling

Namdak Tonpa 6 years ago
parent
commit
dc943ff695
7 changed files with 30 additions and 30 deletions
  1. 1 1
      include/mad.hrl
  2. BIN
      mad
  3. 6 6
      src/compile/mad_compile.erl
  4. 8 8
      src/mad.erl
  5. 1 1
      src/mad_resolve.erl
  6. 10 9
      src/mad_static.erl
  7. 4 5
      src/sources/mad_git.erl

+ 1 - 1
include/mad.hrl

@@ -1 +1 @@
--define(VERSION,"2b8e56").
+-define(VERSION,"ad426a").

BIN
mad


+ 6 - 6
src/compile/mad_compile.erl

@@ -8,9 +8,9 @@ compile(Params) ->
          [] -> mad_compile:'compile-deps'(Cwd, ConfigFile, Conf);
          [] -> mad_compile:'compile-deps'(Cwd, ConfigFile, Conf);
          __ -> mad_compile:deps(Cwd, Conf, ConfigFile, [Params])
          __ -> mad_compile:deps(Cwd, Conf, ConfigFile, [Params])
     end,
     end,
-    case bool(Res) of
-         true -> {error,Params};
-         false -> mad_compile:'compile-apps'(Cwd, ConfigFile, Conf) end.
+    case Res of
+         {error,Reason} -> {error,Reason};
+         {ok,_} -> mad_compile:'compile-apps'(Cwd, ConfigFile, Conf) end.
 
 
 deps(_, _, _, []) -> {ok,deps};
 deps(_, _, _, []) -> {ok,deps};
 deps(Cwd, Conf, ConfigFile, [H|T]) ->
 deps(Cwd, Conf, ConfigFile, [H|T]) ->
@@ -18,9 +18,9 @@ deps(Cwd, Conf, ConfigFile, [H|T]) ->
     Res = case get(Name) == compiled of
     Res = case get(Name) == compiled of
           true -> {ok,[]};
           true -> {ok,[]};
           _    -> dep(Cwd, Conf, ConfigFile, Name) end,
           _    -> dep(Cwd, Conf, ConfigFile, Name) end,
-    case bool(Res) of
-         true  -> {error,Name};
-         false -> deps(Cwd, Conf, ConfigFile, T) end.
+    case Res of
+         {error,Reason}  -> {error,Reason};
+         {ok,_} -> deps(Cwd, Conf, ConfigFile, T) end.
 
 
 bool({ok,_})    -> false;
 bool({ok,_})    -> false;
 bool({error,_}) -> true.
 bool({error,_}) -> true.

+ 8 - 8
src/mad.erl

@@ -16,7 +16,7 @@ main(Params)      ->
     % return any error if exists on flattened fold of profile runs
     % return any error if exists on flattened fold of profile runs
     % of functions (commands) with arguments
     % of functions (commands) with arguments
     halt(return(
     halt(return(
-         lists:any(fun({error,_}) -> true; (_) -> false end,
+         lists:any(fun({error,X}) -> mad:info("~s~n",[X]), true; (_) -> false end,
          lists:flatten([
          lists:flatten([
          lists:foldl(
          lists:foldl(
          fun ({Fun,Arg},ErrAcc) ->
          fun ({Fun,Arg},ErrAcc) ->
@@ -51,8 +51,8 @@ profile()         -> application:get_env(mad,profile,mad_local).
 
 
 errors([])            -> [];
 errors([])            -> [];
 errors(false)         -> [];
 errors(false)         -> [];
-errors(true)          -> [{error,unknown}];
-errors({error,Where}) -> info("ERROR~n"), [{error,Where}];
+errors(true)          -> [{error,"Unknown."}];
+errors({error,What})  -> info("ERROR~n"), [{error,What}];
 errors({ok,_})        -> info("OK~n",[]), [].
 errors({ok,_})        -> info("OK~n",[]), [].
 
 
 return(true)      -> 1;
 return(true)      -> 1;
@@ -65,11 +65,11 @@ info(Format,Args) -> io:format(lists:concat([Format,"\r"]),Args).
 
 
 help(Reason,D)    -> help(io_lib:format("~s ~p", [Reason, D])).
 help(Reason,D)    -> help(io_lib:format("~s ~p", [Reason, D])).
 help(_Msg)        -> help().
 help(_Msg)        -> help().
-help()            -> info("MAD Container Tool version ~s~n",[?VERSION]),
+help()            -> info("MAD Manage Dependencies ~s~n",[?VERSION]),
                      info("~n"),
                      info("~n"),
                      info("    invoke = mad | mad params~n"),
                      info("    invoke = mad | mad params~n"),
-                     info("    params = command [ options ] params ~n"),
-                     info("   command = app     | deps  | clean | compile | up   | eunit  | strip~n"),
-                     info("           | bundle  [ beam  | ling  | script  | runc | depot  ]~n"),
-                     info("           | deploy  | start | stop  | attach  | sh   | static [ <watch|min> ] ~n"),
+                     info("    params = []  | command [options] params ~n"),
+                     info("   command = app <sample> | deps | clean | compile | strip~n"),
+                     info("           | bundle [beam|script] | get repo | up [name] ~n"),
+                     info("           | start | stop | attach | sh | static [watch|min] ~n"),
                      return(false).
                      return(false).

+ 1 - 1
src/mad_resolve.erl

@@ -35,7 +35,7 @@ orderapps() ->
     Apps = triples(),
     Apps = triples(),
     case sort(lists:flatten(Apps)) of
     case sort(lists:flatten(Apps)) of
          {ok,Sorted} -> {ok,Sorted};
          {ok,Sorted} -> {ok,Sorted};
-         Return -> {error,{cycling_apps,Return}} end.
+         Return -> {error,"Cycling apps."} end.
 
 
 system_deps(A) ->
 system_deps(A) ->
     F = code:where_is_file(lists:concat([A,".app"])),
     F = code:where_is_file(lists:concat([A,".app"])),

+ 10 - 9
src/mad_static.erl

@@ -4,16 +4,15 @@
 -define(NODE(Bin), "node_modules/.bin/"++Bin).
 -define(NODE(Bin), "node_modules/.bin/"++Bin).
 
 
 main(_Config, ["min"]) ->
 main(_Config, ["min"]) ->
-    {ok,[SysConfig]} = file:consult("sys.config"),
+    SysConfig = try {ok,[S]} = file:consult("sys.config"), S catch _:_ -> [] end,
     N2O     = proplists:get_value(n2o,SysConfig,[]),
     N2O     = proplists:get_value(n2o,SysConfig,[]),
     AppName = proplists:get_value(app,N2O,sample),
     AppName = proplists:get_value(app,N2O,sample),
-    Minify  = proplists:get_value(minify,N2O,[]),
+    Minify  = proplists:get_value(minify,N2O,{[],[]}),
     Command = lists:concat(["uglify -s ",string:join(element(2,Minify),","),
     Command = lists:concat(["uglify -s ",string:join(element(2,Minify),","),
                                  " -o ",element(1,Minify),"/",AppName,".min.js"]),
                                  " -o ",element(1,Minify),"/",AppName,".min.js"]),
-    io:format("Minify: ~p~n",[Command]),
     case sh:run(Command) of
     case sh:run(Command) of
          {_,0,_} -> {ok,static};
          {_,0,_} -> {ok,static};
-         {_,_,_} -> mad:info("minifyjs not installed. try `npm install -g uglify`~n"), {error,minifier}
+         {_,_,_} -> mad:info("minifyjs not installed. try `npm install -g uglify`~n"), {error,"Minifier."}
     end;
     end;
 
 
 main(Config, ["watch"]) ->
 main(Config, ["watch"]) ->
@@ -37,7 +36,7 @@ install_deps() ->
         _ ->
         _ ->
             case sh:oneliner("npm install mincer-erl") of
             case sh:oneliner("npm install mincer-erl") of
                 {_,0,_} -> {ok,static};
                 {_,0,_} -> {ok,static};
-                {_,_,_} -> mad:info("error while installing mincer-erl~n"), {error,install}
+                {_,_,_} -> mad:info("error while installing mincer-erl~n"), {error,"Static install."}
             end
             end
     end.
     end.
 
 
@@ -47,24 +46,26 @@ serve_static(Port) ->
     Res = sh:oneliner([?NODE("mincer-erl-serve"), "-p " ++ PortStr]),
     Res = sh:oneliner([?NODE("mincer-erl-serve"), "-p " ++ PortStr]),
     case Res of
     case Res of
         {_,0,_} -> {ok,static};
         {_,0,_} -> {ok,static};
-        {_,_,_} -> mad:info("error while serving assets~n"), {error,assests} end.
+        {_,_,_} -> mad:info("error while serving assets~n"), {error,"Static assests."} end.
 
 
 compile_static(Files) ->
 compile_static(Files) ->
     Res = sh:oneliner([?NODE("mincer-erl-compile")] ++ Files),
     Res = sh:oneliner([?NODE("mincer-erl-compile")] ++ Files),
     case Res of
     case Res of
         {_,0,_} -> {ok,static};
         {_,0,_} -> {ok,static};
-        {_,_,_} -> mad:info("error while compiling assets~n"), {error,compile} end.
+        {_,_,_} -> mad:info("error while compiling assets~n"), {error,"Static compile."} end.
 
 
 app([]) -> app(["sample"]);
 app([]) -> app(["sample"]);
 app(Params) ->
 app(Params) ->
     [Name] = Params,
     [Name] = Params,
     mad_repl:load(),
     mad_repl:load(),
     Apps = ets:tab2list(filesystem),
     Apps = ets:tab2list(filesystem),
+    try
     [ case string:str(File,"priv/web") of
     [ case string:str(File,"priv/web") of
-       1 -> Relative = unicode:characters_to_list(Name ++ string:replace(string:substr(File, 9), "sample", Name, all), utf8),
+       1 -> Relative = unicode:characters_to_list(Name ++
+                       string:replace(string:substr(File, 9), "sample", Name, all), utf8),
             mad:info("Create File: ~p~n",[Relative]),
             mad:info("Create File: ~p~n",[Relative]),
             filelib:ensure_dir(Relative),
             filelib:ensure_dir(Relative),
             BinNew = string:replace(Bin, "sample", Name, all),
             BinNew = string:replace(Bin, "sample", Name, all),
             file:write_file(Relative, BinNew);
             file:write_file(Relative, BinNew);
        _ -> skip
        _ -> skip
-       end || {File,Bin} <- Apps, is_list(File) ], {ok,Name}.
+       end || {File,Bin} <- Apps, is_list(File) ], {ok,Name} catch _:_ -> {error,"Skeleton failed."} end.

+ 4 - 5
src/sources/mad_git.erl

@@ -30,16 +30,17 @@ fetch(Cwd, Config, ConfigFile, [H|T]) ->
          {error,E} -> {error,E};
          {error,E} -> {error,E};
          {ok,_} -> fetch(Cwd, Config, ConfigFile, T) end.
          {ok,_} -> fetch(Cwd, Config, ConfigFile, T) end.
 
 
+get_repo([]) -> {error,"Repository unspecified."};
 get_repo([Name|_]) ->
 get_repo([Name|_]) ->
     { Cwd, File, Conf } = mad_utils:configs(),
     { Cwd, File, Conf } = mad_utils:configs(),
     Res = case string:tokens(Name,"/") of
     Res = case string:tokens(Name,"/") of
          [Org, Rep] -> {ok,Rep,lists:concat(["https://github.com/",Org,"/",Rep])};
          [Org, Rep] -> {ok,Rep,lists:concat(["https://github.com/",Org,"/",Rep])};
          [Rep] -> {ok,Rep,lists:concat(["https://github.com/synrc/",Rep])};
          [Rep] -> {ok,Rep,lists:concat(["https://github.com/synrc/",Rep])};
-         [] -> {error,"Repository unspecified."}
+         [] -> {error,"Repository invalid."}
     end,
     end,
     case Res of
     case Res of
          {error,X} -> {error,X};
          {error,X} -> {error,X};
-         {ok,N,Uri} -> fetch_dep(Cwd,Conf,File,N,"git",Uri,[],deps_fetch), {ok,N}
+         {ok,N,Uri} -> fetch_dep(Cwd,Conf,File,N,"git",Uri,[],deps_fetch)
     end.
     end.
 
 
 git_clone(Uri,Fast,TrunkPath,Rev) when Rev == "head"   orelse Rev == "HEAD"
 git_clone(Uri,Fast,TrunkPath,Rev) when Rev == "head"   orelse Rev == "HEAD"
@@ -87,8 +88,7 @@ fetch_dep(Cwd, Config, ConfigFile, Name, Cmd, Uri, Co, Cache) ->
                          CacheDir -> build_dep(Cwd, Config, ConfigFile,
                          CacheDir -> build_dep(Cwd, Config, ConfigFile,
                                         get_publisher(Uri), Name, Cmd, Co1, CacheDir)
                                         get_publisher(Uri), Name, Cmd, Co1, CacheDir)
                     end;
                     end;
-    {_,_,FetchError} -> mad:info("Fetch Error: ~s~n",[binary_to_list(FetchError)]),
-                        {error,binary_to_list(FetchError)} end.
+    {_,_,FetchError} -> {error,FetchError} end.
 
 
 %% build dependency based on branch/tag/commit
 %% build dependency based on branch/tag/commit
 build_dep(Cwd, Conf, _ConfFile, Publisher, Name, _Cmd, _Co, Dir) ->
 build_dep(Cwd, Conf, _ConfFile, Publisher, Name, _Cmd, _Co, Dir) ->
@@ -108,7 +108,6 @@ pull(_,[])         -> {ok,[]};
 pull(Config,[F|T]) ->
 pull(Config,[F|T]) ->
     mad:info("==> up: ~p~n", [F]),
     mad:info("==> up: ~p~n", [F]),
     {_,Status,Message} = sh:run(lists:concat(["cd ",F," && git pull && cd -"])),
     {_,Status,Message} = sh:run(lists:concat(["cd ",F," && git pull && cd -"])),
-    %mad:info("status: ~p~n",[{Status,Message}]),
     case Status of
     case Status of
          0 -> mad_utils:verbose(Config,Message), pull(Config,T);
          0 -> mad_utils:verbose(Config,Message), pull(Config,T);
          _ -> case binary:match(Message,[<<"You are not currently on a branch">>]) of
          _ -> case binary:match(Message,[<<"You are not currently on a branch">>]) of