Skip to content

Commit 1646ec5

Browse files
committed
WIP: pretty print context in client
1 parent 0df2d9b commit 1646ec5

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

assets/js/app.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,35 @@ let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("
55
let livePath = document.querySelector("meta[name='live-path']").getAttribute("content");
66
let liveTransport = document .querySelector("meta[name='live-transport']") .getAttribute("content");
77

8+
const Hooks = {
9+
JsonPrettyPrint: {
10+
mounted() {
11+
this.formatJson();
12+
},
13+
updated() {
14+
this.formatJson();
15+
},
16+
formatJson() {
17+
try {
18+
// Get the raw JSON content
19+
const rawJson = this.el.textContent.trim();
20+
// Parse and stringify with indentation
21+
const formattedJson = JSON.stringify(JSON.parse(rawJson), null, 2);
22+
// Update the element content
23+
this.el.textContent = formattedJson;
24+
} catch (error) {
25+
console.error("Error formatting JSON:", error);
26+
// Keep the original content if there's an error
27+
}
28+
}
29+
}
30+
};
31+
832
let liveSocket = new LiveView.LiveSocket(livePath, Phoenix.Socket, {
933
transport: liveTransport === "longpoll" ? Phoenix.LongPoll : WebSocket,
1034
params: { _csrf_token: csrfToken },
35+
hooks: Hooks
36+
1137
});
1238

1339
// Show progress bar on live navigation and form submits

lib/error_tracker/web/live/show.html.heex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@
7979
</.section>
8080

8181
<.section title="Context">
82-
<pre class="overflow-auto text-sm p-4 rounded-lg bg-gray-300/10 border border-gray-900">
82+
<pre
83+
id="context"
84+
class="overflow-auto text-sm p-4 rounded-lg bg-gray-300/10 border border-gray-900"
85+
phx-hook="JsonPrettyPrint"
86+
>
8387
<%= ErrorTracker.__default_json_encoder__().encode_to_iodata!(@occurrence.context) %>
8488
</pre>
8589
</.section>

priv/static/app.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)