-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy patherror_test.exs
More file actions
176 lines (125 loc) · 5.8 KB
/
Copy patherror_test.exs
File metadata and controls
176 lines (125 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
defmodule ErrorTest do
use ExUnit.Case
import ExUnit.CaptureLog
alias NewRelic.Harvest.Collector
defmodule ErrorDummy do
use GenServer
def init(args), do: {:ok, args}
def start_link, do: GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
@compile {:no_warn_undefined, Not}
def handle_call(:nofun, _from, _state), do: Not.a_function(:one, :two)
def handle_call(:secretfun, _from, _state), do: Not.secretfun("secretvalue", "other_secret")
def handle_call(:sleep, _from, _state), do: :timer.sleep(:infinity)
def handle_call(:raise, _from, _state), do: raise("ERROR")
def handle_call(:erlang_error, _from, _state), do: :erlang.error(:badarg)
end
test "Catch and harvest errors" do
Process.flag(:trap_exit, true)
TestHelper.restart_harvest_cycle(Collector.TransactionErrorEvent.HarvestCycle)
ErrorDummy.start_link()
capture_log(fn ->
catch_exit(GenServer.call(ErrorDummy, :nofun))
end)
events = TestHelper.gather_harvest(Collector.TransactionErrorEvent.Harvester)
assert TestHelper.find_event(events, %{"error.class": "UndefinedFunctionError"})
end
test "Redact arguments when not gathering stacktrace args" do
Process.flag(:trap_exit, true)
TestHelper.restart_harvest_cycle(Collector.TransactionErrorEvent.HarvestCycle)
TestHelper.restart_harvest_cycle(Collector.ErrorTrace.HarvestCycle)
ErrorDummy.start_link()
TestHelper.run_with(:nr_features, stacktrace_argument_collection: false)
capture_log(fn ->
catch_exit(GenServer.call(ErrorDummy, :secretfun))
end)
[[_, event_error, _]] = TestHelper.gather_harvest(Collector.TransactionErrorEvent.Harvester)
[[_, _, _, _, trace_error, _]] = TestHelper.gather_harvest(Collector.ErrorTrace.Harvester)
refute String.contains?(event_error.stacktrace, "secretvalue")
refute String.contains?(event_error.stacktrace, "other_secret")
assert String.contains?(event_error.stacktrace, "Not.secretfun(\"DISABLED (arity: 2)\")")
refute String.contains?(List.first(trace_error.stack_trace), "secretvalue")
refute String.contains?(List.first(trace_error.stack_trace), "other_secret")
assert String.contains?(
List.first(trace_error.stack_trace),
"Not.secretfun(\"DISABLED (arity: 2)\")"
)
end
test "Catch a raised Error" do
Process.flag(:trap_exit, true)
TestHelper.restart_harvest_cycle(Collector.TransactionErrorEvent.HarvestCycle)
ErrorDummy.start_link()
capture_log(fn ->
catch_exit(GenServer.call(ErrorDummy, :raise))
end)
events = TestHelper.gather_harvest(Collector.TransactionErrorEvent.Harvester)
assert TestHelper.find_event(events, %{"error.message": "(RuntimeError) ERROR"})
end
test "Catch a GenServer timeout error" do
TestHelper.restart_harvest_cycle(Collector.TransactionErrorEvent.HarvestCycle)
ErrorDummy.start_link()
:proc_lib.spawn(fn ->
GenServer.call(ErrorDummy, :sleep, 50)
end)
:timer.sleep(100)
events = TestHelper.gather_harvest(Collector.TransactionErrorEvent.Harvester)
assert TestHelper.find_event(events, %{"error.class": "EXIT", "error.message": "(GenServer.call/3) :timeout"})
end
test "Catch an erlang :badarg error" do
TestHelper.restart_harvest_cycle(Collector.TransactionErrorEvent.HarvestCycle)
:proc_lib.spawn(fn ->
:erlang.error(:badarg)
end)
:timer.sleep(100)
events = TestHelper.gather_harvest(Collector.TransactionErrorEvent.Harvester)
assert TestHelper.find_event(events, %{"error.class": "ArgumentError"})
end
test "Handle Erlang exception when stacktrace arg colleection is turned off" do
Process.flag(:trap_exit, true)
TestHelper.restart_harvest_cycle(Collector.TransactionErrorEvent.HarvestCycle)
TestHelper.restart_harvest_cycle(Collector.ErrorTrace.HarvestCycle)
ErrorDummy.start_link()
TestHelper.run_with(:nr_features, stacktrace_argument_collection: false)
capture_log(fn ->
catch_exit(GenServer.call(ErrorDummy, :erlang_error))
end)
[[_, event_error, _]] = TestHelper.gather_harvest(Collector.TransactionErrorEvent.Harvester)
assert String.contains?(event_error.stacktrace, "init(\"DISABLED (arity: 1)\")")
end
test "Catch a simple raise" do
TestHelper.restart_harvest_cycle(Collector.TransactionErrorEvent.HarvestCycle)
:proc_lib.spawn(fn ->
raise "RAISE"
end)
:timer.sleep(100)
events = TestHelper.gather_harvest(Collector.TransactionErrorEvent.Harvester)
assert TestHelper.find_event(events, %{"error.message": "(RuntimeError) RAISE"})
end
test "Nicely format EXIT when it's an exception struct" do
TestHelper.restart_harvest_cycle(Collector.TransactionErrorEvent.HarvestCycle)
:proc_lib.spawn(fn ->
exit(%RuntimeError{message: "foo"})
end)
:timer.sleep(100)
events = TestHelper.gather_harvest(Collector.TransactionErrorEvent.Harvester)
assert TestHelper.find_event(events, %{"error.class": "RuntimeError", "error.message": "(RuntimeError) foo"})
end
test "Catch a file error" do
TestHelper.restart_harvest_cycle(Collector.TransactionErrorEvent.HarvestCycle)
:proc_lib.spawn(fn ->
File.read!("no_file")
end)
:timer.sleep(100)
events = TestHelper.gather_harvest(Collector.TransactionErrorEvent.Harvester)
assert TestHelper.find_event(events, %{"error.class": "File.Error"})
end
@tag :capture_log
test "Catch a function clause error inside a Task" do
TestHelper.restart_harvest_cycle(Collector.TransactionErrorEvent.HarvestCycle)
:proc_lib.spawn(fn ->
Task.async(fn -> ErrorDummy.handle_call(:foo, nil, nil) end) |> Task.await()
end)
:timer.sleep(100)
events = TestHelper.gather_harvest(Collector.TransactionErrorEvent.Harvester)
assert TestHelper.find_event(events, %{"error.class": "FunctionClauseError"})
end
end