-
Notifications
You must be signed in to change notification settings - Fork 20
/
dev.exs
301 lines (242 loc) · 7.32 KB
/
dev.exs
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# This is the development server for Errortracker built on the PhoenixLiveDashboard project.
# To start the development server run:
# $ iex dev.exs
#
Mix.install([
{:ecto_sqlite3, ">= 0.0.0"},
{:error_tracker, path: "."},
{:phoenix_playground, "~> 0.1.7"}
])
otp_app = :error_tracker_dev
Application.put_all_env(
error_tracker_dev: [
{ErrorTrackerDev.Repo, [database: "priv/repo/dev.db"]}
],
error_tracker: [
{:application, otp_app},
{:otp_app, otp_app},
{:repo, ErrorTrackerDev.Repo}
]
)
defmodule ErrorTrackerDev.Repo do
require Logger
use Ecto.Repo, otp_app: otp_app, adapter: Ecto.Adapters.SQLite3
defmodule Migration do
use Ecto.Migration
def up, do: ErrorTracker.Migration.up()
def down, do: ErrorTracker.Migration.down()
end
def migrate do
Ecto.Migrator.run(__MODULE__, [{0, __MODULE__.Migration}], :up, all: true)
end
end
defmodule ErrorTrackerDev.Controller do
use Phoenix.Controller, formats: [:html]
use Phoenix.Component
plug :put_layout, false
plug :put_view, __MODULE__
def index(conn, _params) do
render(conn)
end
def index(assigns) do
~H"""
<h2>ErrorTracker Dev server</h2>
<ul>
<li></li>
</ul>
"""
end
def noroute(conn, _params) do
ErrorTracker.add_breadcrumb("ErrorTrackerDev.Controller.noroute/2")
raise Phoenix.Router.NoRouteError, conn: conn, router: ErrorTrackerDev.Router
end
def exception(_conn, _params) do
ErrorTracker.add_breadcrumb("ErrorTrackerDev.Controller.exception/2")
raise ErrorTrackerDev.Exception,
message: "This is a controller exception",
bread_crumbs: ["First", "Second"]
end
def exit(_conn, _params) do
ErrorTracker.add_breadcrumb("ErrorTrackerDev.Controller.exit/2")
exit(:timeout)
end
end
defmodule ErrorTrackerDev.Live do
use Phoenix.LiveView
def mount(params, _session, socket) do
if params["crash_on_mount"] do
raise("Crashed on mount/3")
end
{:ok, socket}
end
def handle_params(params, _uri, socket) do
if params["crash_on_handle_params"] do
raise "Crashed on handle_params/3"
end
{:noreply, socket}
end
def handle_event("crash_on_handle_event", _params, _socket) do
raise "Crashed on handle_event/3"
end
def handle_event("crash_on_render", _params, socket) do
{:noreply, assign(socket, crash_on_render: true)}
end
def handle_event("genserver-timeout", _params, socket) do
GenServer.call(ErrorTrackerDev.GenServer, :timeout, 2000)
{:noreply, socket}
end
def render(assigns) do
if Map.has_key?(assigns, :crash_on_render) do
raise "Crashed on render/1"
end
~H"""
<h1>ErrorTracker Dev server</h1>
<.link href="/dev/errors" target="_blank">Open the ErrorTracker dashboard</.link>
<p>
Errors are stored in the <code>priv/repo/dev.db</code>
database, which is automatically created by this script.<br />
If you want to clear the state stop the script, run the following command and start it again. <pre>rm priv/repo/dev.db priv/repo/dev.db-shm priv/repo/dev.db-wal</pre>
</p>
<h2>LiveView examples</h2>
<ul>
<li>
<.link href="/?crash_on_mount">Crash on mount/3</.link>
</li>
<li>
<.link patch="/?crash_on_handle_params">Crash on handle_params/3</.link>
</li>
<li>
<.link phx-click="crash_on_render">Crash on render/1</.link>
</li>
<li>
<.link phx-click="crash_on_handle_event">Crash on handle_event/3</.link>
</li>
<li>
<.link phx-click="genserver-timeout">Crash with a GenServer timeout</.link>
</li>
</ul>
<h2>Controller examples</h2>
<ul>
<li>
<.link href="/noroute">Generate a 404 error from the controller</.link>
</li>
<li>
<.link href="/exception">Generate an exception from the controller</.link>
</li>
<li>
<.link href="/plug_exception">Generate an exception from the router</.link>
</li>
<li>
<.link href="/exit">Generate an exit from the controller</.link>
</li>
</ul>
"""
end
end
defmodule ErrorTrackerDev.Router do
use Phoenix.Router
use ErrorTracker.Web, :router
import Phoenix.LiveView.Router
pipeline :browser do
plug :accepts, [:html]
plug :put_root_layout, html: {PhoenixPlayground.Layout, :root}
plug :put_secure_browser_headers
end
scope "/" do
pipe_through :browser
live "/", ErrorTrackerDev.Live
get "/noroute", ErrorTrackerDev.Controller, :noroute
get "/exception", ErrorTrackerDev.Controller, :exception
get "/exit", ErrorTrackerDev.Controller, :exit
scope "/dev" do
error_tracker_dashboard "/errors", csp_nonce_assign_key: :custom_csp_nonce
end
end
end
defmodule ErrorTrackerDev.Endpoint do
use Phoenix.Endpoint, otp_app: :phoenix_playground
use ErrorTracker.Integrations.Plug
# Default PhoenixPlayground.Endpoint
plug Plug.Logger
socket "/live", Phoenix.LiveView.Socket
plug Plug.Static, from: {:phoenix, "priv/static"}, at: "/assets/phoenix"
plug Plug.Static, from: {:phoenix_live_view, "priv/static"}, at: "/assets/phoenix_live_view"
socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket
plug Phoenix.LiveReloader
plug Phoenix.CodeReloader, reloader: &PhoenixPlayground.CodeReloader.reload/2
# Use a custom Content Security Policy
plug :set_csp
# Raise an exception in the /plug_exception path
plug :plug_exception
# Our custom router which allows us to have regular controllers and live views
plug ErrorTrackerDev.Router
defp set_csp(conn, _opts) do
nonce = 10 |> :crypto.strong_rand_bytes() |> Base.encode64()
policies = [
"script-src 'self' 'nonce-#{nonce}';",
"style-src 'self' 'nonce-#{nonce}';"
]
conn
|> Plug.Conn.assign(:custom_csp_nonce, "#{nonce}")
|> Plug.Conn.put_resp_header("content-security-policy", Enum.join(policies, " "))
end
defp plug_exception(conn = %Plug.Conn{path_info: path_info}, _opts) when is_list(path_info) do
if "plug_exception" in path_info,
do: raise("Crashed in Endpoint"),
else: conn
end
end
defmodule ErrorTrackerDev.ErrorView do
def render("404.html", _assigns) do
"This is a 404"
end
def render("500.html", _assigns) do
"This is a 500"
end
end
defmodule ErrorTrackerDev.GenServer do
use GenServer
# Client
def start_link(_) do
GenServer.start_link(__MODULE__, %{})
end
# Server (callbacks)
@impl true
def init(initial_state) do
{:ok, initial_state}
end
@impl true
def handle_call(:timeout, _from, state) do
:timer.sleep(5000)
{:reply, state, state}
end
end
defmodule ErrorTrackerDev.Exception do
defexception [:message, :bread_crumbs]
end
defmodule ErrorTrackerDev.Telemetry do
def handle_event(event, measure, metadata, _opts) do
dbg([event, measure, metadata])
end
end
PhoenixPlayground.start(
endpoint: ErrorTrackerDev.Endpoint,
child_specs: [
{ErrorTrackerDev.Repo, []},
{ErrorTrackerDev.GenServer, [name: ErrorTrackerDev.GenServer]}
],
open_browser: false,
debug_errors: false
)
ErrorTrackerDev.Repo.migrate()
:telemetry.attach_many(
"error-tracker-events",
[
[:error_tracker, :error, :new],
[:error_tracker, :error, :resolved],
[:error_tracker, :error, :unresolved],
[:error_tracker, :occurrence, :new]
],
&ErrorTrackerDev.Telemetry.handle_event/4,
[]
)