Refactor error handling utilities
- Replace `showError` with a more versatile `showToast` utility. - Add specific toast functions (`showErrorToast`, `showSuccessToast`, etc.) for better message handling. - Remove `showError.ts` and migrate logic to `showToast.ts` for cleaner structure. - Update `ErrorBoundary` and `useErrorHandler` to use the new `showToast` functions.
This commit is contained in:
@@ -1,16 +0,0 @@
|
||||
// lib/ui/showError.ts
|
||||
import {toast} from "sonner";
|
||||
|
||||
export function showError(error: unknown, fallback = "Ein unbekannter Fehler ist aufgetreten") {
|
||||
let message: string;
|
||||
|
||||
if (error instanceof Error) {
|
||||
message = error.message;
|
||||
} else if (typeof error === "string") {
|
||||
message = error;
|
||||
} else {
|
||||
message = fallback;
|
||||
}
|
||||
|
||||
toast.error(message);
|
||||
}
|
||||
71
internal_frontend/lib/ui/showToast.ts
Normal file
71
internal_frontend/lib/ui/showToast.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
// lib/ui/showToast.ts
|
||||
import { toast } from "sonner";
|
||||
|
||||
// Generic toast function
|
||||
export function showToast(message: string, type: 'success' | 'error' | 'info' | 'warning' = 'info') {
|
||||
const options = {
|
||||
dismissible: true,
|
||||
closeButton: true,
|
||||
};
|
||||
|
||||
switch (type) {
|
||||
case 'success':
|
||||
toast.success(message, options);
|
||||
break;
|
||||
case 'error':
|
||||
toast.error(message, options);
|
||||
break;
|
||||
case 'info':
|
||||
toast.info(message, options);
|
||||
break;
|
||||
case 'warning':
|
||||
toast.warning(message, options);
|
||||
break;
|
||||
default:
|
||||
toast(message, options);
|
||||
}
|
||||
}
|
||||
|
||||
// Specific toast functions with appropriate error handling
|
||||
export function showErrorToast(error: unknown, fallback = "Ein unbekannter Fehler ist aufgetreten") {
|
||||
let message: string;
|
||||
|
||||
if (error instanceof Error) {
|
||||
message = error.message;
|
||||
} else if (typeof error === "string") {
|
||||
message = error;
|
||||
} else {
|
||||
message = fallback;
|
||||
}
|
||||
|
||||
toast.error(message, {
|
||||
dismissible: true,
|
||||
closeButton: true,
|
||||
});
|
||||
}
|
||||
|
||||
export function showSuccessToast(message: string) {
|
||||
toast.success(message, {
|
||||
dismissible: true,
|
||||
closeButton: true,
|
||||
});
|
||||
}
|
||||
|
||||
export function showInfoToast(message: string) {
|
||||
toast.info(message, {
|
||||
dismissible: true,
|
||||
closeButton: true,
|
||||
});
|
||||
}
|
||||
|
||||
export function showWarningToast(message: string) {
|
||||
toast.warning(message, {
|
||||
dismissible: true,
|
||||
closeButton: true,
|
||||
});
|
||||
}
|
||||
|
||||
// Backward compatibility - keep the original showError function
|
||||
export function showError(error: unknown, fallback = "Ein unbekannter Fehler ist aufgetreten") {
|
||||
showErrorToast(error, fallback);
|
||||
}
|
||||
Reference in New Issue
Block a user