From db09c383f832e27ba14045c51d686100e1bb204e Mon Sep 17 00:00:00 2001 From: teodorus-nathaniel Date: Fri, 13 Sep 2024 22:12:20 +0700 Subject: [PATCH 1/2] Show error message in error toast --- src/hooks/useToastError.tsx | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/hooks/useToastError.tsx b/src/hooks/useToastError.tsx index 3a9536f26..b8b15eb5d 100644 --- a/src/hooks/useToastError.tsx +++ b/src/hooks/useToastError.tsx @@ -1,6 +1,7 @@ import CustomToast from '@/components/Toast' +import truncate from 'lodash.truncate' import { ReactNode, useEffect, useRef } from 'react' -import { toast, Toast, ToastOptions } from 'react-hot-toast' +import { Toast, ToastOptions, toast } from 'react-hot-toast' export function showErrorToast( error: unknown, @@ -13,15 +14,16 @@ export function showErrorToast( } ) { const { actionButton, getMessage, toastConfig } = config ?? {} - // TODO: this error message should be sent to logger for debugging purposes, but not shown to user - // let message: string | undefined = (error as any)?.message + let message: string | undefined = (error as any)?.message - // const response = (error as any)?.response?.data - // message = response?.message ?? message - // if (getMessage) { - // const responseMessage = getMessage(response) - // if (responseMessage) message = responseMessage - // } + const response = (error as any)?.response?.data + message = response?.message ?? message + if (getMessage) { + const responseMessage = getMessage(response) + if (responseMessage) message = responseMessage + } + + message = truncate(message, { length: 150 }) toast.custom( (t) => ( @@ -29,7 +31,7 @@ export function showErrorToast( t={t} type='error' title={errorTitle} - // subtitle={message} + subtitle={message} description={config?.getDescription?.(t)} action={actionButton?.(t)} /> From 0f500799d1f01c340b6fc26060afce132a2524a8 Mon Sep 17 00:00:00 2001 From: teodorus-nathaniel Date: Fri, 13 Sep 2024 22:13:41 +0700 Subject: [PATCH 2/2] Add validation to prevent runtime error --- src/hooks/useToastError.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/hooks/useToastError.tsx b/src/hooks/useToastError.tsx index b8b15eb5d..9dab66801 100644 --- a/src/hooks/useToastError.tsx +++ b/src/hooks/useToastError.tsx @@ -23,7 +23,9 @@ export function showErrorToast( if (responseMessage) message = responseMessage } - message = truncate(message, { length: 150 }) + message = truncate(typeof message === 'string' ? message : '', { + length: 150, + }) toast.custom( (t) => (