|
1 | 1 | import fetch from 'jest-fetch-mock'; |
2 | 2 | import { fetchOnce, fetchURL } from '../../common/utils/test-utils'; |
3 | 3 | import { FetchHttpClient } from './fetch-client'; |
| 4 | +import { ParseError } from '../exceptions/parse-error'; |
4 | 5 |
|
5 | 6 | const fetchClient = new FetchHttpClient('https://test.workos.com', { |
6 | 7 | headers: { |
@@ -224,4 +225,82 @@ describe('Fetch client', () => { |
224 | 225 | expect(await response.toJSON()).toEqual({ data: 'response' }); |
225 | 226 | }); |
226 | 227 | }); |
| 228 | + |
| 229 | + describe('error handling', () => { |
| 230 | + it('should throw ParseError when response body is not valid JSON on non-200 status', async () => { |
| 231 | + // Mock a 500 response with invalid JSON (like an HTML error page) |
| 232 | + fetch.mockResponseOnce( |
| 233 | + '<html><body>Internal Server Error</body></html>', |
| 234 | + { |
| 235 | + status: 500, |
| 236 | + statusText: 'Internal Server Error', |
| 237 | + headers: { |
| 238 | + 'X-Request-ID': 'test-request-123', |
| 239 | + 'Content-Type': 'text/html', |
| 240 | + }, |
| 241 | + }, |
| 242 | + ); |
| 243 | + |
| 244 | + await expect(fetchClient.get('/users', {})).rejects.toThrow(ParseError); |
| 245 | + |
| 246 | + try { |
| 247 | + await fetchClient.get('/users', {}); |
| 248 | + } catch (error) { |
| 249 | + expect(error).toBeInstanceOf(ParseError); |
| 250 | + const parseError = error as ParseError; |
| 251 | + expect(parseError.message).toContain('Unexpected token'); |
| 252 | + expect(parseError.rawBody).toBe( |
| 253 | + '<html><body>Internal Server Error</body></html>', |
| 254 | + ); |
| 255 | + expect(parseError.requestID).toBe('test-request-123'); |
| 256 | + expect(parseError.rawStatus).toBe(500); |
| 257 | + } |
| 258 | + }); |
| 259 | + |
| 260 | + it('should throw ParseError for non-FGA endpoints with invalid JSON response', async () => { |
| 261 | + // Test with a non-FGA endpoint to ensure the error handling works for regular requests too |
| 262 | + fetch.mockResponseOnce('Not JSON content', { |
| 263 | + status: 400, |
| 264 | + statusText: 'Bad Request', |
| 265 | + headers: { |
| 266 | + 'X-Request-ID': 'bad-request-456', |
| 267 | + 'Content-Type': 'text/plain', |
| 268 | + }, |
| 269 | + }); |
| 270 | + |
| 271 | + await expect( |
| 272 | + fetchClient.post('/organizations', { name: 'Test' }, {}), |
| 273 | + ).rejects.toThrow(ParseError); |
| 274 | + |
| 275 | + try { |
| 276 | + await fetchClient.post('/organizations', { name: 'Test' }, {}); |
| 277 | + } catch (error) { |
| 278 | + expect(error).toBeInstanceOf(ParseError); |
| 279 | + const parseError = error as ParseError; |
| 280 | + expect(parseError.rawBody).toBe('Not JSON content'); |
| 281 | + expect(parseError.requestID).toBe('bad-request-456'); |
| 282 | + expect(parseError.rawStatus).toBe(400); |
| 283 | + } |
| 284 | + }); |
| 285 | + |
| 286 | + it('should throw ParseError when X-Request-ID header is missing', async () => { |
| 287 | + fetch.mockResponseOnce('Invalid JSON Response', { |
| 288 | + status: 422, |
| 289 | + statusText: 'Unprocessable Entity', |
| 290 | + headers: { |
| 291 | + 'Content-Type': 'application/json', |
| 292 | + }, |
| 293 | + }); |
| 294 | + |
| 295 | + try { |
| 296 | + await fetchClient.put('/users/123', { name: 'Updated' }, {}); |
| 297 | + } catch (error) { |
| 298 | + expect(error).toBeInstanceOf(ParseError); |
| 299 | + const parseError = error as ParseError; |
| 300 | + expect(parseError.rawBody).toBe('Invalid JSON Response'); |
| 301 | + expect(parseError.requestID).toBe(''); // Should default to empty string when header is missing |
| 302 | + expect(parseError.rawStatus).toBe(422); |
| 303 | + } |
| 304 | + }); |
| 305 | + }); |
227 | 306 | }); |
0 commit comments