Files
library/apps/frontend/src/app/services/global-error-handler.service.ts
T
hikari 6d5b0581a5
Node.js CI / CI (push) Has been cancelled
Security Scan and Upload / Security & DefectDojo Upload (push) Has been cancelled
fix: base64 uploads, audit log noise, and stale chunk reloads (#69)
## Summary

- **Base64 cover image uploads broken for books, shows, manga, and music** — a premature `validateStringLength` check ran before the data URL detection, rejecting all base64 images with a 2,048-char URL limit error. Also fixed the size calculation to extract only the base64 portion after the comma (matching the correct pattern already in `game.service.ts`).
- **Audit log flooded with expected 401s on `/api/auth/me`** — these occur during normal token refresh flow and are not genuine security events. Excluded this URL from the global 401/403 audit log handler.
- **ChunkLoadError spam after deployments** — when Angular lazy-loaded chunks are missing (stale cache after a redeploy), the global error handler now detects `ChunkLoadError` and silently reloads the page instead of logging the error and sending it to the API/Discord.

## Test plan

- [ ] Upload a base64 cover image for a book, show, manga, and music item — should succeed
- [ ] Verify `/api/auth/me` 401s no longer appear in the audit log
- [ ] Deploy a new build and confirm stale-chunk users are silently reloaded

 This PR was created with help from Hikari~ 🌸

Reviewed-on: #69
Co-authored-by: Hikari <hikari@nhcarrigan.com>
Co-committed-by: Hikari <hikari@nhcarrigan.com>
2026-02-23 20:37:52 -08:00

49 lines
1.3 KiB
TypeScript

/**
* @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 {
if (error.name === 'ChunkLoadError' || error.message.includes('Loading chunk')) {
window.location.reload();
return;
}
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.';
}
}