blob: 6dd308440a36a041e8fa6ff5ef02dcedfba0731d [file] [log] [blame]
Christopher Piro094823a2007-07-18 00:26:12 +00001%%% Copyright (c) 2007- Facebook
2%%% Distributed under the Thrift Software License
Christopher Pirode11d852007-11-18 02:10:20 +00003%%%
Christopher Piro094823a2007-07-18 00:26:12 +00004%%% See accompanying file LICENSE or visit the Thrift site at:
5%%% http://developers.facebook.com/thrift/
6
7-module(tException).
8
9-include("oop.hrl").
Christopher Pirode11d852007-11-18 02:10:20 +000010-include("thrift.hrl").
Christopher Piro094823a2007-07-18 00:26:12 +000011-include("tException.hrl").
12
13-behavior(oop).
14
15-export([attr/4, super/0, inspect/1]).
16
Christopher Pirode11d852007-11-18 02:10:20 +000017-export([new/2, add_backtrace_element/2, throw/2, inspect_with_backtrace/2, inspect_with_backtrace/3]).
18
19-export([read/1]).
Christopher Piro094823a2007-07-18 00:26:12 +000020
21%%%
22%%% define attributes
23%%% 'super' is required unless ?MODULE is a base class
24%%%
25
Christopher Pirode11d852007-11-18 02:10:20 +000026?DEFINE_ATTR(message);
27?DEFINE_ATTR(type);
28?DEFINE_ATTR(backtrace).
29
Christopher Piro094823a2007-07-18 00:26:12 +000030%%%
31%%% behavior callbacks
32%%%
Christopher Pirode11d852007-11-18 02:10:20 +000033
Christopher Piro094823a2007-07-18 00:26:12 +000034%%% super() -> SuperModule = atom()
35%%% | none
36
37super() ->
38 none.
39
40%%% inspect(This) -> string()
41
42inspect(This) ->
Christopher Pirode11d852007-11-18 02:10:20 +000043 BT = ?ATTR(backtrace),
44 Depth =
45 if
46 is_list(BT) -> integer_to_list(length(BT));
47 true -> "?"
48 end,
49 ?FORMAT_ATTR(message) ++ ", " ++
50 ?FORMAT_ATTR(type) ++ ", "
51 " backtrace:" ++ Depth.
Christopher Piro094823a2007-07-18 00:26:12 +000052
53%%%
54%%% class methods
55%%%
56
Christopher Pirode11d852007-11-18 02:10:20 +000057new(Type, Message) ->
58 #?MODULE{type=Type, message=Message, backtrace=[]}.
Christopher Piro094823a2007-07-18 00:26:12 +000059
Christopher Pirode11d852007-11-18 02:10:20 +000060add_backtrace_element(E, Info) ->
61 BT = oop:get(E, backtrace),
62 E1 = oop:set(E, backtrace, [Info|BT]),
63 E1.
Christopher Piro094823a2007-07-18 00:26:12 +000064
Christopher Pirode11d852007-11-18 02:10:20 +000065throw(Class, Args) ->
66 E = apply(Class, new, Args),
67 exit({thrift_exception, E}).
68
69
70inspect_with_backtrace(E, Where, Info) ->
71 E1 = add_backtrace_element(E, Info),
72 inspect_with_backtrace(E1, Where).
73
74inspect_with_backtrace(E, Where) ->
75 thrift_utils:sformat("** ~s~n** ~s", [Where, oop:inspect(E)]) ++
76 case oop:get(E, backtrace) of
77 [] ->
78 "";
79 BT when is_list(BT) ->
80 thrift_utils:sformat("~n** trace = ~p", [lists:reverse(BT)]);
81 Else ->
82 thrift_utils:sformat("<ERROR BT NOT A LIST = ~p>", [Else])
83 end.
84
85read(E) ->
86 case oop:class(E) of
87 none ->
88 none;
89 Class ->
90 Type = oop:get(E, type),
91 BT = oop:get(E, backtrace),
92 {Class, Type, BT}
93 end.