Hey is a decentralized and permissionless social media app built with Lens Protocol 🌿

Add reusable API error handler (#5790)

authored by yoginth.com and committed by

GitHub aeb51ab3 5424b9d1

+27 -20
+2 -2
apps/api/src/routes/cron/guild/syncFollowersStandingToGuild.ts
··· 1 - import { ERRORS } from "@hey/data/errors"; 2 1 import type { Context } from "hono"; 2 + import handleApiError from "src/utils/handleApiError"; 3 3 import lensPg from "src/utils/lensPg"; 4 4 import syncAddressesToGuild from "src/utils/syncAddressesToGuild"; 5 5 ··· 26 26 27 27 return ctx.json(data); 28 28 } catch { 29 - return ctx.json({ success: false, error: ERRORS.SomethingWentWrong }, 500); 29 + return handleApiError(ctx); 30 30 } 31 31 }; 32 32
+2 -2
apps/api/src/routes/cron/guild/syncSubscribersToGuild.ts
··· 1 1 import { PERMISSIONS } from "@hey/data/constants"; 2 - import { ERRORS } from "@hey/data/errors"; 3 2 import type { Context } from "hono"; 3 + import handleApiError from "src/utils/handleApiError"; 4 4 import lensPg from "src/utils/lensPg"; 5 5 import syncAddressesToGuild from "src/utils/syncAddressesToGuild"; 6 6 ··· 32 32 33 33 return ctx.json(data); 34 34 } catch { 35 - return ctx.json({ success: false, error: ERRORS.SomethingWentWrong }, 500); 35 + return handleApiError(ctx); 36 36 } 37 37 }; 38 38
+2 -2
apps/api/src/routes/cron/removeExpiredSubscribers/index.ts
··· 1 1 import { PERMISSIONS } from "@hey/data/constants"; 2 - import { ERRORS } from "@hey/data/errors"; 3 2 import type { Context } from "hono"; 3 + import handleApiError from "src/utils/handleApiError"; 4 4 import lensPg from "src/utils/lensPg"; 5 5 import signer from "src/utils/signer"; 6 6 import ABI from "./ABI"; ··· 41 41 42 42 return ctx.json({ success: true, addresses, hash }); 43 43 } catch { 44 - return ctx.json({ success: false, error: ERRORS.SomethingWentWrong }, 500); 44 + return handleApiError(ctx); 45 45 } 46 46 }; 47 47
+2 -2
apps/api/src/routes/jumper/getStats.ts
··· 1 - import { ERRORS } from "@hey/data/errors"; 2 1 import type { Context } from "hono"; 3 2 import getDbPostId from "src/utils/getDbPostId"; 3 + import handleApiError from "src/utils/handleApiError"; 4 4 import lensPg from "src/utils/lensPg"; 5 5 6 6 const getStats = async (ctx: Context) => { ··· 60 60 comments: Number(result[2].count) 61 61 }); 62 62 } catch { 63 - return ctx.json({ success: false, error: ERRORS.SomethingWentWrong }, 500); 63 + return handleApiError(ctx); 64 64 } 65 65 }; 66 66
+2 -2
apps/api/src/routes/lens/authorization.ts
··· 1 - import { ERRORS } from "@hey/data/errors"; 2 1 import type { Context } from "hono"; 2 + import handleApiError from "src/utils/handleApiError"; 3 3 4 4 const authorization = async (ctx: Context) => { 5 5 try { ··· 21 21 signingKey: process.env.PRIVATE_KEY 22 22 }); 23 23 } catch { 24 - return ctx.json({ success: false, error: ERRORS.SomethingWentWrong }, 500); 24 + return handleApiError(ctx); 25 25 } 26 26 }; 27 27
+2 -2
apps/api/src/routes/live/createLive.ts
··· 1 1 import { LIVEPEER_KEY } from "@hey/data/constants"; 2 - import { ERRORS } from "@hey/data/errors"; 3 2 import generateUUID from "@hey/helpers/generateUUID"; 4 3 import type { Context } from "hono"; 4 + import handleApiError from "src/utils/handleApiError"; 5 5 6 6 const createLive = async (ctx: Context) => { 7 7 try { ··· 45 45 } 46 46 }); 47 47 } catch { 48 - return ctx.json({ success: false, error: ERRORS.SomethingWentWrong }, 500); 48 + return handleApiError(ctx); 49 49 } 50 50 }; 51 51
+2 -2
apps/api/src/routes/metadata/getSTS.ts
··· 1 1 import { AssumeRoleCommand, STSClient } from "@aws-sdk/client-sts"; 2 2 import { EVER_API, EVER_BUCKET, EVER_REGION } from "@hey/data/constants"; 3 - import { ERRORS } from "@hey/data/errors"; 4 3 import type { Context } from "hono"; 4 + import handleApiError from "src/utils/handleApiError"; 5 5 6 6 const params = { 7 7 DurationSeconds: 900, ··· 49 49 } 50 50 }); 51 51 } catch { 52 - return ctx.json({ success: false, error: ERRORS.SomethingWentWrong }, 500); 52 + return handleApiError(ctx); 53 53 } 54 54 }; 55 55
+2 -2
apps/api/src/routes/oembed/getOembed.ts
··· 1 - import { ERRORS } from "@hey/data/errors"; 2 1 import type { Context } from "hono"; 3 2 import { CACHE_AGE_1_DAY } from "src/utils/constants"; 3 + import handleApiError from "src/utils/handleApiError"; 4 4 import { generateExtraLongExpiry, getRedis, setRedis } from "src/utils/redis"; 5 5 import sha256 from "src/utils/sha256"; 6 6 import getMetadata from "./helpers/getMetadata"; ··· 26 26 27 27 return ctx.json({ success: true, data: oembed }); 28 28 } catch { 29 - return ctx.json({ success: false, error: ERRORS.SomethingWentWrong }, 500); 29 + return handleApiError(ctx); 30 30 } 31 31 }; 32 32
+2 -2
apps/api/src/routes/preferences/getPreferences.ts
··· 1 - import { ERRORS } from "@hey/data/errors"; 2 1 import type { Context } from "hono"; 3 2 import prisma from "src/prisma/client"; 3 + import handleApiError from "src/utils/handleApiError"; 4 4 import { getRedis, setRedis } from "src/utils/redis"; 5 5 6 6 const getPreferences = async (ctx: Context) => { ··· 31 31 32 32 return ctx.json({ success: true, data }); 33 33 } catch { 34 - return ctx.json({ success: false, error: ERRORS.SomethingWentWrong }, 500); 34 + return handleApiError(ctx); 35 35 } 36 36 }; 37 37
+2 -2
apps/api/src/routes/preferences/updatePreferences.ts
··· 1 - import { ERRORS } from "@hey/data/errors"; 2 1 import type { Context } from "hono"; 3 2 import prisma from "src/prisma/client"; 3 + import handleApiError from "src/utils/handleApiError"; 4 4 import { delRedis } from "src/utils/redis"; 5 5 6 6 const updatePreferences = async (ctx: Context) => { ··· 24 24 } 25 25 }); 26 26 } catch { 27 - return ctx.json({ success: false, error: ERRORS.SomethingWentWrong }, 500); 27 + return handleApiError(ctx); 28 28 } 29 29 }; 30 30
+7
apps/api/src/utils/handleApiError.ts
··· 1 + import { ERRORS } from "@hey/data/errors"; 2 + import type { Context } from "hono"; 3 + 4 + const handleApiError = (ctx: Context): Response => 5 + ctx.json({ success: false, error: ERRORS.SomethingWentWrong }, 500); 6 + 7 + export default handleApiError;