From 885a9578642c346c67292873346e089752649655 Mon Sep 17 00:00:00 2001 From: Prakhar Sharma Date: Thu, 11 Jul 2024 20:04:48 +0530 Subject: [PATCH] refactor: localized api errors --- components/account/ManageSessions.tsx | 7 ++++--- components/account/UpdateEmail.tsx | 2 +- components/account/UpdateName.tsx | 2 +- components/account/UpdatePassword.tsx | 5 ++--- components/account/UploadAvatar.tsx | 6 +++--- components/apiKey/APIKeys.tsx | 2 +- components/apiKey/NewAPIKey.tsx | 2 +- components/auth/Join.tsx | 2 +- components/auth/JoinWithInvitation.tsx | 5 ++--- components/auth/ResetPassword.tsx | 5 ++--- components/billing/LinkToPortal.tsx | 2 +- components/invitation/AcceptInvitation.tsx | 2 +- components/invitation/InviteViaEmail.tsx | 2 +- components/invitation/InviteViaLink.tsx | 4 ++-- components/invitation/PendingInvitations.tsx | 5 ++--- components/team/CreateTeam.tsx | 2 +- components/team/Members.tsx | 5 ++--- components/team/RemoveTeam.tsx | 2 +- components/team/Teams.tsx | 5 ++--- components/team/UpdateMemberRole.tsx | 5 ++--- components/webhook/CreateWebhook.tsx | 5 ++--- components/webhook/EditWebhook.tsx | 5 ++--- components/webhook/Webhooks.tsx | 5 ++--- docker-compose.yml | 2 +- lib/jackson/dsync/hosted.ts | 11 ++++++----- locales/en/common.json | 5 ++++- pages/auth/forgot-password.tsx | 5 ++--- pages/auth/resend-email-token.tsx | 5 ++--- pages/auth/sso/index.tsx | 2 +- pages/auth/unlock-account.tsx | 3 ++- 30 files changed, 57 insertions(+), 63 deletions(-) diff --git a/components/account/ManageSessions.tsx b/components/account/ManageSessions.tsx index e391f2866..4098169d9 100644 --- a/components/account/ManageSessions.tsx +++ b/components/account/ManageSessions.tsx @@ -9,6 +9,7 @@ import { Session } from '@prisma/client'; import { WithLoadingAndError } from '@/components/shared'; import ConfirmationDialog from '@/components/shared/ConfirmationDialog'; import { Table } from '@/components/shared/table/Table'; +import { ApiResponse } from 'types'; type NextAuthSession = Session & { isCurrent: boolean }; @@ -35,8 +36,8 @@ const ManageSessions = () => { }); if (!response.ok) { - const json = await response.json(); - throw new Error(json.error.message); + const json = (await response.json()) as ApiResponse; + toast.error(t(json?.error?.message || 'Something went wrong')); } toast.success(t('session-removed')); @@ -45,7 +46,7 @@ const ManageSessions = () => { window.location.reload(); } } catch (error: any) { - toast.error(error.message); + toast.error(t(error?.message)); } finally { mutate(); setSessionToDelete(null); diff --git a/components/account/UpdateEmail.tsx b/components/account/UpdateEmail.tsx index 1a83b94a8..837a495f9 100644 --- a/components/account/UpdateEmail.tsx +++ b/components/account/UpdateEmail.tsx @@ -39,7 +39,7 @@ const UpdateEmail = ({ user, allowEmailChange }: UpdateEmailProps) => { if (!response.ok) { const json = (await response.json()) as ApiResponse; - toast.error(json.error.message); + toast.error(t(json?.error?.message || 'Something went wrong')); return; } diff --git a/components/account/UpdateName.tsx b/components/account/UpdateName.tsx index d21908f52..b82af0431 100644 --- a/components/account/UpdateName.tsx +++ b/components/account/UpdateName.tsx @@ -38,7 +38,7 @@ const UpdateName = ({ user }: { user: Partial }) => { if (!response.ok) { const json = (await response.json()) as ApiResponse; - toast.error(json.error.message); + toast.error(t(json?.error?.message || 'Something went wrong')); return; } diff --git a/components/account/UpdatePassword.tsx b/components/account/UpdatePassword.tsx index d986d42f8..5f7692d7f 100644 --- a/components/account/UpdatePassword.tsx +++ b/components/account/UpdatePassword.tsx @@ -32,10 +32,9 @@ const UpdatePassword = () => { body: JSON.stringify(values), }); - const json = await response.json(); - if (!response.ok) { - toast.error(json.error.message); + const json = await response.json(); + toast.error(t(json?.error?.message || 'Something went wrong')); return; } diff --git a/components/account/UploadAvatar.tsx b/components/account/UploadAvatar.tsx index 53791b8ad..99d39afeb 100644 --- a/components/account/UploadAvatar.tsx +++ b/components/account/UploadAvatar.tsx @@ -44,12 +44,12 @@ const UploadAvatar = ({ user }: { user: Partial }) => { const onAvatarUpload = (file: File) => { if (file.size / 1024 / 1024 > 2) { - toast.error('File size too big (max 2MB)'); + toast.error(t('file-size-too-big')); return; } if (file.type !== 'image/png' && file.type !== 'image/jpeg') { - toast.error('File type not supported (.png or .jpg only)'); + toast.error(t('file-type-not-supported')); return; } @@ -76,7 +76,7 @@ const UploadAvatar = ({ user }: { user: Partial }) => { if (!response.ok) { const json = (await response.json()) as ApiResponse; - toast.error(json.error.message); + toast.error(t(json?.error?.message || 'Something went wrong')); return; } diff --git a/components/apiKey/APIKeys.tsx b/components/apiKey/APIKeys.tsx index 0b65b259a..1fd59deb3 100644 --- a/components/apiKey/APIKeys.tsx +++ b/components/apiKey/APIKeys.tsx @@ -40,7 +40,7 @@ const APIKeys = ({ team }: APIKeysProps) => { if (!response.ok) { const { error } = (await response.json()) as ApiResponse; - toast.error(error.message); + toast.error(t(error?.message || 'Something went wrong')); return; } diff --git a/components/apiKey/NewAPIKey.tsx b/components/apiKey/NewAPIKey.tsx index 92f98ac48..8ef29bcb2 100644 --- a/components/apiKey/NewAPIKey.tsx +++ b/components/apiKey/NewAPIKey.tsx @@ -76,7 +76,7 @@ const CreateAPIKeyForm = ({ }>; if (error) { - toast.error(error.message); + toast.error(t(error?.message || 'Something went wrong')); return; } diff --git a/components/auth/Join.tsx b/components/auth/Join.tsx index 9307db4a6..d24054b01 100644 --- a/components/auth/Join.tsx +++ b/components/auth/Join.tsx @@ -66,7 +66,7 @@ const Join = ({ recaptchaSiteKey }: JoinProps) => { recaptchaRef.current?.reset(); if (!response.ok) { - toast.error(json.error.message); + toast.error(t(json?.error?.message || 'Something went wrong')); return; } diff --git a/components/auth/JoinWithInvitation.tsx b/components/auth/JoinWithInvitation.tsx index 500871044..ce2faa343 100644 --- a/components/auth/JoinWithInvitation.tsx +++ b/components/auth/JoinWithInvitation.tsx @@ -80,12 +80,11 @@ const JoinWithInvitation = ({ }), }); - const json = (await response.json()) as ApiResponse; - recaptchaRef.current?.reset(); if (!response.ok) { - toast.error(json.error.message); + const json = (await response.json()) as ApiResponse; + toast.error(t(json?.error?.message || 'Something went wrong')); return; } diff --git a/components/auth/ResetPassword.tsx b/components/auth/ResetPassword.tsx index c7bec5c8e..a6c4e6c8a 100644 --- a/components/auth/ResetPassword.tsx +++ b/components/auth/ResetPassword.tsx @@ -49,12 +49,11 @@ const ResetPassword = () => { }), }); - const json = (await response.json()) as ApiResponse; - setSubmitting(false); if (!response.ok) { - toast.error(json.error.message); + const json = (await response.json()) as ApiResponse; + toast.error(t(json?.error?.message || 'Something went wrong')); return; } diff --git a/components/billing/LinkToPortal.tsx b/components/billing/LinkToPortal.tsx index dcd546539..35ac3a069 100644 --- a/components/billing/LinkToPortal.tsx +++ b/components/billing/LinkToPortal.tsx @@ -32,7 +32,7 @@ const LinkToPortal = ({ team }: LinkToPortalProps) => { const result = (await response.json()) as ApiResponse<{ url: string }>; if (!response.ok) { - toast.error(result.error.message); + toast.error(t(result?.error?.message || 'Something went wrong')); return; } diff --git a/components/invitation/AcceptInvitation.tsx b/components/invitation/AcceptInvitation.tsx index 1a9cf0781..82dd818f9 100644 --- a/components/invitation/AcceptInvitation.tsx +++ b/components/invitation/AcceptInvitation.tsx @@ -27,7 +27,7 @@ const AcceptInvitation = ({ invitation }: AcceptInvitationProps) => { if (!response.ok) { const result = (await response.json()) as ApiResponse; - toast.error(result.error.message); + toast.error(t(result?.error?.message || 'Something went wrong')); return; } diff --git a/components/invitation/InviteViaEmail.tsx b/components/invitation/InviteViaEmail.tsx index 4fb408098..120088bda 100644 --- a/components/invitation/InviteViaEmail.tsx +++ b/components/invitation/InviteViaEmail.tsx @@ -45,7 +45,7 @@ const InviteViaEmail = ({ setVisible, team }: InviteViaEmailProps) => { if (!response.ok) { const result = (await response.json()) as ApiResponse; - toast.error(result.error.message); + toast.error(t(result?.error?.message || 'Something went wrong')); return; } diff --git a/components/invitation/InviteViaLink.tsx b/components/invitation/InviteViaLink.tsx index ba467db41..2c9c11e93 100644 --- a/components/invitation/InviteViaLink.tsx +++ b/components/invitation/InviteViaLink.tsx @@ -63,7 +63,7 @@ const InviteViaLink = ({ team }: InviteViaLinkProps) => { if (!response.ok) { const result = (await response.json()) as ApiResponse; - toast.error(result.error.message); + toast.error(t(result?.error?.message || 'Something went wrong')); return; } @@ -85,7 +85,7 @@ const InviteViaLink = ({ team }: InviteViaLinkProps) => { if (!response.ok) { const result = (await response.json()) as ApiResponse; - toast.error(result.error.message); + toast.error(t(result?.error?.message || 'Something went wrong')); return; } diff --git a/components/invitation/PendingInvitations.tsx b/components/invitation/PendingInvitations.tsx index f75fe096e..50d9b364a 100644 --- a/components/invitation/PendingInvitations.tsx +++ b/components/invitation/PendingInvitations.tsx @@ -47,10 +47,9 @@ const PendingInvitations = ({ team }: { team: Team }) => { } ); - const json = (await response.json()) as ApiResponse; - if (!response.ok) { - toast.error(json.error.message); + const json = (await response.json()) as ApiResponse; + toast.error(t(json?.error?.message || 'Something went wrong')); return; } diff --git a/components/team/CreateTeam.tsx b/components/team/CreateTeam.tsx index d55b57989..3634f2007 100644 --- a/components/team/CreateTeam.tsx +++ b/components/team/CreateTeam.tsx @@ -39,7 +39,7 @@ const CreateTeam = ({ visible, setVisible }: CreateTeamProps) => { const json = (await response.json()) as ApiResponse; if (!response.ok) { - toast.error(json.error.message); + toast.error(t(json?.error?.message || 'Something went wrong')); return; } diff --git a/components/team/Members.tsx b/components/team/Members.tsx index 115a1d71c..383efa007 100644 --- a/components/team/Members.tsx +++ b/components/team/Members.tsx @@ -56,10 +56,9 @@ const Members = ({ team }: { team: Team }) => { } ); - const json = (await response.json()) as ApiResponse; - if (!response.ok) { - toast.error(json.error.message); + const json = (await response.json()) as ApiResponse; + toast.error(t(json?.error?.message || 'Something went wrong')); return; } diff --git a/components/team/RemoveTeam.tsx b/components/team/RemoveTeam.tsx index 89557e5da..8e570cc81 100644 --- a/components/team/RemoveTeam.tsx +++ b/components/team/RemoveTeam.tsx @@ -33,7 +33,7 @@ const RemoveTeam = ({ team, allowDelete }: RemoveTeamProps) => { if (!response.ok) { const json = (await response.json()) as ApiResponse; - toast.error(json.error.message); + toast.error(t(json?.error?.message || 'Something went wrong')); return; } diff --git a/components/team/Teams.tsx b/components/team/Teams.tsx index 0d62abdc7..caf2774be 100644 --- a/components/team/Teams.tsx +++ b/components/team/Teams.tsx @@ -36,10 +36,9 @@ const Teams = () => { headers: defaultHeaders, }); - const json = (await response.json()) as ApiResponse; - if (!response.ok) { - toast.error(json.error.message); + const json = (await response.json()) as ApiResponse; + toast.error(t(json?.error?.message || 'Something went wrong')); return; } diff --git a/components/team/UpdateMemberRole.tsx b/components/team/UpdateMemberRole.tsx index 88d8d71a2..01d4534fb 100644 --- a/components/team/UpdateMemberRole.tsx +++ b/components/team/UpdateMemberRole.tsx @@ -23,10 +23,9 @@ const UpdateMemberRole = ({ team, member }: UpdateMemberRoleProps) => { }), }); - const json = (await response.json()) as ApiResponse; - if (!response.ok) { - toast.error(json.error.message); + const json = (await response.json()) as ApiResponse; + toast.error(t(json?.error?.message || 'Something went wrong')); return; } diff --git a/components/webhook/CreateWebhook.tsx b/components/webhook/CreateWebhook.tsx index b0470ecc0..749eee1ec 100644 --- a/components/webhook/CreateWebhook.tsx +++ b/components/webhook/CreateWebhook.tsx @@ -32,10 +32,9 @@ const CreateWebhook = ({ body: JSON.stringify(values), }); - const json = (await response.json()) as ApiResponse; - if (!response.ok) { - toast.error(json.error.message); + const json = (await response.json()) as ApiResponse; + toast.error(t(json?.error?.message || 'Something went wrong')); return; } diff --git a/components/webhook/EditWebhook.tsx b/components/webhook/EditWebhook.tsx index 5c549c8a2..4dcc2c285 100644 --- a/components/webhook/EditWebhook.tsx +++ b/components/webhook/EditWebhook.tsx @@ -49,10 +49,9 @@ const EditWebhook = ({ } ); - const json = (await response.json()) as ApiResponse; - if (!response.ok) { - toast.error(json.error.message); + const json = (await response.json()) as ApiResponse; + toast.error(t(json?.error?.message || 'Something went wrong')); return; } diff --git a/components/webhook/Webhooks.tsx b/components/webhook/Webhooks.tsx index bae27a697..9f61b4df1 100644 --- a/components/webhook/Webhooks.tsx +++ b/components/webhook/Webhooks.tsx @@ -46,10 +46,9 @@ const Webhooks = ({ team }: { team: Team }) => { } ); - const json = (await response.json()) as ApiResponse; - if (!response.ok) { - toast.error(json.error.message); + const json = (await response.json()) as ApiResponse; + toast.error(t(json?.error?.message || 'Something went wrong')); return; } diff --git a/docker-compose.yml b/docker-compose.yml index 5765f75a3..a14cccc0e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,7 @@ services: db: image: postgres - restart: always + # restart: always environment: POSTGRES_PASSWORD: admin POSTGRES_USER: admin diff --git a/lib/jackson/dsync/hosted.ts b/lib/jackson/dsync/hosted.ts index 43357602f..2651fa02f 100644 --- a/lib/jackson/dsync/hosted.ts +++ b/lib/jackson/dsync/hosted.ts @@ -4,6 +4,7 @@ import env from '@/lib/env'; import { options } from '../config'; import { ApiError } from '@/lib/errors'; import type { JacksonDsync } from './utils'; +import type { ApiResponse } from 'types'; export class JacksonHosted implements JacksonDsync { private dsyncUrl = `${env.jackson.url}/api/v1/dsync`; @@ -30,7 +31,7 @@ export class JacksonHosted implements JacksonDsync { }), }); - const { data, error } = await response.json(); + const { data, error } = (await response.json()) as ApiResponse; if (!response.ok) { throw new ApiError(response.status, error.message); @@ -49,7 +50,7 @@ export class JacksonHosted implements JacksonDsync { ...options, }); - const { data, error } = await response.json(); + const { data, error } = (await response.json()) as ApiResponse; if (!response.ok) { throw new ApiError(response.status, error.message); @@ -65,7 +66,7 @@ export class JacksonHosted implements JacksonDsync { body: JSON.stringify(params), }); - const { data, error } = await response.json(); + const { data, error } = (await response.json()) as ApiResponse; if (!response.ok) { throw new ApiError(response.status, error.message); @@ -80,7 +81,7 @@ export class JacksonHosted implements JacksonDsync { method: 'DELETE', }); - const { data, error } = await response.json(); + const { data, error } = (await response.json()) as ApiResponse; if (!response.ok) { throw new ApiError(response.status, error.message); @@ -94,7 +95,7 @@ export class JacksonHosted implements JacksonDsync { ...options, }); - const { data, error } = await response.json(); + const { data, error } = (await response.json()) as ApiResponse; if (!response.ok) { throw new ApiError(response.status, error.message); diff --git a/locales/en/common.json b/locales/en/common.json index b9e37449e..f657962bd 100644 --- a/locales/en/common.json +++ b/locales/en/common.json @@ -246,5 +246,8 @@ "internal-server-error": "Internal Server Error !", "unable-to-find": "We're unable to find out what's happening! We suggest you to", "try-again-later": "or visit here later.", - "multiple-sso-teams": "User belongs to multiple teams with SSO enabled. Please enter your team slug to get started." + "multiple-sso-teams": "User belongs to multiple teams with SSO enabled. Please enter your team slug to get started.", + "Something went wrong": "Something went wrong", + "file-size-too-big": "File size too big (max 2MB)", + "file-type-not-supported": "File type not supported (.png or .jpg only)" } diff --git a/pages/auth/forgot-password.tsx b/pages/auth/forgot-password.tsx index d674cc5ee..26994515d 100644 --- a/pages/auth/forgot-password.tsx +++ b/pages/auth/forgot-password.tsx @@ -43,13 +43,12 @@ const ForgotPassword: NextPageWithLayout< }), }); - const json = (await response.json()) as ApiResponse; - formik.resetForm(); recaptchaRef.current?.reset(); if (!response.ok) { - toast.error(json.error.message); + const json = (await response.json()) as ApiResponse; + toast.error(t(json?.error?.message || 'Something went wrong')); return; } diff --git a/pages/auth/resend-email-token.tsx b/pages/auth/resend-email-token.tsx index f833eebfd..6bbf9fafc 100644 --- a/pages/auth/resend-email-token.tsx +++ b/pages/auth/resend-email-token.tsx @@ -49,10 +49,9 @@ const VerifyAccount: NextPageWithLayout< body: JSON.stringify(values), }); - const json = (await response.json()) as ApiResponse; - if (!response.ok) { - toast.error(json.error.message); + const json = (await response.json()) as ApiResponse; + toast.error(t(json?.error?.message || 'Something went wrong')); return; } diff --git a/pages/auth/sso/index.tsx b/pages/auth/sso/index.tsx index c00c65d9e..10513c8e1 100644 --- a/pages/auth/sso/index.tsx +++ b/pages/auth/sso/index.tsx @@ -52,7 +52,7 @@ const SSO: NextPageWithLayout< const { data, error } = await response.json(); if (error) { - toast.error(error.message); + toast.error(t(error?.message || 'Something went wrong')); return; } if (data.useSlug) { diff --git a/pages/auth/unlock-account.tsx b/pages/auth/unlock-account.tsx index 7db8519cf..10b24d2d6 100644 --- a/pages/auth/unlock-account.tsx +++ b/pages/auth/unlock-account.tsx @@ -14,6 +14,7 @@ import { defaultHeaders } from '@/lib/common'; import { AuthLayout } from '@/components/layouts'; import { unlockAccount } from '@/lib/accountLock'; import { getUser } from 'models/user'; +import { ApiResponse } from 'types'; interface UnlockAccountProps { email: string; @@ -61,7 +62,7 @@ const UnlockAccount = ({ }); if (!response.ok) { - const json = await response.json(); + const json = (await response.json()) as ApiResponse; throw new Error(json.error.message); }