Repository navigation
Global API Middleware for Failure Notification with Query Batching 20260415
Paulo Gomes da Cruz Junior edited this page Apr 16, 2026
·
1 revision
Implements a global API error handling strategy and query batching optimization for waste search filters. Silent CORS/network failures are now surfaced as toast notifications. Three separate code table queries are batched into a single parallel request, reducing React effect executions and improving filter load performance.
- Eliminate silent API failures by implementing global error notification middleware
- Surface CORS, network, and backend errors to users via toast notifications
- Batch independent queries to reduce React context overhead and improve performance
- Establish a reusable middleware pattern for consistent error handling across all API calls
- Enable fine-grained notification routing through metadata tracking
- Implemented
failureNotificationMiddlewareas new global API middleware insrc/config/api/failureNotificationMiddleware.ts - Added
MIDDLEWAREfield toAPIConfigtype to support composable middleware - Batched three separate
useCodesQuerycalls into singleuseWasteSearchFilterOptionsQuerieshook usinguseQueries - Extracted
notificationTargetmetadata parameter into query keys for proper cache key differentiation - Established middleware registration pattern in
BackendApiConfig
- Reduced HTTP requests by batching three independent code table queries into a single parallel batch
- Eliminated redundant React effect executions for waste search filter loading
- Improved cache efficiency by including
notificationTargetin query keys for proper cache separation
- Added
metaparameter support to all service methods:CodesService,ForestClientService,SearchService - Added
ApiMiddlewaretype with request, response, and failure handlers - Middleware merging in
request.tscombines config-levelMIDDLEWAREwith request-level middleware - Global middleware registered in
BackendApiConfigapplies to all API calls
- Global toast notifications for unhandled API failures
- Inline error notifications for scoped requests via
notificationTargetmetadata - Updated WasteSearchFilters component to use batched hook
- Error details from RFC 7807 ProblemDetails properly formatted and displayed
- Removed individual
useCodesQuerycalls in favor of batcheduseWasteSearchFilterOptionsQueries - Consolidated error handling patterns through middleware instead of component-level try-catch
- Silent failures on CORS errors are now surfaced as toast notifications
- Network unreachability errors no longer go unnoticed
- Backend error details (ProblemDetails) are properly formatted and displayed to users
- User Experience: Users now receive immediate feedback when API calls fail due to network, CORS, or backend issues
- Maintainability: Centralized error handling through middleware reduces code duplication and improves consistency
- Reliability: All API failures are now captured and reported; silent failures eliminated
- Performance: Query batching reduces React context call overhead and improves filter load performance
- Testability: Middleware can be tested independently; query batching simplifies mock setup
- WasteSearchFilters
- Reference data loading across waste search
- Unit Tests: 133 passing (includes CORS failure scenarios)
- E2E Tests: 131 passing, 35 skipped (verified with
VITE_MOCK_AUTH=true) - Type Safety: No ESLint warnings or TypeScript compilation errors
New:
src/config/api/failureNotificationMiddleware.tssrc/config/api/failureNotificationMiddleware.unit.test.ts
Modified:
src/config/api/types.tssrc/config/api/request.tssrc/config/react-query/hooks.tssrc/config/react-query/queryKeys.tssrc/services/APIs.tssrc/services/codes.service.tssrc/services/forestclient.service.tssrc/services/search.service.tssrc/components/waste/WasteSearch/WasteSearchFilters/useWasteSearchFilterOptions.tssrc/components/waste/WasteSearch/WasteSearchFilters/useWasteSearchFilterOptions.unit.test.tsxsrc/components/waste/WasteSearch/WasteSearchTable/index.unit.test.tsx
- Monitor error notification frequency to tune thresholds if needed
- Consider extending middleware pattern to other error scenarios (rate limiting, timeout handling)
- Review other query hook consumers to identify additional batching opportunities
- Add architectural documentation on middleware patterns
- Create reusable middleware templates for common scenarios
This wiki documents NR Waste Plus for users and developers. Verify security and deployment guidance against the current application configuration before applying it elsewhere.
- Report an issue or ask a question in the GitHub repository.
- Follow the Contributing Guidelines to propose an update.
- Read the Code of Conduct.
Collection indexes support human browsing and machine retrieval. Keep both current when adding or moving wiki pages.
NR Waste Plus Wiki | GitHub Repository