generated from nhcarrigan/template
feat: toast messages for errors and such
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* @copyright 2026 NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { ErrorHandler, Injectable, inject } from '@angular/core';
|
||||
import { ToastService } from './toast.service';
|
||||
|
||||
@Injectable()
|
||||
export class GlobalErrorHandler implements ErrorHandler {
|
||||
private toast = inject(ToastService);
|
||||
|
||||
handleError(error: Error): void {
|
||||
console.error('Global error caught:', error);
|
||||
|
||||
// Show user-friendly error message
|
||||
const message = this.getUserFriendlyMessage(error);
|
||||
this.toast.error(message);
|
||||
}
|
||||
|
||||
private getUserFriendlyMessage(error: Error): string {
|
||||
// Check for common error types
|
||||
if (error.message.includes('Http failure')) {
|
||||
return 'Network error. Please check your connection.';
|
||||
}
|
||||
|
||||
if (error.message.includes('401') || error.message.includes('403')) {
|
||||
return 'Your session has expired. Please refresh the page.';
|
||||
}
|
||||
|
||||
if (error.message.includes('404')) {
|
||||
return 'Resource not found.';
|
||||
}
|
||||
|
||||
if (error.message.includes('500')) {
|
||||
return 'Server error. Please try again later.';
|
||||
}
|
||||
|
||||
// Generic error message
|
||||
return 'Something went wrong. Please try again.';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* @copyright 2026 NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { Injectable, signal } from '@angular/core';
|
||||
|
||||
export interface Toast {
|
||||
id: number;
|
||||
message: string;
|
||||
type: 'error' | 'success' | 'info' | 'warning';
|
||||
duration: number;
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ToastService {
|
||||
private toasts = signal<Toast[]>([]);
|
||||
public readonly toastList = this.toasts.asReadonly();
|
||||
private nextId = 0;
|
||||
|
||||
/**
|
||||
* Show an error toast notification.
|
||||
*/
|
||||
error(message: string, duration = 5000): void {
|
||||
this.addToast(message, 'error', duration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a success toast notification.
|
||||
*/
|
||||
success(message: string, duration = 3000): void {
|
||||
this.addToast(message, 'success', duration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show an info toast notification.
|
||||
*/
|
||||
info(message: string, duration = 3000): void {
|
||||
this.addToast(message, 'info', duration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a warning toast notification.
|
||||
*/
|
||||
warning(message: string, duration = 4000): void {
|
||||
this.addToast(message, 'warning', duration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a toast by ID.
|
||||
*/
|
||||
remove(id: number): void {
|
||||
this.toasts.update(toasts => toasts.filter(t => t.id !== id));
|
||||
}
|
||||
|
||||
private addToast(message: string, type: Toast['type'], duration: number): void {
|
||||
const id = this.nextId++;
|
||||
const toast: Toast = { id, message, type, duration };
|
||||
|
||||
this.toasts.update(toasts => [...toasts, toast]);
|
||||
|
||||
// Auto-remove after duration
|
||||
setTimeout(() => {
|
||||
this.remove(id);
|
||||
}, duration);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user