PageRenderTime 47ms CodeModel.GetById 15ms app.highlight 11ms RepoModel.GetById 19ms app.codeStats 0ms

/src/error.erl

https://github.com/3lectrologos/Concuerror
Erlang | 71 lines | 43 code | 15 blank | 13 comment | 0 complexity | 2a5d06bad572da96cb414378bf90d8cc MD5 | raw file
 1%%%----------------------------------------------------------------------
 2%%% Copyright (c) 2011, Alkis Gotovos <el3ctrologos@hotmail.com>,
 3%%%                     Maria Christakis <mchrista@softlab.ntua.gr>
 4%%%                 and Kostis Sagonas <kostis@cs.ntua.gr>.
 5%%% All rights reserved.
 6%%%
 7%%% This file is distributed under the Simplified BSD License.
 8%%% Details can be found in the LICENSE file.
 9%%%----------------------------------------------------------------------
10%%% Authors     : Alkis Gotovos <el3ctrologos@hotmail.com>
11%%%               Maria Christakis <mchrista@softlab.ntua.gr>
12%%% Description : Error interface
13%%%----------------------------------------------------------------------
14
15-module(error).
16
17-export([long/1, mock/0, new/1, short/1, type/1]).
18
19-export_type([error/0]).
20
21-include("gen.hrl").
22
23-type error_type()  :: 'assertion_violation' | 'deadlock' | 'exception'.
24-type error()       :: {error_type(), term()}.
25
26-spec new(term()) -> error().
27
28new({deadlock, _Set} = Deadlock) -> Deadlock;
29new({{assertion_failed, Details}, _Any}) -> {assertion_violation, Details};
30new({{assertEqual_failed, Details}, _Any}) -> {assertion_violation, Details};
31new(Reason) -> {exception, Reason}.
32
33-spec type(error()) -> nonempty_string().
34
35type({deadlock, _Blocked}) -> "Deadlock";
36type({assertion_violation, _Details}) -> "Assertion violation";
37type({exception, _Details}) -> "Exception".
38
39-spec short(error()) -> nonempty_string().
40
41short({deadlock, Blocked}) ->
42    OldList = lists:sort(?SETS:to_list(Blocked)),
43    {List, [Last]} = lists:split(length(OldList) - 1, OldList),
44    Fun = fun(L, A) -> A ++ lid:to_string(L) ++ ", " end,
45    lists:foldl(Fun, "", List) ++ lid:to_string(Last);
46short({assertion_violation, [{module, Module}, {line, Line}|_Rest]}) ->
47    util:flat_format("~p.erl:~p", [Module, Line]);
48short({exception, Reason}) ->
49    lists:flatten(io_lib:format("~W", [Reason, 3])).
50
51-spec long(error()) -> nonempty_string().
52
53long({deadlock, _Blocked} = Error) ->
54    Format = "Error type        : Deadlock~n"
55	     "Blocked processes : ~s",
56    util:flat_format(Format, [short(Error)]);
57long({assertion_violation,
58      [{module, Module}, {line, Line}, _Xpr, {expected, Exp}, {value, Val}]}) ->
59    Format = "Error type        : Assertion violation~n"
60	     "Module:Line       : ~p.erl:~p~n"
61	     "Expected          : ~p~n"
62	     "Value             : ~p",
63    util:flat_format(Format, [Module, Line, Exp, Val]);
64long({exception, Details}) ->
65    Format = "Error type        : Exception~n"
66             "Details           : ~p",
67    util:flat_format(Format, [Details]).
68
69-spec mock() -> {'exception', 'foobar'}.
70
71mock() -> {exception, foobar}.