refactor: ap error recording to also capture stack trace, prettify json on output

This commit is contained in:
Julian Lam
2026-04-08 15:17:46 -04:00
parent 621aaa0f35
commit 098ee291b4
4 changed files with 23 additions and 8 deletions

View File

@@ -494,13 +494,16 @@ ActivityPub.record.send = async ({ type, target }) => {
]);
};
ActivityPub.record.receiptError = async (body) => {
ActivityPub.record.receiptError = async (body, error) => {
const { id, actor } = body;
const now = Date.now();
const { hostname } = new URL(actor);
await Promise.all([
db.sortedSetAdd('ap.errors', now, id),
db.set(`ap.errors:${id}`, JSON.stringify(body)),
db.setObject(`ap.errors:${id}`, {
body: JSON.stringify(body),
stack: error.stack,
}),
analytics.increment(['ap.inErr', `ap.inErr:byHost:${hostname}`]),
]);
await db.expire(`ap.errors:${id}`, 60 * 60 * 24); // 24 hours

View File

@@ -272,7 +272,7 @@ Controller.postInbox = async (req, res) => {
await activitypub.record.receipt(req.body);
await helpers.formatApiResponse(202, res);
} catch (e) {
activitypub.record.receiptError(req.body);
activitypub.record.receiptError(req.body, e);
if (req.body?.type && req.body?.object && req.body?.actor) {
activitypub.inbox._reject(req.body.type, req.body.object, req.body.actor);
} else {

View File

@@ -88,9 +88,16 @@ federationController.analytics = async function (req, res) {
federationController.errors = async function (req, res) {
const ids = await db.getSortedSetRevRangeByScore('ap.errors', 0, -1, Date.now(), '-inf');
const payloads = await db.mget(ids.map(id => `ap.errors:${id}`));
const errorObj = await db.getObjects(ids.map(id => `ap.errors:${id}`));
const errors = ids.map((id, idx) => {
return { id, payload: payloads[idx] };
let { body, stack } = errorObj[idx];
try {
body = JSON.stringify(JSON.parse(body), null, 4);
} catch (e) {
// noop
}
return { id, body, stack };
});
res.render('admin/federation/errors', {

View File

@@ -6,10 +6,15 @@
<div class="mb-4">
<p class="lead">[[admin/settings/activitypub:errors.intro]]</p>
{{{ each errors }}}
<details>
<details class="mb-3">
<summary>{./id}</summary>
{{{ if ./payload }}}
<pre class="m-2 border p-2"><code>{./payload}</code></pre>
{{{ if ./stack }}}
<pre class="m-2 border p-2"><code>{./stack}</code></pre>
{{{ end }}}
{{{ if ./body }}}
<pre class="m-2 border p-2"><code>{./body}</code></pre>
{{{ else }}}
<em>[[admin/settings/activitypub:errors.payload-gone]]</em>
{{{ end }}}