|
8 | 8 | getRetryDelay, |
9 | 9 | } from '../../src/lib/retryPolicy/delivery-sdk-handlers'; |
10 | 10 | import MockAdapter from 'axios-mock-adapter'; |
| 11 | +import { APIError } from '../../src/lib/api-error'; |
11 | 12 |
|
12 | 13 | describe('retryRequestHandler', () => { |
13 | 14 | it('should add retryCount to the request config', () => { |
@@ -201,22 +202,38 @@ describe('retryResponseErrorHandler', () => { |
201 | 202 | jest.useRealTimers(); |
202 | 203 | }); |
203 | 204 |
|
204 | | - it('should resolve the promise to 408 error if retryOnError is true and error code is ECONNABORTED', async () => { |
| 205 | + it('should throw a real Error with the timeout duration and a TIMEOUT code when ECONNABORTED occurs', async () => { |
205 | 206 | const error = { config: { retryOnError: true, retryCount: 1 }, code: 'ECONNABORTED' }; |
206 | 207 | const config = { retryLimit: 5, timeout: 1000 }; |
207 | 208 | const client = axios.create(); |
208 | 209 | try { |
209 | 210 | await retryResponseErrorHandler(error, config, client); |
210 | 211 | fail('Expected retryResponseErrorHandler to throw an error'); |
211 | | - } catch (err) { |
212 | | - expect(err).toEqual( |
213 | | - expect.objectContaining({ |
214 | | - error_code: 408, |
215 | | - error_message: `Request timeout of ${config.timeout}ms exceeded. Please try again or increase the timeout value in your configuration.`, |
216 | | - errors: null, |
217 | | - }) |
| 212 | + } catch (err: any) { |
| 213 | + expect(err).toBeInstanceOf(Error); |
| 214 | + expect(err.message).toBe( |
| 215 | + `Request timeout of ${config.timeout}ms exceeded. Please try again or increase the timeout value in your configuration.` |
218 | 216 | ); |
| 217 | + expect(err.code).toBe(408); |
| 218 | + } |
| 219 | + }); |
| 220 | + it('should classify a request timeout distinctly instead of as an unknown error', async () => { |
| 221 | + const error = { config: { retryOnError: true, retryCount: 1 }, code: 'ECONNABORTED' }; |
| 222 | + const config = { retryLimit: 5, timeout: 1000 }; |
| 223 | + const client = axios.create(); |
| 224 | + |
| 225 | + let thrown: any; |
| 226 | + try { |
| 227 | + await retryResponseErrorHandler(error, config, client); |
| 228 | + fail('Expected retryResponseErrorHandler to throw an error'); |
| 229 | + } catch (err) { |
| 230 | + thrown = err; |
219 | 231 | } |
| 232 | + |
| 233 | + const apiError = APIError.fromAxiosError(thrown); |
| 234 | + |
| 235 | + expect(apiError.error_code).toBe(408); |
| 236 | + expect(apiError.error_message).toContain('timeout'); |
220 | 237 | }); |
221 | 238 | it('should reject the promise if response status is 429 and retryCount exceeds retryLimit', async () => { |
222 | 239 | const error = { |
|
0 commit comments