Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1285 refactor:API errors not localized #1525

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions components/account/ManageSessions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand All @@ -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'));
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion components/account/UpdateEmail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion components/account/UpdateName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const UpdateName = ({ user }: { user: Partial<User> }) => {

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;
}

Expand Down
5 changes: 2 additions & 3 deletions components/account/UpdatePassword.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions components/account/UploadAvatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ const UploadAvatar = ({ user }: { user: Partial<User> }) => {

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;
}

Expand All @@ -76,7 +76,7 @@ const UploadAvatar = ({ user }: { user: Partial<User> }) => {

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;
}

Expand Down
2 changes: 1 addition & 1 deletion components/apiKey/APIKeys.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion components/apiKey/NewAPIKey.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const CreateAPIKeyForm = ({
}>;

if (error) {
toast.error(error.message);
toast.error(t(error?.message || 'Something went wrong'));
return;
}

Expand Down
2 changes: 1 addition & 1 deletion components/auth/Join.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 2 additions & 3 deletions components/auth/JoinWithInvitation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 2 additions & 3 deletions components/auth/ResetPassword.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion components/billing/LinkToPortal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion components/invitation/AcceptInvitation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion components/invitation/InviteViaEmail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions components/invitation/InviteViaLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
5 changes: 2 additions & 3 deletions components/invitation/PendingInvitations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ const PendingInvitations = ({ team }: { team: Team }) => {
}
);

const json = (await response.json()) as ApiResponse<unknown>;

if (!response.ok) {
toast.error(json.error.message);
const json = (await response.json()) as ApiResponse<unknown>;
toast.error(t(json?.error?.message || 'Something went wrong'));
return;
}

Expand Down
2 changes: 1 addition & 1 deletion components/team/CreateTeam.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const CreateTeam = ({ visible, setVisible }: CreateTeamProps) => {
const json = (await response.json()) as ApiResponse<Team>;

if (!response.ok) {
toast.error(json.error.message);
toast.error(t(json?.error?.message || 'Something went wrong'));
return;
}

Expand Down
5 changes: 2 additions & 3 deletions components/team/Members.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion components/team/RemoveTeam.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 2 additions & 3 deletions components/team/Teams.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 2 additions & 3 deletions components/team/UpdateMemberRole.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 2 additions & 3 deletions components/webhook/CreateWebhook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ const CreateWebhook = ({
body: JSON.stringify(values),
});

const json = (await response.json()) as ApiResponse<Team>;

if (!response.ok) {
toast.error(json.error.message);
const json = (await response.json()) as ApiResponse<Team>;
toast.error(t(json?.error?.message || 'Something went wrong'));
return;
}

Expand Down
5 changes: 2 additions & 3 deletions components/webhook/EditWebhook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 2 additions & 3 deletions components/webhook/Webhooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
services:
db:
image: postgres
restart: always
# restart: always
environment:
POSTGRES_PASSWORD: admin
POSTGRES_USER: admin
Expand Down
11 changes: 6 additions & 5 deletions lib/jackson/dsync/hosted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
}
5 changes: 2 additions & 3 deletions pages/auth/forgot-password.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 2 additions & 3 deletions pages/auth/resend-email-token.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Loading