Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 1 | %%% Copyright (c) 2007- Facebook |
| 2 | %%% Distributed under the Thrift Software License |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 3 | %%% |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 4 | %%% 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 Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 10 | -include("thrift.hrl"). |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 11 | -include("tException.hrl"). |
| 12 | |
| 13 | -behavior(oop). |
| 14 | |
| 15 | -export([attr/4, super/0, inspect/1]). |
| 16 | |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 17 | -export([new/2, add_backtrace_element/2, throw/2, inspect_with_backtrace/2, inspect_with_backtrace/3]). |
| 18 | |
| 19 | -export([read/1]). |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 20 | |
| 21 | %%% |
| 22 | %%% define attributes |
| 23 | %%% 'super' is required unless ?MODULE is a base class |
| 24 | %%% |
| 25 | |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 26 | ?DEFINE_ATTR(message); |
| 27 | ?DEFINE_ATTR(type); |
| 28 | ?DEFINE_ATTR(backtrace). |
| 29 | |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 30 | %%% |
| 31 | %%% behavior callbacks |
| 32 | %%% |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 33 | |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 34 | %%% super() -> SuperModule = atom() |
| 35 | %%% | none |
| 36 | |
| 37 | super() -> |
| 38 | none. |
| 39 | |
| 40 | %%% inspect(This) -> string() |
| 41 | |
| 42 | inspect(This) -> |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 43 | 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 Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 52 | |
| 53 | %%% |
| 54 | %%% class methods |
| 55 | %%% |
| 56 | |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 57 | new(Type, Message) -> |
| 58 | #?MODULE{type=Type, message=Message, backtrace=[]}. |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 59 | |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 60 | add_backtrace_element(E, Info) -> |
| 61 | BT = oop:get(E, backtrace), |
| 62 | E1 = oop:set(E, backtrace, [Info|BT]), |
| 63 | E1. |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 64 | |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 65 | throw(Class, Args) -> |
| 66 | E = apply(Class, new, Args), |
| 67 | exit({thrift_exception, E}). |
| 68 | |
| 69 | |
| 70 | inspect_with_backtrace(E, Where, Info) -> |
| 71 | E1 = add_backtrace_element(E, Info), |
| 72 | inspect_with_backtrace(E1, Where). |
| 73 | |
| 74 | inspect_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 | |
| 85 | read(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. |