From c9526b9a31436e8b923844128d776469a9e74b28 Mon Sep 17 00:00:00 2001 From: yumeko Date: Fri, 21 Jul 2023 23:15:53 +0300 Subject: [PATCH] Throw a proper ApiError with statusCode 404 when GETting a non-existing emoji. Closes #10332 --- .../backend/src/server/api/endpoints/emoji.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/backend/src/server/api/endpoints/emoji.ts b/packages/backend/src/server/api/endpoints/emoji.ts index ddfad77374..f5a5be4ec5 100644 --- a/packages/backend/src/server/api/endpoints/emoji.ts +++ b/packages/backend/src/server/api/endpoints/emoji.ts @@ -1,6 +1,7 @@ import { IsNull } from "typeorm"; import { Emojis } from "@/models/index.js"; import define from "../define.js"; +import { ApiError } from "../error.js"; export const meta = { tags: ["meta"], @@ -9,6 +10,15 @@ export const meta = { allowGet: true, cacheSec: 3600, + errors: { + noSuchEmoji: { + message: "No such emoji.", + code: "NO_SUCH_EMOJI", + id: "6a5e3be7-5ac3-44a6-a425-9937cd66f8e1", + httpStatusCode: 404, + }, + }, + res: { type: "object", optional: false, @@ -28,12 +38,16 @@ export const paramDef = { } as const; export default define(meta, paramDef, async (ps, me) => { - const emoji = await Emojis.findOneOrFail({ + const emoji = await Emojis.findOne({ where: { name: ps.name, host: IsNull(), }, }); + if (!emoji) { + throw new ApiError(meta.errors.noSuchEmoji); + } + return Emojis.pack(emoji); });