-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a990f5f
commit db298dc
Showing
19 changed files
with
184 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { TRPCClientError } from "@trpc/client"; | ||
import { useTranslation } from "react-i18next"; | ||
import { z } from "zod"; | ||
|
||
import { KnownErrorsTypeNames } from "@mc/common/KnownError/index"; | ||
import { useToast } from "@mc/ui/hooks/use-toast"; | ||
|
||
export default function useShowError() { | ||
const { t } = useTranslation(); | ||
const { toast } = useToast(); | ||
|
||
const showUnknown = (error: unknown): unknown => { | ||
toast({ title: t("common.unknownError"), variant: "destructive" }); | ||
return error; | ||
}; | ||
|
||
return (error: unknown) => { | ||
if (!(error instanceof TRPCClientError)) throw showUnknown(error); | ||
|
||
const errorMessage = z.enum(KnownErrorsTypeNames).safeParse(error.message); | ||
|
||
if (!errorMessage.success) throw showUnknown(error); | ||
|
||
toast({ | ||
title: t(`common.knownError`), | ||
description: t(`common.knownErrors.${errorMessage.data}`), | ||
variant: "destructive", | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export interface UserError { | ||
type: "UserError"; | ||
name: "USER_NOT_FOUND"; | ||
} | ||
export const UserErrorNames = ["USER_NOT_FOUND"] as const; | ||
|
||
export type UserError = (typeof UserErrorNames)[number]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
import type { DataSourceError } from "./DataSourceError"; | ||
import type { UserError } from "./UserError"; | ||
import { DataSourceErrorNames } from "./DataSourceError"; | ||
import { UserErrorNames } from "./UserError"; | ||
|
||
export type ErrorCause = DataSourceError | UserError; | ||
export const KnownErrorsTypeNames = [ | ||
...DataSourceErrorNames, | ||
...UserErrorNames, | ||
] as const; | ||
export type KnownErrorType = (typeof KnownErrorsTypeNames)[number]; | ||
|
||
export class KnownError extends Error { | ||
constructor(public cause: ErrorCause) { | ||
super(cause.type, { cause }); | ||
constructor( | ||
public message: KnownErrorType, | ||
options?: ErrorOptions, | ||
) { | ||
super(message, options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.