Merge branch 'handle-timeout-error' into 'main'

Handle timeout error in Hono app.onError() function & send custom error messages in /relay

See merge request soapbox-pub/ditto!407
This commit is contained in:
Alex Gleason 2024-07-02 21:45:12 +00:00
commit 5addf58a10
3 changed files with 40 additions and 3 deletions

View file

@ -340,6 +340,13 @@ app.get('/', frontendController, indexController);
// Fallback
app.get('*', publicFiles, staticFiles, frontendController);
app.onError((err, c) => {
if (err.message === 'canceling statement due to statement timeout') {
return c.json({ error: 'The server was unable to respond in a timely manner' }, 500);
}
return c.json({ error: 'Something went wrong' }, 500);
});
export default app;
export type { AppContext, AppController, AppMiddleware };

View file

@ -191,3 +191,33 @@ Deno.test('inserting replaceable events', async () => {
await eventsDB.event(newerEvent);
assertEquals(await eventsDB.query([{ kinds: [0] }]), [newerEvent]);
});
Deno.test("throws a RelayError when querying an event with a large 'since'", async () => {
const { eventsDB } = await createDB();
await assertRejects(
() => eventsDB.query([{ since: 33333333333333 }]),
RelayError,
'since filter too far into the future',
);
});
Deno.test("throws a RelayError when querying an event with a large 'until'", async () => {
const { eventsDB } = await createDB();
await assertRejects(
() => eventsDB.query([{ until: 66666666666666 }]),
RelayError,
'until filter too far into the future',
);
});
Deno.test("throws a RelayError when querying an event with a large 'kind'", async () => {
const { eventsDB } = await createDB();
await assertRejects(
() => eventsDB.query([{ kinds: [99999999999999] }]),
RelayError,
'kind filter too far into the future',
);
});

View file

@ -147,14 +147,14 @@ class EventsDB implements NStore {
for (const filter of filters) {
if (filter.since && filter.since >= 2_147_483_647) {
throw new Error('since filter too far into the future');
throw new RelayError('invalid', 'since filter too far into the future');
}
if (filter.until && filter.until >= 2_147_483_647) {
throw new Error('until filter too far into the future');
throw new RelayError('invalid', 'until filter too far into the future');
}
for (const kind of filter.kinds ?? []) {
if (kind >= 2_147_483_647) {
throw new Error('kind filter too far into the future');
throw new RelayError('invalid', 'kind filter too far into the future');
}
}
}