summaryrefslogtreecommitdiff
path: root/lib/handle-error.ts
diff options
context:
space:
mode:
authorjoonhoekim <26rote@gmail.com>2025-03-25 15:55:45 +0900
committerjoonhoekim <26rote@gmail.com>2025-03-25 15:55:45 +0900
commit1a2241c40e10193c5ff7008a7b7b36cc1d855d96 (patch)
tree8a5587f10ca55b162d7e3254cb088b323a34c41b /lib/handle-error.ts
initial commit
Diffstat (limited to 'lib/handle-error.ts')
-rw-r--r--lib/handle-error.ts22
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/handle-error.ts b/lib/handle-error.ts
new file mode 100644
index 00000000..1f608723
--- /dev/null
+++ b/lib/handle-error.ts
@@ -0,0 +1,22 @@
+import { toast } from "sonner"
+import { z } from "zod"
+
+export function getErrorMessage(err: unknown) {
+ const unknownError = "Something went wrong, please try again later."
+
+ if (err instanceof z.ZodError) {
+ const errors = err.issues.map((issue) => {
+ return issue.message
+ })
+ return errors.join("\n")
+ } else if (err instanceof Error) {
+ return err.message
+ } else {
+ return unknownError
+ }
+}
+
+export function showErrorToast(err: unknown) {
+ const errorMessage = getErrorMessage(err)
+ return toast.error(errorMessage)
+}