From 5fd66907558b5124bc555b957c18631e0299548a Mon Sep 17 00:00:00 2001 From: April John Date: Fri, 5 May 2023 14:30:08 +0200 Subject: [PATCH] Fix: properly handle timeline db errors --- .../api/endpoints/notes/global-timeline.ts | 19 +++++++++++---- .../api/endpoints/notes/hybrid-timeline.ts | 19 +++++++++++---- .../api/endpoints/notes/local-timeline.ts | 19 +++++++++++---- .../endpoints/notes/recommended-timeline.ts | 19 +++++++++++---- .../server/api/endpoints/notes/timeline.ts | 23 +++++++++++++++---- .../api/endpoints/notes/user-list-timeline.ts | 19 +++++++++++---- 6 files changed, 88 insertions(+), 30 deletions(-) diff --git a/packages/backend/src/server/api/endpoints/notes/global-timeline.ts b/packages/backend/src/server/api/endpoints/notes/global-timeline.ts index 077a1ad5e1..93fbda8c76 100644 --- a/packages/backend/src/server/api/endpoints/notes/global-timeline.ts +++ b/packages/backend/src/server/api/endpoints/notes/global-timeline.ts @@ -32,6 +32,11 @@ export const meta = { code: "GTL_DISABLED", id: "0332fc13-6ab2-4427-ae80-a9fadffd1a6b", }, + queryError: { + message: "Please follow more users.", + code: "QUERY_ERROR", + id: "620763f4-f621-4533-ab33-0577a1a3c343", + }, }, } as const; @@ -106,11 +111,15 @@ export default define(meta, paramDef, async (ps, user) => { const found = []; const take = Math.floor(ps.limit * 1.5); let skip = 0; - while (found.length < ps.limit) { - const notes = await query.take(take).skip(skip).getMany(); - found.push(...(await Notes.packMany(notes, user))); - skip += take; - if (notes.length < take) break; + try { + while (found.length < ps.limit) { + const notes = await query.take(take).skip(skip).getMany(); + found.push(...(await Notes.packMany(notes, user))); + skip += take; + if (notes.length < take) break; + } + } catch (error) { + throw new ApiError(meta.errors.queryError); } if (found.length > ps.limit) { diff --git a/packages/backend/src/server/api/endpoints/notes/hybrid-timeline.ts b/packages/backend/src/server/api/endpoints/notes/hybrid-timeline.ts index 3c171278b4..06f9edf453 100644 --- a/packages/backend/src/server/api/endpoints/notes/hybrid-timeline.ts +++ b/packages/backend/src/server/api/endpoints/notes/hybrid-timeline.ts @@ -36,6 +36,11 @@ export const meta = { code: "STL_DISABLED", id: "620763f4-f621-4533-ab33-0577a1a3c342", }, + queryError: { + message: "Please follow more users.", + code: "QUERY_ERROR", + id: "620763f4-f621-4533-ab33-0577a1a3c343", + }, }, } as const; @@ -162,11 +167,15 @@ export default define(meta, paramDef, async (ps, user) => { const found = []; const take = Math.floor(ps.limit * 1.5); let skip = 0; - while (found.length < ps.limit) { - const notes = await query.take(take).skip(skip).getMany(); - found.push(...(await Notes.packMany(notes, user))); - skip += take; - if (notes.length < take) break; + try { + while (found.length < ps.limit) { + const notes = await query.take(take).skip(skip).getMany(); + found.push(...(await Notes.packMany(notes, user))); + skip += take; + if (notes.length < take) break; + } + } catch (error) { + throw new ApiError(meta.errors.queryError); } if (found.length > ps.limit) { diff --git a/packages/backend/src/server/api/endpoints/notes/local-timeline.ts b/packages/backend/src/server/api/endpoints/notes/local-timeline.ts index cec371c8dc..ec3d4bde09 100644 --- a/packages/backend/src/server/api/endpoints/notes/local-timeline.ts +++ b/packages/backend/src/server/api/endpoints/notes/local-timeline.ts @@ -35,6 +35,11 @@ export const meta = { code: "LTL_DISABLED", id: "45a6eb02-7695-4393-b023-dd3be9aaaefd", }, + queryError: { + message: "Please follow more users.", + code: "QUERY_ERROR", + id: "620763f4-f621-4533-ab33-0577a1a3c343", + }, }, } as const; @@ -136,11 +141,15 @@ export default define(meta, paramDef, async (ps, user) => { const found = []; const take = Math.floor(ps.limit * 1.5); let skip = 0; - while (found.length < ps.limit) { - const notes = await query.take(take).skip(skip).getMany(); - found.push(...(await Notes.packMany(notes, user))); - skip += take; - if (notes.length < take) break; + try { + while (found.length < ps.limit) { + const notes = await query.take(take).skip(skip).getMany(); + found.push(...(await Notes.packMany(notes, user))); + skip += take; + if (notes.length < take) break; + } + } catch (error) { + throw new ApiError(meta.errors.queryError); } if (found.length > ps.limit) { diff --git a/packages/backend/src/server/api/endpoints/notes/recommended-timeline.ts b/packages/backend/src/server/api/endpoints/notes/recommended-timeline.ts index 56847b1dd2..27b72f4a3f 100644 --- a/packages/backend/src/server/api/endpoints/notes/recommended-timeline.ts +++ b/packages/backend/src/server/api/endpoints/notes/recommended-timeline.ts @@ -35,6 +35,11 @@ export const meta = { code: "RTL_DISABLED", id: "45a6eb02-7695-4393-b023-dd3be9aaaefe", }, + queryError: { + message: "Please follow more users.", + code: "QUERY_ERROR", + id: "620763f4-f621-4533-ab33-0577a1a3c343", + }, }, } as const; @@ -139,11 +144,15 @@ export default define(meta, paramDef, async (ps, user) => { const found = []; const take = Math.floor(ps.limit * 1.5); let skip = 0; - while (found.length < ps.limit) { - const notes = await query.take(take).skip(skip).getMany(); - found.push(...(await Notes.packMany(notes, user))); - skip += take; - if (notes.length < take) break; + try { + while (found.length < ps.limit) { + const notes = await query.take(take).skip(skip).getMany(); + found.push(...(await Notes.packMany(notes, user))); + skip += take; + if (notes.length < take) break; + } + } catch (error) { + throw new ApiError(meta.errors.queryError); } if (found.length > ps.limit) { diff --git a/packages/backend/src/server/api/endpoints/notes/timeline.ts b/packages/backend/src/server/api/endpoints/notes/timeline.ts index f85c0cfd32..436b50e9f4 100644 --- a/packages/backend/src/server/api/endpoints/notes/timeline.ts +++ b/packages/backend/src/server/api/endpoints/notes/timeline.ts @@ -10,6 +10,7 @@ import { generateMutedNoteQuery } from "../../common/generate-muted-note-query.j import { generateChannelQuery } from "../../common/generate-channel-query.js"; import { generateBlockedUserQuery } from "../../common/generate-block-query.js"; import { generateMutedUserRenotesQueryForNotes } from "../../common/generated-muted-renote-query.js"; +import { ApiError } from "../../error.js"; export const meta = { tags: ["notes"], @@ -27,6 +28,14 @@ export const meta = { ref: "Note", }, }, + + errors: { + queryError: { + message: "Please follow more users.", + code: "QUERY_ERROR", + id: "620763f4-f621-4533-ab33-0577a1a3c343", + }, + }, } as const; export const paramDef = { @@ -154,11 +163,15 @@ export default define(meta, paramDef, async (ps, user) => { const found = []; const take = Math.floor(ps.limit * 1.5); let skip = 0; - while (found.length < ps.limit) { - const notes = await query.take(take).skip(skip).getMany(); - found.push(...(await Notes.packMany(notes, user))); - skip += take; - if (notes.length < take) break; + try { + while (found.length < ps.limit) { + const notes = await query.take(take).skip(skip).getMany(); + found.push(...(await Notes.packMany(notes, user))); + skip += take; + if (notes.length < take) break; + } + } catch (error) { + throw new ApiError(meta.errors.queryError); } if (found.length > ps.limit) { diff --git a/packages/backend/src/server/api/endpoints/notes/user-list-timeline.ts b/packages/backend/src/server/api/endpoints/notes/user-list-timeline.ts index 03f5cee3f3..5c3fc55bef 100644 --- a/packages/backend/src/server/api/endpoints/notes/user-list-timeline.ts +++ b/packages/backend/src/server/api/endpoints/notes/user-list-timeline.ts @@ -29,6 +29,11 @@ export const meta = { code: "NO_SUCH_LIST", id: "8fb1fbd5-e476-4c37-9fb0-43d55b63a2ff", }, + queryError: { + message: "Please follow more users.", + code: "QUERY_ERROR", + id: "620763f4-f621-4533-ab33-0577a1a3c343", + }, }, } as const; @@ -149,11 +154,15 @@ export default define(meta, paramDef, async (ps, user) => { const found = []; const take = Math.floor(ps.limit * 1.5); let skip = 0; - while (found.length < ps.limit) { - const notes = await query.take(take).skip(skip).getMany(); - found.push(...(await Notes.packMany(notes, user))); - skip += take; - if (notes.length < take) break; + try { + while (found.length < ps.limit) { + const notes = await query.take(take).skip(skip).getMany(); + found.push(...(await Notes.packMany(notes, user))); + skip += take; + if (notes.length < take) break; + } + } catch (error) { + throw new ApiError(meta.errors.queryError); } if (found.length > ps.limit) {