A React application demonstrating centralized error handling with Axios interceptors and Material-UI snackbars.
- Centralized Error Handling: All HTTP errors are handled in one place using Axios response interceptors
- Automatic Error Display: Error messages are automatically shown via Material-UI snackbars
- Comprehensive Error Types: Handles various HTTP status codes (400, 401, 403, 404, 500) and network errors
- Modern UI: Built with React, TypeScript, Tailwind CSS, and Material-UI
- Error Testing: Interactive buttons to test different error scenarios
- React 18 with TypeScript
- Vite for build tooling
- Axios for HTTP requests
- Material-UI for snackbar notifications
- Tailwind CSS for styling
- Lucide React for icons
- Clone the repository:
git clone <repository-url>
cd axios-error-handling- Install dependencies:
npm install- Start the development server:
npm run dev- Open your browser and navigate to
http://localhost:5173
src/
├── api/
│ ├── axiosInstance.ts # Axios configuration with interceptors
│ └── userService.ts # API service functions
├── components/
│ └── UserList.tsx # Main component with error testing
├── context/
│ └── ErrorContext.tsx # Error context and snackbar provider
├── snackbarUtils.ts # Global snackbar utilities
└── App.tsx # Main app component
The application uses a centralized error handling approach:
- Axios Interceptor: All HTTP errors are caught by the response interceptor in
axiosInstance.ts - Global Snackbar: The interceptor automatically displays error messages using Material-UI snackbars
- Error Context: The
ErrorContextprovides a global error display system - Snackbar Utils: A utility system allows the interceptor to trigger snackbars from anywhere
- 400 Bad Request: Input validation errors
- 401 Unauthorized: Authentication required
- 403 Forbidden: Insufficient permissions
- 404 Not Found: Resource not found
- 500 Server Error: Internal server errors
- Network Errors: Connection issues
import { getUsers } from './api/userService';
try {
const users = await getUsers();
// Success handling
} catch (error) {
// Error is automatically handled by interceptor
// No need to manually show error messages
}The application includes interactive buttons to test various error scenarios:
- Fetch Users: Normal API call to JSONPlaceholder
- 404 Error: Tests not found errors
- 400 Bad Request: Tests validation errors
- 401 Unauthorized: Tests authentication errors
- 500 Server Error: Tests server errors
- Network Error: Tests connection issues
- API Request → Axios instance
- Error Occurs → Response interceptor catches it
- Error Processing → Interceptor formats the error message
- Snackbar Display → Error message is automatically shown
- Promise Rejection → Error is still thrown for component handling
To handle additional HTTP status codes, update the interceptor in axiosInstance.ts:
switch (error.response.status) {
case 422:
errorMessage = 'Validation Error: Please check your input';
break;
case 429:
errorMessage = 'Too Many Requests: Please try again later';
break;
// Add more cases as needed
}Modify the error messages in the interceptor to match your application's tone and requirements.
The application uses Tailwind CSS for styling. Modify the classes in components to change the appearance.
The application includes built-in error testing capabilities:
- Click "Fetch Users" to test successful API calls
- Use the error testing buttons to simulate different error scenarios
- Observe how errors are automatically displayed via snackbars
- DRY Principle: No duplicate error handling code across components
- Consistency: All errors are displayed uniformly
- Maintainability: Error handling logic is centralized
- User Experience: Automatic error feedback without manual intervention
- Developer Experience: Components focus on business logic, not error handling
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
This project is open source and available under the MIT License.
- JSONPlaceholder for the test API
- Material-UI for the snackbar components
- Tailwind CSS for styling utilities