mirror of
https://github.com/pezkuwichain/pwap.git
synced 2026-06-18 04:41:00 +00:00
fix: support both sonner and shadcn toast APIs in error handler
This commit is contained in:
+61
-49
@@ -336,66 +336,70 @@ export function getUserFriendlyError(
|
|||||||
// TOAST HELPER
|
// TOAST HELPER
|
||||||
// ========================================
|
// ========================================
|
||||||
|
|
||||||
|
// Support both shadcn/ui toast and sonner toast
|
||||||
export interface ToastFunction {
|
export interface ToastFunction {
|
||||||
(options: {
|
(options: {
|
||||||
title: string;
|
title: string;
|
||||||
description: string;
|
description: string;
|
||||||
variant?: 'default' | 'destructive';
|
variant?: 'default' | 'destructive';
|
||||||
}): void;
|
}): void;
|
||||||
|
error?: (message: string, options?: { description?: string }) => void;
|
||||||
|
success?: (message: string, options?: { description?: string }) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle blockchain error with toast notification
|
* Handle blockchain error with toast notification
|
||||||
* Automatically extracts user-friendly message
|
* Automatically extracts user-friendly message
|
||||||
|
* Supports both shadcn/ui toast and sonner toast APIs
|
||||||
*/
|
*/
|
||||||
export function handleBlockchainError(
|
export function handleBlockchainError(
|
||||||
error: any,
|
error: any,
|
||||||
api: ApiPromise | null,
|
api: ApiPromise | null,
|
||||||
toast: ToastFunction,
|
toast: ToastFunction | any,
|
||||||
language: 'en' | 'kmr' = 'en'
|
language: 'en' | 'kmr' = 'en'
|
||||||
): void {
|
): void {
|
||||||
console.error('Blockchain error:', error);
|
let errorMessage = language === 'en'
|
||||||
|
? 'An unexpected error occurred. Please try again.'
|
||||||
|
: 'Xeletîyek nediyar qewimî. Ji kerema xwe dîsa biceribîne.';
|
||||||
|
let errorTitle = language === 'en' ? 'Error' : 'Xeletî';
|
||||||
|
|
||||||
// If it's a dispatch error from transaction callback
|
// If it's a dispatch error from transaction callback
|
||||||
if (error?.isModule !== undefined && api) {
|
if (error?.isModule !== undefined && api) {
|
||||||
const userMessage = getUserFriendlyError(api, error, language);
|
try {
|
||||||
|
const errorInfo = extractDispatchError(api, error);
|
||||||
|
console.error('Blockchain module error:', errorInfo);
|
||||||
|
errorMessage = getUserFriendlyError(api, error, language);
|
||||||
|
errorTitle = language === 'en' ? 'Transaction Failed' : 'Transaction Têk Çû';
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Failed to extract error:', e);
|
||||||
|
errorMessage = `Module error: ${error.type || 'Unknown'}`;
|
||||||
|
}
|
||||||
|
} else if (error?.message) {
|
||||||
|
errorMessage = error.message;
|
||||||
|
} else if (typeof error === 'string') {
|
||||||
|
errorMessage = error;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.error('Blockchain error:', errorMessage);
|
||||||
|
|
||||||
|
// Try sonner toast API first (toast.error)
|
||||||
|
if (typeof toast?.error === 'function') {
|
||||||
|
toast.error(errorTitle, { description: errorMessage });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fall back to shadcn/ui toast API
|
||||||
|
if (typeof toast === 'function') {
|
||||||
toast({
|
toast({
|
||||||
title: language === 'en' ? 'Transaction Failed' : 'Transaction Têk Çû',
|
title: errorTitle,
|
||||||
description: userMessage,
|
description: errorMessage,
|
||||||
variant: 'destructive',
|
variant: 'destructive',
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If it's a standard error object
|
// Last resort - just log
|
||||||
if (error?.message) {
|
console.error('Toast not available:', errorTitle, errorMessage);
|
||||||
toast({
|
|
||||||
title: language === 'en' ? 'Error' : 'Xeletî',
|
|
||||||
description: error.message,
|
|
||||||
variant: 'destructive',
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If it's a string
|
|
||||||
if (typeof error === 'string') {
|
|
||||||
toast({
|
|
||||||
title: language === 'en' ? 'Error' : 'Xeletî',
|
|
||||||
description: error,
|
|
||||||
variant: 'destructive',
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generic fallback
|
|
||||||
toast({
|
|
||||||
title: language === 'en' ? 'Error' : 'Xeletî',
|
|
||||||
description:
|
|
||||||
language === 'en'
|
|
||||||
? 'An unexpected error occurred. Please try again.'
|
|
||||||
: 'Xeletîyek nediyar qewimî. Ji kerema xwe dîsa biceribîne.',
|
|
||||||
variant: 'destructive',
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ========================================
|
// ========================================
|
||||||
@@ -507,31 +511,39 @@ export const SUCCESS_MESSAGES: Record<string, SuccessMessage> = {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle successful blockchain transaction
|
* Handle successful blockchain transaction
|
||||||
|
* Supports both shadcn/ui toast and sonner toast APIs
|
||||||
*/
|
*/
|
||||||
export function handleBlockchainSuccess(
|
export function handleBlockchainSuccess(
|
||||||
messageKey: string,
|
messageKey: string,
|
||||||
toast: ToastFunction,
|
toast: ToastFunction | any,
|
||||||
params: Record<string, string | number> = {},
|
params: Record<string, string | number> = {},
|
||||||
language: 'en' | 'kmr' = 'en'
|
language: 'en' | 'kmr' = 'en'
|
||||||
): void {
|
): void {
|
||||||
const template = SUCCESS_MESSAGES[messageKey];
|
const template = SUCCESS_MESSAGES[messageKey];
|
||||||
|
const title = language === 'en' ? 'Success' : 'Serkeft';
|
||||||
|
|
||||||
if (!template) {
|
let message = language === 'en' ? 'Transaction successful!' : 'Transaction serkeftî!';
|
||||||
toast({
|
|
||||||
title: language === 'en' ? 'Success' : 'Serkeft',
|
if (template) {
|
||||||
description: language === 'en' ? 'Transaction successful!' : 'Transaction serkeftî!',
|
// Replace template variables like {{amount}}
|
||||||
|
message = template[language];
|
||||||
|
Object.entries(params).forEach(([key, value]) => {
|
||||||
|
message = message.replace(new RegExp(`{{${key}}}`, 'g'), String(value));
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try sonner toast API first (toast.success)
|
||||||
|
if (typeof toast?.success === 'function') {
|
||||||
|
toast.success(title, { description: message });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace template variables like {{amount}}
|
// Fall back to shadcn/ui toast API
|
||||||
let message = template[language];
|
if (typeof toast === 'function') {
|
||||||
Object.entries(params).forEach(([key, value]) => {
|
toast({
|
||||||
message = message.replace(new RegExp(`{{${key}}}`, 'g'), String(value));
|
title,
|
||||||
});
|
description: message,
|
||||||
|
});
|
||||||
toast({
|
return;
|
||||||
title: language === 'en' ? 'Success' : 'Serkeft',
|
}
|
||||||
description: message,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user