From 2899873b26f2cf802461c273594ebc198ced78c5 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Sat, 7 Oct 2023 19:49:33 +0200 Subject: [PATCH] [mastodon-client] Use MastoApiError everywhere --- .../server/api/mastodon/endpoints/filter.ts | 4 ++-- .../server/api/mastodon/endpoints/status.ts | 7 ++----- .../src/server/api/mastodon/middleware/auth.ts | 18 ++++++------------ 3 files changed, 10 insertions(+), 19 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/endpoints/filter.ts b/packages/backend/src/server/api/mastodon/endpoints/filter.ts index 069d0044e..b57adb30a 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/filter.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/filter.ts @@ -1,5 +1,6 @@ import Router from "@koa/router"; import { auth } from "@/server/api/mastodon/middleware/auth.js"; +import { MastoApiError } from "@/server/api/mastodon/middleware/catch-errors.js"; export function setupEndpointsFilter(router: Router): void { router.get(["/v1/filters", "/v2/filters"], @@ -11,8 +12,7 @@ export function setupEndpointsFilter(router: Router): void { router.post(["/v1/filters", "/v2/filters"], auth(true, ['write:filters']), async (ctx) => { - ctx.status = 400; - ctx.body = { error: "Please change word mute settings in the web frontend settings." }; + throw new MastoApiError(400, "Please change word mute settings in the web frontend settings."); } ); } diff --git a/packages/backend/src/server/api/mastodon/endpoints/status.ts b/packages/backend/src/server/api/mastodon/endpoints/status.ts index 2b25bdb23..8cc8927e4 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/status.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/status.ts @@ -15,6 +15,7 @@ import { UserConverter } from "@/server/api/mastodon/converters/user.js"; import { PollHelpers } from "@/server/api/mastodon/helpers/poll.js"; import { toArray } from "@/prelude/array.js"; import { auth } from "@/server/api/mastodon/middleware/auth.js"; +import { MastoApiError } from "@/server/api/mastodon/middleware/catch-errors.js"; export function setupEndpointsStatus(router: Router): void { router.post("/v1/statuses", @@ -280,11 +281,7 @@ export function setupEndpointsStatus(router: Router): void { const body: any = ctx.request.body; const choices = toArray(body.choices ?? []).map(p => parseInt(p)); - if (choices.length < 1) { - ctx.status = 400; - ctx.body = { error: 'Must vote for at least one option' }; - return; - } + if (choices.length < 1) throw new MastoApiError(400, "Must vote for at least one option"); const data = await PollHelpers.voteInPoll(choices, note, ctx.user, ctx.cache); ctx.body = convertPollId(data); diff --git a/packages/backend/src/server/api/mastodon/middleware/auth.ts b/packages/backend/src/server/api/mastodon/middleware/auth.ts index 152dfb73a..c656d9f7a 100644 --- a/packages/backend/src/server/api/mastodon/middleware/auth.ts +++ b/packages/backend/src/server/api/mastodon/middleware/auth.ts @@ -2,6 +2,7 @@ import authenticate from "@/server/api/authenticate.js"; import { ILocalUser } from "@/models/entities/user.js"; import { MastoContext } from "@/server/api/mastodon/index.js"; import { AuthConverter } from "@/server/api/mastodon/converters/auth.js"; +import { MastoApiError } from "@/server/api/mastodon/middleware/catch-errors.js"; export async function AuthMiddleware(ctx: MastoContext, next: () => Promise) { const auth = await authenticate(ctx.headers.authorization, null, true); @@ -13,20 +14,13 @@ export async function AuthMiddleware(ctx: MastoContext, next: () => Promise export function auth(required: boolean, scopes: string[] = []) { return async function auth(ctx: MastoContext, next: () => Promise) { - if (required && !ctx.user) { - ctx.status = 401; - ctx.body = { error: "This method requires an authenticated user" }; - return; - } + if (required && !ctx.user) throw new MastoApiError(401, "This method requires an authenticated user"); if (!AuthConverter.decode(scopes).every(p => ctx.scopes.includes(p))) { - if (required) { - ctx.status = 403; - ctx.body = { error: "This action is outside the authorized scopes" }; - } else { - ctx.user = null; - ctx.scopes = []; - } + if (required) throw new MastoApiError(403, "This action is outside the authorized scopes") + + ctx.user = null; + ctx.scopes = []; } ctx.scopes = AuthConverter.encode(ctx.scopes);