Skip to content

Commit

Permalink
fix: refactor trpc errors (documenso#1511)
Browse files Browse the repository at this point in the history
  • Loading branch information
dguyen authored Dec 6, 2024
1 parent 3b6b96f commit 904948e
Show file tree
Hide file tree
Showing 27 changed files with 824 additions and 1,536 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { useLingui } from '@lingui/react';
import { signOut } from 'next-auth/react';

import type { User } from '@documenso/prisma/client';
import { TRPCClientError } from '@documenso/trpc/client';
import { trpc } from '@documenso/trpc/react';
import { Alert, AlertDescription, AlertTitle } from '@documenso/ui/primitives/alert';
import { Button } from '@documenso/ui/primitives/button';
Expand Down Expand Up @@ -52,30 +51,20 @@ export const DeleteAccountDialog = ({ className, user }: DeleteAccountDialogProp

return await signOut({ callbackUrl: '/' });
} catch (err) {
if (err instanceof TRPCClientError && err.data?.code === 'BAD_REQUEST') {
toast({
title: _(msg`An error occurred`),
description: err.message,
variant: 'destructive',
});
} else {
toast({
title: _(msg`An unknown error occurred`),
variant: 'destructive',
description:
err.message ??
_(
msg`We encountered an unknown error while attempting to delete your account. Please try again later.`,
),
});
}
toast({
title: _(msg`An unknown error occurred`),
variant: 'destructive',
description: _(
msg`We encountered an unknown error while attempting to delete your account. Please try again later.`,
),
});
}
};

return (
<div className={className}>
<Alert
className="flex flex-col items-center justify-between gap-4 p-6 md:flex-row "
className="flex flex-col items-center justify-between gap-4 p-6 md:flex-row"
variant="neutral"
>
<div>
Expand Down
37 changes: 18 additions & 19 deletions apps/web/src/app/(signing)/sign/[token]/complete/claim-account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useForm } from 'react-hook-form';
import { z } from 'zod';

import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { TRPCClientError } from '@documenso/trpc/client';
import { AppError } from '@documenso/lib/errors/app-error';
import { trpc } from '@documenso/trpc/react';
import { ZPasswordSchema } from '@documenso/trpc/server/auth-router/schema';
import { Button } from '@documenso/ui/primitives/button';
Expand All @@ -25,6 +25,8 @@ import { Input } from '@documenso/ui/primitives/input';
import { PasswordInput } from '@documenso/ui/primitives/password-input';
import { useToast } from '@documenso/ui/primitives/use-toast';

import { signupErrorMessages } from '~/components/forms/v2/signup';

export type ClaimAccountProps = {
defaultName: string;
defaultEmail: string;
Expand All @@ -33,7 +35,10 @@ export type ClaimAccountProps = {

export const ZClaimAccountFormSchema = z
.object({
name: z.string().trim().min(1, { message: 'Please enter a valid name.' }),
name: z
.string()
.trim()
.min(1, { message: msg`Please enter a valid name.`.id }),
email: z.string().email().min(1),
password: ZPasswordSchema,
})
Expand All @@ -43,7 +48,7 @@ export const ZClaimAccountFormSchema = z
return !password.includes(name) && !password.includes(email.split('@')[0]);
},
{
message: 'Password should not be common or based on personal information',
message: msg`Password should not be common or based on personal information`.id,
path: ['password'],
},
);
Expand Down Expand Up @@ -86,22 +91,16 @@ export const ClaimAccount = ({ defaultName, defaultEmail }: ClaimAccountProps) =
email,
timestamp: new Date().toISOString(),
});
} catch (error) {
if (error instanceof TRPCClientError && error.data?.code === 'BAD_REQUEST') {
toast({
title: _(msg`An error occurred`),
description: error.message,
variant: 'destructive',
});
} else {
toast({
title: _(msg`An unknown error occurred`),
description: _(
msg`We encountered an unknown error while attempting to sign you up. Please try again later.`,
),
variant: 'destructive',
});
}
} catch (err) {
const error = AppError.parseError(err);

const errorMessage = signupErrorMessages[error.code] ?? signupErrorMessages.INVALID_REQUEST;

toast({
title: _(msg`An error occurred`),
description: _(errorMessage),
variant: 'destructive',
});
}
};

Expand Down
29 changes: 13 additions & 16 deletions apps/web/src/components/forms/avatar-image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import { match } from 'ts-pattern';
import { z } from 'zod';

import { NEXT_PUBLIC_WEBAPP_URL } from '@documenso/lib/constants/app';
import { AppError } from '@documenso/lib/errors/app-error';
import { base64 } from '@documenso/lib/universal/base64';
import { extractInitials } from '@documenso/lib/utils/recipient-formatter';
import type { Team, User } from '@documenso/prisma/client';
import { TRPCClientError } from '@documenso/trpc/client';
import { trpc } from '@documenso/trpc/react';
import { cn } from '@documenso/ui/lib/utils';
import { Avatar, AvatarFallback, AvatarImage } from '@documenso/ui/primitives/avatar';
Expand Down Expand Up @@ -111,21 +111,18 @@ export const AvatarImageForm = ({ className, user, team }: AvatarImageFormProps)

router.refresh();
} catch (err) {
if (err instanceof TRPCClientError && err.data?.code === 'BAD_REQUEST') {
toast({
title: _(msg`An error occurred`),
description: err.message,
variant: 'destructive',
});
} else {
toast({
title: _(msg`An unknown error occurred`),
description: _(
msg`We encountered an unknown error while attempting to update the avatar. Please try again later.`,
),
variant: 'destructive',
});
}
const error = AppError.parseError(err);

const errorMessage = match(error.code).otherwise(
() =>
msg`We encountered an unknown error while attempting to update your password. Please try again later.`,
);

toast({
title: _(msg`An error occurred`),
description: _(errorMessage),
variant: 'destructive',
});
}
};

Expand Down
35 changes: 20 additions & 15 deletions apps/web/src/components/forms/password.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { zodResolver } from '@hookform/resolvers/zod';
import { Trans, msg } from '@lingui/macro';
import { useLingui } from '@lingui/react';
import { useForm } from 'react-hook-form';
import { match } from 'ts-pattern';
import { z } from 'zod';

import { AppError } from '@documenso/lib/errors/app-error';
import type { User } from '@documenso/prisma/client';
import { TRPCClientError } from '@documenso/trpc/client';
import { trpc } from '@documenso/trpc/react';
import { ZCurrentPasswordSchema, ZPasswordSchema } from '@documenso/trpc/server/auth-router/schema';
import { cn } from '@documenso/ui/lib/utils';
Expand Down Expand Up @@ -73,21 +74,25 @@ export const PasswordForm = ({ className }: PasswordFormProps) => {
duration: 5000,
});
} catch (err) {
if (err instanceof TRPCClientError && err.data?.code === 'BAD_REQUEST') {
toast({
title: _(msg`An error occurred`),
description: err.message,
variant: 'destructive',
});
} else {
toast({
title: _(msg`An unknown error occurred`),
description: _(
const error = AppError.parseError(err);

const errorMessage = match(error.code)
.with('NO_PASSWORD', () => msg`User has no password.`)
.with('INCORRECT_PASSWORD', () => msg`Current password is incorrect.`)
.with(
'SAME_PASSWORD',
() => msg`Your new password cannot be the same as your old password.`,
)
.otherwise(
() =>
msg`We encountered an unknown error while attempting to update your password. Please try again later.`,
),
variant: 'destructive',
});
}
);

toast({
title: _(msg`An error occurred`),
description: _(errorMessage),
variant: 'destructive',
});
}
};

Expand Down
35 changes: 20 additions & 15 deletions apps/web/src/components/forms/reset-password.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { zodResolver } from '@hookform/resolvers/zod';
import { Trans, msg } from '@lingui/macro';
import { useLingui } from '@lingui/react';
import { useForm } from 'react-hook-form';
import { match } from 'ts-pattern';
import { z } from 'zod';

import { TRPCClientError } from '@documenso/trpc/client';
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
import { trpc } from '@documenso/trpc/react';
import { ZPasswordSchema } from '@documenso/trpc/server/auth-router/schema';
import { cn } from '@documenso/ui/lib/utils';
Expand Down Expand Up @@ -76,21 +77,25 @@ export const ResetPasswordForm = ({ className, token }: ResetPasswordFormProps)

router.push('/signin');
} catch (err) {
if (err instanceof TRPCClientError && err.data?.code === 'BAD_REQUEST') {
toast({
title: _(msg`An error occurred`),
description: err.message,
variant: 'destructive',
});
} else {
toast({
title: _(msg`An unknown error occurred`),
description: _(
const error = AppError.parseError(err);

const errorMessage = match(error.code)
.with(AppErrorCode.EXPIRED_CODE, () => msg`Token has expired. Please try again.`)
.with('INVALID_TOKEN', () => msg`Invalid token provided. Please try again.`)
.with(
'SAME_PASSWORD',
() => msg`Your new password cannot be the same as your old password.`,
)
.otherwise(
() =>
msg`We encountered an unknown error while attempting to reset your password. Please try again later.`,
),
variant: 'destructive',
});
}
);

toast({
title: _(msg`An error occurred`),
description: _(errorMessage),
variant: 'destructive',
});
}
};

Expand Down
28 changes: 12 additions & 16 deletions apps/web/src/components/forms/signup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { FcGoogle } from 'react-icons/fc';
import { z } from 'zod';

import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { TRPCClientError } from '@documenso/trpc/client';
import { AppError } from '@documenso/lib/errors/app-error';
import { trpc } from '@documenso/trpc/react';
import { ZPasswordSchema } from '@documenso/trpc/server/auth-router/schema';
import { cn } from '@documenso/ui/lib/utils';
Expand All @@ -29,6 +29,8 @@ import { PasswordInput } from '@documenso/ui/primitives/password-input';
import { SignaturePad } from '@documenso/ui/primitives/signature-pad';
import { useToast } from '@documenso/ui/primitives/use-toast';

import { signupErrorMessages } from './v2/signup';

const SIGN_UP_REDIRECT_PATH = '/documents';

export const ZSignUpFormSchema = z
Expand Down Expand Up @@ -102,21 +104,15 @@ export const SignUpForm = ({
timestamp: new Date().toISOString(),
});
} catch (err) {
if (err instanceof TRPCClientError && err.data?.code === 'BAD_REQUEST') {
toast({
title: _(msg`An error occurred`),
description: err.message,
variant: 'destructive',
});
} else {
toast({
title: _(msg`An unknown error occurred`),
description: _(
msg`We encountered an unknown error while attempting to sign you up. Please try again later.`,
),
variant: 'destructive',
});
}
const error = AppError.parseError(err);

const errorMessage = signupErrorMessages[error.code] ?? signupErrorMessages.INVALID_REQUEST;

toast({
title: _(msg`An error occurred`),
description: _(errorMessage),
variant: 'destructive',
});
}
};

Expand Down
Loading

0 comments on commit 904948e

Please sign in to comment.