import { describe, it, expect } from 'vitest';
import { AxiosError } from 'axios';
describe('API Error Scenarios', () => {
describe('HTTP Status Code Handling', () => {
it('should handle 400 Bad Request', () => {
const error = {
response: {
status: 400,
data: { message: 'Invalid request' },
},
} as AxiosError;
expect(error.response?.status).toBe(400);
expect(error.response?.data?.message).toBe('Invalid request');
});
it('should handle 403 Forbidden', () => {
const error = {
response: {
status: 403,
data: { error: 'Access denied' },
},
} as AxiosError;
expect(error.response?.status).toBe(403);
});
it('should handle 409 Conflict', () => {
const error = {
response: {
status: 409,
data: { message: 'Resource already exists' },
},
} as AxiosError;
expect(error.response?.status).toBe(409);
});
it('should handle 500 Internal Server Error', () => {
const error = {
response: {
status: 500,
data: {},
},
} as AxiosError;
expect(error.response?.status).toBe(500);
});
it('should handle 502 Bad Gateway', () => {
const error = {
response: {
status: 502,
data: {},
},
} as AxiosError;
expect(error.response?.status).toBe(502);
});
it('should handle 503 Service Unavailable', () => {
const error = {
response: {
status: 503,
data: {},
},
} as AxiosError;
expect(error.response?.status).toBe(503);
});
});
describe('Error Response Formats', () => {
it('should handle errors with message field', () => {
const error = {
response: {
status: 400,
data: { message: 'Error message' },
},
} as AxiosError;
expect(error.response?.data?.message).toBe('Error message');
});
it('should handle errors with error field', () => {
const error = {
response: {
status: 400,
data: { error: 'Error description' },
},
} as AxiosError;
expect(error.response?.data?.error).toBe('Error description');
});
it('should handle errors with both message and error fields', () => {
const error = {
response: {
status: 400,
data: { message: 'Message', error: 'Error' },
},
} as AxiosError;
expect(error.response?.data?.message).toBe('Message');
expect(error.response?.data?.error).toBe('Error');
});
});
});