Skip to content

Commit

Permalink
feat: create rpc toast error type
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbrusegard committed Nov 10, 2024
1 parent b36724f commit 4b962e3
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 3 deletions.
3 changes: 2 additions & 1 deletion messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"error": "Oops! Something went wrong",
"errorDescription": "Don't worry, our best hackers are on it!",
"goToHomepage": "Return to homepage",
"tryAgain": "Try again"
"tryAgain": "Try again",
"invalidLocale": "Invalid locale: {locale}"
},
"auth": {
"welcome": "Welcome!",
Expand Down
3 changes: 2 additions & 1 deletion messages/no.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"error": "Oops! Noe gikk galt",
"errorDescription": "Ikke bekymre deg, våre beste hackere jobber med saken!",
"goToHomepage": "Gå tilbake til hjemmesiden",
"tryAgain": "Prøv igjen"
"tryAgain": "Prøv igjen",
"invalidLocale": "Ugyldig språk: {locale}"
},
"auth": {
"welcome": "Velkommen!",
Expand Down
20 changes: 20 additions & 0 deletions src/server/api/locale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { TRPCError } from '@trpc/server';
import { getTranslations } from 'next-intl/server';
import { cookies } from 'next/headers';

import { routing } from '@/lib/locale';

async function getLocaleFromCookie() {
const cookieStore = await cookies();
const locale = cookieStore.get('locale')?.value ?? routing.defaultLocale;
if (!routing.locales.includes(locale as 'en')) {
const t = await getTranslations('error');
throw new TRPCError({
code: 'BAD_REQUEST',
message: t('invalidLocale', { locale }),
});
}
return locale;
}

export { getLocaleFromCookie };
3 changes: 3 additions & 0 deletions src/server/api/routers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const authRouter = createRouter({
throw new TRPCError({
code: 'TOO_MANY_REQUESTS',
message: 'Rate limit exceeded. Please try again later.',
data: {
toast: 'error',
},
});
}
return getFeideAuthorizationURL();
Expand Down
7 changes: 6 additions & 1 deletion src/server/api/trpc.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import type { createContext } from '@/server/api/context';
import type { TRPCError } from '@/server/api/types';
import { initTRPC } from '@trpc/server';
import superjson from 'superjson';
import { ZodError } from 'zod';

const trpc = initTRPC.context<typeof createContext>().create({
transformer: superjson,
errorFormatter({ shape, error }) {
const TRPCError = error as TRPCError;
return {
...shape,
data: {
...shape.data,
zodError:
error.cause instanceof ZodError ? error.cause.flatten() : null,
TRPCError.cause instanceof ZodError
? TRPCError.cause.flatten()
: null,
toast: TRPCError.toast,
},
};
},
Expand Down
7 changes: 7 additions & 0 deletions src/server/api/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { TRPCError as BaseTRPCError } from '@trpc/server';

type TRPCError = BaseTRPCError & {
toast?: 'success' | 'info' | 'warning' | 'error';
};

export type { TRPCError };

0 comments on commit 4b962e3

Please sign in to comment.